WPF私的vscode用スニペット一覧

VSCodeを入れ直したときに最初に設定すること(自分用メモ)

配置場所

C:\Users\<User>\AppData\Roaming\Code\User\snippets\csharp.json

VSCode C# スニペット例

{
  "propvm": {
    "prefix": "propvm",
    "body": [
      "private ${1:type} _${2:name};",
      "public ${1:type} ${2:name}",
      "{",
      "    get => _${2:name};",
      "    set",
      "    {",
      "        if (_${2:name} == value) return;",
      "        _${2:name} = value;",
      "        OnPropertyChanged();",
      "    }",
      "}"
    ],
    "description": "ViewModel Property"
  },

  "notify": {
    "prefix": "notify",
    "body": [
      "public event PropertyChangedEventHandler? PropertyChanged;",
      "protected void OnPropertyChanged([CallerMemberName] string? name = null)",
      "    => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));"
    ],
    "description": "INotifyPropertyChanged boilerplate"
  },

  "relaycmd": {
    "prefix": "relaycmd",
    "body": [
      "public ICommand ${1:CommandName} => _${1:CommandName} ??= new RelayCommand(${2:Execute});",
      "private ICommand? _${1:CommandName};"
    ],
    "description": "RelayCommand property"
  },

  "dispose": {
    "prefix": "dispose",
    "body": [
      "public void Dispose()",
      "{",
      "    ${1:// cleanup}",
      "}"
    ],
    "description": "Dispose method"
  },

  "ap": {
    "prefix": "ap",
    "body": [
      "public static readonly DependencyProperty ${1:PropertyName}Property =",
      "    DependencyProperty.RegisterAttached(",
      "        \"${1:PropertyName}\",",
      "        typeof(${2:type}),",
      "        typeof(${3:OwnerType}),",
      "        new PropertyMetadata(${4:default}, On${1:PropertyName}Changed));",
      "",
      "public static void Set${1:PropertyName}(DependencyObject element, ${2:type} value)",
      "{",
      "    element.SetValue(${1:PropertyName}Property, value);",
      "}",
      "",
      "public static ${2:type} Get${1:PropertyName}(DependencyObject element)",
      "{",
      "    return (${2:type})element.GetValue(${1:PropertyName}Property);",
      "}",
      "",
      "private static void On${1:PropertyName}Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)",
      "{",
      "    ${5:// property changed}",
      "}"
    ],
    "description": "AttachedProperty"
  },

  "dp": {
    "prefix": "dp",
    "body": [
      "public static readonly DependencyProperty ${1:PropertyName}Property =",
      "    DependencyProperty.Register(",
      "        nameof(${1:PropertyName}),",
      "        typeof(${2:type}),",
      "        typeof(${3:OwnerType}),",
      "        new PropertyMetadata(${4:default}));",
      "",
      "public ${2:type} ${1:PropertyName}",
      "{",
      "    get => (${2:type})GetValue(${1:PropertyName}Property);",
      "    set => SetValue(${1:PropertyName}Property, value);",
      "}"
    ],
    "description": "DependencyProperty"
  },

  "rxprop": {
    "prefix": "rxprop",
    "body": [
      "public ReactivePropertySlim<${1:type}> ${2:Name} { get; } = new(${3:default});"
    ],
    "description": "ReactivePropertySlim"
  },

  "cmd": {
    "prefix": "cmd",
    "body": [
      "public ReactiveCommandSlim ${1:CommandName} { get; } = new();"
    ],
    "description": "ReactiveCommandSlim"
  },

  "sub": {
    "prefix": "sub",
    "body": [
      "${1:Observable}",
      "    .Subscribe(${2:value} =>",
      "    {",
      "        ${3:// code}",
      "    })",
      "    .AddTo(Disposable);"
    ],
    "description": "Rx Subscribe"
  }
}

保存場所

C:\Users\<User>\AppData\Roaming\Code\User\snippets\xaml.json

WPF XAML スニペット

{
  "Window DataContext": {
    "prefix": "datacontext",
    "body": [
      "<Window.DataContext>",
      "    <${1:MainWindowViewModel}/>",
      "</Window.DataContext>"
    ],
    "description": "Window.DataContext"
  },

  "App Resource Style": {
    "prefix": "appstyle",
    "body": [
      "<Application.Resources>",
      "    <ResourceDictionary>",
      "        <ResourceDictionary.MergedDictionaries>",
      "            <ResourceDictionary Source=\"${1:Resources/Styles.xaml}\"/>",
      "        </ResourceDictionary.MergedDictionaries>",
      "    </ResourceDictionary>",
      "</Application.Resources>"
    ],
    "description": "App.xaml ResourceDictionary Style"
  },

  "StackPanel Horizontal": {
    "prefix": "hstack",
    "body": [
      "<StackPanel Orientation=\"Horizontal\">",
      "    $0",
      "</StackPanel>"
    ],
    "description": "Horizontal StackPanel"
  },

  "Grid Columns": {
    "prefix": "gridcols",
    "body": [
      "<Grid>",
      "    <Grid.ColumnDefinitions>",
      "        <ColumnDefinition Width=\"Auto\"/>",
      "        <ColumnDefinition Width=\"*\"/>",
      "    </Grid.ColumnDefinitions>",
      "",
      "    $0",
      "</Grid>"
    ],
    "description": "Grid with ColumnDefinitions"
  },

  "Grid Rows": {
    "prefix": "gridrows",
    "body": [
      "<Grid>",
      "    <Grid.RowDefinitions>",
      "        <RowDefinition Height=\"Auto\"/>",
      "        <RowDefinition Height=\"*\"/>",
      "    </Grid.RowDefinitions>",
      "",
      "    $0",
      "</Grid>"
    ],
    "description": "Grid with RowDefinitions"
  }
}