Home About
WPF , .NET

dotnet コマンドでつくる WPF Application Hello, World! その3 別プロジェクトの参照

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

前回は、ユーザーコントロールを同じプロジェクト内で作成した。 これを今回は、別プロジェクトとして作成し、それをメインのプロジェクトから参照する形にした。

WPF-1

事前準備

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

プロジェクトの作成

目論見として、 MyApp というプロジェクトと MyHelloWorld というユーザーコントロール(ライブラリ)プロジェクトをつくり、 MyApp プロジェクトから MyHelloWorld ユーザーコントロールを使う形にします。

まず、ソリューションディレクトリに該当するこの二つのプロジェクトを入れておくディレクトリ my-app を作成し、 その後、MyApp, MyHelloWorld プロジェクトを作成。

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

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

リファレンスの追加

続いて、MyApp プロジェクトに MyHelloWorldプロジェクトのリファレンスを追加:

dotnet.exe add MyApp/MyApp.csproj reference MyHelloWorld/MyHelloWorld.csproj

この段階で MyApp プロジェクトをビルドして作動を確かめる。

dotnet.exe build MyApp/MyApp.csproj

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

WPF-0

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

MyHelloWorld プロジェクトに HelloWorld! テキストブロックを追加

UserControl1.xaml の Grid要素の子要素として TextBlock 要素を追加:

./MyHelloWorld/UserControl1.xaml:

<UserControl x:Class="MyHelloWorld.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:MyHelloWorld"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock
            FontSize="30"
            HorizontalAlignment="Center"
            VerticalAlignment="Center">Hello, World!</TextBlock>
    </Grid>
</UserControl>

これで UserControl1 の中央に Hello, World! と表示させています。

MyApp に MyHelloWorld の UserControl1 を配置

MyAppの MainWindow に MyHelloWorld の UserControl1 を配置します。 そのためには、MyApp の MainWindow.xaml にそれを追加します。

./MyApp/MainWindow.xmal:

<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"
        xmlns:MyHelloWorld="clr-namespace:MyHelloWorld;assembly=MyHelloWorld"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <MyHelloWorld:UserControl1 />
    </Grid>
</Window>

Window要素の属性に xmlns:MyHelloWorld="clr-namespace:MyHelloWorld;assembly=MyHelloWorld" を追加し、 Grid要素の子要素として MyHelloWorld の UserControl1 要素を追加。

ここで再び MyApp をビルドします。

dotnet.exe build MyApp/MyApp.csproj

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

WPF-1

できた。

まとめ

Visual Studio なしで、コマンドラインとテキストエディタで開発が進められるのでよい。

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