public event EventHandler HandlerName;
var handler = HandlerName; if (handler != null) handler(o, e);
if (HandlerName != null) HandlerName(o, e);
public event EventHandler HandlerName = (o, e) => { };
public event EventHandler HandlerName = delegate { };
HandlerName(o, e);
private string _name; public string Name { get { return _name; } set { _name = value; var handler = PropertyChanged; if (handler != null) PropertyChanged(this, new PropertyChangedEventArgs("Name")); } }
public string Name { get { return Get(() => Name); } set { Set(() => Name, value); } }
PropertyChanged += (o, e) => { if (e.PropertyName != "Name") return; // do something }
this[() => Name].PropertyChanging += (o, e) => { // do somethig }; this[() => Name].PropertyChanged += (o, e) => { // do somethig };
viewModel[() => viewModel.Name].PropertyChanged += (o, e) => { // do somethig };
this[() => Name].PropertyChanged += (o, e) => SaveChanges();
this[() => Name].Validation += () => Error = Name == null || Name.Length < 3 ? Unity.App.Localize("InvalidName") : null;
((Type2)((Type1)obj).Property1)).Property2 = 77;
Of<TType>()
. obj.Of<Type1>().Property1.Of<Type2>.Property2 = 77;
public static class Sugar { public static T Of<T>(this object o) { return (T) o; } public static bool Is<T>(this object o) { return o is T; } public static T As<T>(this object o) where T : class { return o as T; } }
List<TItem>
There is a convenient ForEach method, but it is useful to extend it for collections of other types public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action) { foreach (var item in collection) { action(item); } }
persons.Where(p => p.HasChanged).ForEach(p => p.Save());
public static class AsyncAdapter { public static TResult Await<TResult>(this Task<TResult> operation) { // deadlock safe variations // var result = default(TResult); // Task.Factory.StartNew(async () => result = await operation).Wait(); // return result; // return Task.Run(() => operation.Result).Result; return operation.Result; } public static TResult Await<TResult>(this IAsyncOperation<TResult> operation) { return operation.AsTask().Result; } public static TResult Await<TResult, TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> operation) { return operation.AsTask().Result; } }
// var result = await source.GetItemsAsync(); var result = source.GetItemsAsync().Await();
public class HelloViewModel : ContextObject, IExposable { public string Message { get { return Get(() => Message); } set { Set(() => Message, value); } } public virtual void Expose() { this[() => Message].PropertyChanged += (sender, args) => Context.Make.RaiseCanExecuteChanged(); this[Context.Make].CanExecute += (sender, args) => args.CanExecute = !string.IsNullOrEmpty(Message); this[Context.Make].Executed += async (sender, args) => { await MessageService.ShowAsync(Message); }; } }
<Window DataContext="{Store Key=viewModels:HelloViewModel}"> <StackPanel> <TextBox Text="{Binding Message, Mode=TwoWay}"> <Button Content="{Localizing Make}" Command="{Context Key=Make}"> </StackPanel> </Window>
<Button Command="New"/>
this[ApplicationCommands.New].Executed += (o, e) => { ... };
Source: https://habr.com/ru/post/255759/
All Articles