WPFでReactiveProperty向けプロジェクトのテンプレート

コンピュータ

WPFのプロジェクトを dotnet.exe で作成する場合、以下のコマンドを実行します。

dotnet new wpf

このコマンドを実行すると、WPFアプリケーションの作成に必要なファイルが自動的に生成されます。

生成されるファイルには、アプリケーションのひな形となる最小構成のプロジェクトが含まれています。

プログラマは、このひな形のファイルを修正・追加しながらアプリケーションを作成していきます。

ここで、生成されるファイルを自分が普段よく使う構成やコードに置き換えておくことで、毎回の初期作業を減らすことができ、開発効率の向上が期待できます。

そこで今回は、ReactivePropertyを使うことを前提とした、WPFアプリのテンプレートプロジェクトを作成してみました。

ファイル名:MyTemplateWpfRx.csproj


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net10.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
    <!-- <ApplicationIcon>Assets/App.ico</ApplicationIcon> -->
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="ReactiveProperty.WPF" Version="9.8.0" />
  </ItemGroup>

	<ItemGroup>
		<Resource Include="Assets\**\*.*" />
	</ItemGroup>

</Project>

ファイル名:App.xaml

<Application x:Class="MyTemplateWpfRx.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyTemplateWpfRx"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <!-- Menu / MenuItem -->
        <Style TargetType="Menu">
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="Padding"  Value="0"/>
            <Setter Property="Margin"   Value="0,0,0,4"/>
        </Style>

        <!-- StatusBar -->
        <Style TargetType="StatusBar">
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="Padding"  Value="0"/>
            <Setter Property="Margin"   Value="0,0,0,4"/>
        </Style>

        <!-- GridSplitter -->
        <Style x:Key="HGridSplitterStyle" TargetType="GridSplitter">
            <Setter Property="Width" Value="5"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="ShowsPreview" Value="True"/>
            <Setter Property="ResizeDirection" Value="Columns"/>
        </Style>

        <!-- TextBlock -->
        <Style x:Key="CenterTextStyle" TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>

    </Application.Resources>
</Application>

ファイル名:MainWindow.xaml

<Window x:Class="MyTemplateWpfRx.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyTemplateWpfRx"
        xmlns:h="clr-namespace:Maywork.WPF.Helpers"
        h:DisposeDataContextBehavior.Enable="True"
        mc:Ignorable="d"
        Title="{Binding Title.Value}" Height="450" Width="800">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!-- メニュー -->
        <Menu Grid.Row="0">
            <MenuItem Header="_File">
                <MenuItem Header="E_xit" Command="{Binding ExitCommand}" />
                <Separator/>
            </MenuItem>
        </Menu>
        <!-- メイン -->
        <Grid Grid.Row="1">
            <!--
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Border Grid.Column="0">
                <TextBlock Text="左側" Style="{StaticResource CenterTextStyle}" />
            </Border>

            <GridSplitter
                Grid.Column="1"
                Style="{StaticResource HGridSplitterStyle}" />
            
            <Border Grid.Column="2">
                <TextBlock Text="右側" Style="{StaticResource CenterTextStyle}" />
            </Border>                
            -->
        </Grid>
        <!-- ステータスバー -->
        <StatusBar Grid.Row="2">
            <StatusBarItem>
                <TextBlock Text="Ready"/>
            </StatusBarItem>
        </StatusBar>
    </Grid>

</Window>

ファイル名:MainWindowViewModel.cs


using System.Diagnostics;
using System.Windows;
using Maywork.WPF.Helpers;
using Reactive.Bindings;
using Reactive.Bindings.Extensions;

namespace MyTemplateWpfRx;
public class MainWindowViewModel : ViewModelBaseRx
{
    // プロパティ
    public ReactivePropertySlim<string> Title { get; private set; }
         = new("");

    // コマンド
    public ReactiveCommandSlim ExitCommand { get; }

    // コンストラクタ
    public MainWindowViewModel()
    {
        // タイトル
        Title.Subscribe(value =>
        {
            Debug.Print($"タイトルの値が{value}に変更された");
        })
        .AddTo(Disposable);

        // 終了
        ExitCommand = new ReactiveCommandSlim()
        .WithSubscribe(()=>
        {
            Application.Current.Shutdown();
        })
        .AddTo(Disposable);
    }
}

Helpers\AppPathHelper.cs
Helpers\BoolToVisibilityConverter.cs
Helpers\DisposeDataContextBehavior.cs
Helpers\ViewModelBaseRx.cs

.gitignore

# Build results
bin/
obj/

# Visual Studio
.vs/
*.user
*.suo
*.userosscache
*.sln.docstates

# Rider
.idea/

# VSCode
.vscode/

# NuGet
packages/
*.nupkg
*.snupkg

# Logs
*.log

# OS
Thumbs.db
.DS_Store

# Test results
TestResults/

# Publish
publish/

# Dotnet
*.runtimeconfig.dev.json

# Temporary files
*.tmp
*.temp

# Coverage
coverage/
*.coverage
*.coveragexml

.template.config\template.json

{
  "$schema": "http://json.schemastore.org/template",
  "author": "maywork",
  "classifications": [ "WPF", "Rx" ],
  "identity": "Maywork.Wpf.RxTemplate",
  "name": "WPF Rx Minimal Template",
  "shortName": "wpfrx-min",
  "sourceName": "MyTemplateWpfRx",
  "tags": {
    "language": "C#",
    "type": "project"
  }
}

テンプレートのインストールコマンド

dotnet new install .

テンプレートを指定してプロジェクトを作成

dotnet new wpfrx-min

プロジェクトを実行例

dotnet run

コメント