Home About
WPF , .NET

dotnet コマンドでつくる WPF Application HelloWorld! その1

WPFアプリケーションを Visual Studio を使わないで、dotnet コマンドとエディタだけでつくる覚え書き。

WPF-1

事前準備

Windows11 WSL 上で作業します。 dotnet 5.0 SDK などはインストール済であることが前提。

MyApp プロジェクトの作成

ソリューションに該当するディレクトリ my-app を作成する。 その中に、MyApp プロジェクトを作成する。

mkdir my-app
cd my-app
dotnet.exe new wpf --target-framework-override net5.0 --name MyApp

WSL上で作業しているので、dotnet コマンドには exe をつけて実行する必要あり。

この段階での my-app ディレクトリ以下のプロジェクト構成を確認します。

.
└── MyApp
    ├── App.xaml
    ├── App.xaml.cs
    ├── AssemblyInfo.cs
    ├── MainWindow.xaml
    ├── MainWindow.xaml.cs
    └── MyApp.csproj

それでは、MyApp プロジェクトをビルドして作動を確かめます。

dotnet.exe build MyApp/MyApp.csproj

./MyApp/bin/Debug/net5.0-windows/MyApp.exe が生成されるので、これを実行。

WPF-0

空の状態のウインドウが表示されました。

MainWindow に TextBlock を追加

MainWindow.xaml に追記します。

MainWindow.xaml:

<Window x:Class="MyApp.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:MyApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock
            FontSize="30"
            HorizontalAlignment="Center"
            VerticalAlignment="Center">Hello, World!</TextBlock>
    </Grid>
</Window>

Grid 要素の子要素として TextBlock 要素を配置して Hello, World! しています。

それでは、再び MyApp をビルドします。

dotnet.exe build MyApp/MyApp.csproj

./MyApp/bin/Debug/net5.0-windows/MyApp.exe が生成されるので、これを実行。

WPF-1

できた。

Liked some of this entry? Buy me a coffee, please.