-
Notifications
You must be signed in to change notification settings - Fork 390
Snippets
- How to install and use
- T10_Command
- T10_CommandTyped
- T10_Dispatch
- T10_DispatchAwait
- T10_DispatchAwaitResult
- T10_Event
- T10_EventTyped
- T10_INotifyPropertyChanged
- T10_Property
- T10_PropertySetting
- T10_PropertyValidated
- T10_SampleData
- T10_SingletonClass
- T10_ViewModel
- T10_ViewModelFull
The Template 10 project provides a number of useful code snippets to help make the most of the libary.
At the moment, the snippets are only available if you download the source code for Template 10 and then manually configure Visual Studio to find the snippets. There is work being done to make the snippets available as a Visual Studio Extension (like the Templates pack) but this hasn't yet been finished.
To add the snippets to your copy of Visual Studio, start by clicking Tools > Snippet Manager. Make sure that the selected language is CSharp then click on the Add button. Find the downloaded sources for Template 10 then find this directory:
VisualStudio\Vsix\Snippets\CSharp
When you've navigated to this folder, click on Select folder.
Please note that this directory also contains a XAML snippet. This currently cannot be used in this configuration as it needs to be in its own directory. The source code for T10 will be updated soon to reflect this.
To use a snippet, simply type the snippet name and press TAB to have it expanded in Visual Studio.
As noted above, the XAML snippet (T10_PageContent) currently cannot be used.
Expands to create a command definition:
DelegateCommand _executeCommand;
public DelegateCommand ExecuteCommand
=> _executeCommand ?? (_executeCommand = new DelegateCommand(() =>
{
throw new NotImplementedException();
}, () => true));
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();
}
Expands to code snippet that allows an action to be executed on the current UI thread:
WindowWrapper.Current().Dispatcher.Dispatch(() => { /* TODO */ });
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 */ });
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); });
Expands to create an event and method to raise the event:
public event EventHandler MyEvent;
void RaiseMyEvent() => MyEvent?.Invoke(this, new EventArgs { });
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.
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
}
Expands to create a property definition that raises the PropertyChanged 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 thePropertyChanged
event if the values don't match (useful as in some circumstances, dur to knock on effects/relationships the assignment may be called recusively).
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); }
}
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); } }
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
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());
}
A snippet that expands to create the basis for a view model class:
public class MyPageViewModel : ViewModelBase
{
}
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;
}
}
- Home
- Getting Started
- Template 10 Templates
- Behaviors and Actions
- Bootstrapper
- Controls
- Converters
- Services
- Hints, tips and other documentation