Skip to content
pcolmer edited this page Sep 21, 2016 · 4 revisions

Table of Contents

  1. T10_Command
  2. T10_CommandTyped
  3. T10_Dispatch
  4. T10_DispatchAwait
  5. T10_DispatchAwaitResult
  6. T10_Event
  7. T10_INotifyPropertyChanged
  8. T10_PropertySetting
  9. T10_PropertyValidated
  10. T10_SampleData
  11. T10_SingletonClass
  12. T10_ViewModel
  13. T10_ViewModelFull

T10_Command

Expands to create a command definition:

DelegateCommand _executeCommand;
public DelegateCommand ExecuteCommand
    => _executeCommand ?? (_executeCommand = new DelegateCommand(() =>
    {
        throw new NotImplementedException();
    }, () => true));

T10_CommandTyped

Expands to create a strongly type command:

DelegateCommand<object> _MyCommand;
public DelegateCommand<object> MyCommand
    => _MyCommand ?? (_MyCommand = new DelegateCommand<object>(MyCommandExecute, MyCommandCanExecute));
bool MyCommandCanExecute(object param) => true;
void MyCommandExecute(object param)
{
    throw new NotImplementedException();
}

T10_Dispatch

Expands to code snippet that allows an action to be executed on the current UI thread:

WindowWrapper.Current().Dispatcher.Dispatch(() => { /* TODO */ });

T10_DispatchAwait

Expands to code snippet that allows an async action to be executed on the current UI thread and the task to be awaited:

await WindowWrapper.Current().Dispatcher.DispatchAsync(() => { /* TODO */ });

T10_DispatchAwaitResult

Expands to code snippet that allows an async action to be executed on the current UI thread, the task to be awaited and the result to be assigned:

var MyVariable = await WindowWrapper.Current().Dispatcher
                 .DispatchAsync<string>(() => { return default(string); });

T10_Event

Expands to create an event and method to raise the event:

public event EventHandler MyEvent;
void RaiseMyEvent() => MyEvent?.Invoke(this, new EventArgs { });

T10_EventTyped

Expands to create an event that takes a strongly typed value:

public event EventHandler<string> MyEvent;
void RaiseMyEvent(string value) => MyEvent?.Invoke(this, value);

Note: by convention the value would derive from EventArg.

T10_INotifyPropertyChanged

Expands to implement the INotifiyPropertyChanged interface:

#region INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

public void Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
    if (!Equals(storage, value))
    {
        storage = value;
        RaisePropertyChanged(propertyName);
    }
}

public void RaisePropertyChanged([CallerMemberName] string propertyName = null) =>
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

#endregion

Note: You still need to add the interface to your class definition.

public class MyViewModel : INotifiyPropertyChanged
{
    // expand T10_INotifyPropertyChanged here
}

T10_Property

Expands to create a proerty definition that raises the ProertyChanged event:

string _MyProperty = default(string);
public string MyProperty { get { return _MyProperty; } set { Set(ref _MyProperty, value); } }

Note: the Set method checks if the incoming value matches the existing value and only assigns the value\raises the PropertyChanged event if the values don't match (useful as in some circumstances, dur to knock on effects/relationships the assignment may be called recusively).

T10_PropertySetting

Expands to create a property that leverages the SettingsHelper to read\write the value to the local app settings:

public string MyProperty
{
    get { return (new SettingsHelper()).Read<string>(nameof(MyProperty), default(string)); }
    set { (new SettingsHelper()).Write(nameof(MyProperty), value); }
}

T10_PropertyValidated

Expands to create a property that calls functions for read and writing values that provides the means to validate data before writing, etc.:

public string MyProperty { get { return Read<string>(); } set { Write(value); } }  

T10_SampleData

Expands to create some color sample data:

#region SampleData
          
public class ColorInfo
{
    public Color Color { get; internal set; }
    public string Name { get; internal set; }
    public string Hex => Color.ToString();
    public Brush Brush => new SolidColorBrush(Color);
}

public ObservableCollection<ColorInfo> Colors { get; }
    = new ObservableCollection<ColorInfo>(typeof(Colors).GetRuntimeProperties()
        .Select(x => new ColorInfo { Name = x.Name, Color = (Color)x.GetValue(null) }));

ColorInfo _Color = default(ColorInfo);
public ColorInfo Color { get { return _Color; } set { Set(ref _Color, value); } }

#endregion

T10_SingletonClass

A snippet that expands to create the basis for a singleton class:

public class YourClassName
{
    private YourClassName() 
    {
        // constructor
    }
    private static YourClassName sInstance;
    public static YourClassName Instance => sInstance ?? (sInstance = new YourClassName());
}

T10_ViewModel

A snippet that expands to create the basis for a view model class:

public class MyPageViewModel : ViewModelBase
{
}

T10_ViewModelFull

A snippet that expands to create a full example of a view model class:

public class MyPageViewModel : ViewModelBase
{
    public MyPageViewModel() 
    {
        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            // design-time experience
        }
        else
        {
            // runtime experience
        }
    }
    
    // sample data
    public IEnumerable<object> Items =>
        typeof(Windows.UI.Colors).GetRuntimeProperties()
            .Select(x => new { Name = x.Name, Color = (Color)x.GetValue(null) });
    
    public override Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
    {
        if (state.Any())
        {
            // restore state
            state.Clear();
        }
        else
        {
            // use parameter
        }
        return Task.CompletedTask;
    }

    public override Task OnNavigatedFromAsync(IDictionary<string, object> state, bool suspending)
    {
        if (suspending)
        {
            // save state
        }
        return Task.CompletedTask;
    }

    public override Task OnNavigatingFromAsync(NavigatingEventArgs args)
    {
        args.Cancel = false;
        return Task.CompletedTask;
    }
}
Clone this wiki locally