Skip to content

ファイルベース実行例4. File-based Apps で、WinUI 3 デスクトップアプリ を作成する

This content is not available in your language yet.

Page ID: 1114988449001

Last updated:

ファイルベース実行例4.
File-based Apps で、WinUI 3 デスクトップアプリ を作成する

Section titled “ファイルベース実行例4. File-based Apps で、WinUI 3 デスクトップアプリ を作成する”

統合開発環境 Visual Studio が無くとも、極論「 メモ帳 」と「 1つの C# ファイル 」だけで、
WinUI 3 デスクトップアプリ を作って実行する… なんてことも、
File-based Apps で、実際にできてしまいます。
(やや茨の道ですが)。

Program.cs
#!/usr/bin/env dotnet
#:property TargetFramework=net10.0-windows10.0.26100.0
#:property UseWinUI=true
#:property WindowsAppSDKSelfContained=true
#:property WindowsPackageType=None
#:property RuntimeIdentifier=win-x64
#:property Nullable=enable
#:package Microsoft.UI.Reactor@0.1.0-*
using Microsoft.UI.Reactor;
using Microsoft.UI.Reactor.Core;
using static Microsoft.UI.Reactor.Factories;
ReactorApp.Run<App>("WinUI3", width: 320, height: 160);
file class App : Component
{
public override Element Render()
{
var (cnt, setCnt) = UseState(0);
return VStack(12,
TextBlock($"カウント: {cnt}")
.FontSize(20)
.SemiBold(),
HStack(8,
Button("-1", () => setCnt(--cnt)),
Button("リセット", () => setCnt(0)),
Button("+1", () => setCnt(++cnt))
)
)
.Padding(24)
.Backdrop(BackdropKind.Mica);
}
}
使用ライブラリの、ライセンス表記
Microsoft.UI.Reactor
MIT License
Copyright (c) Microsoft Corporation.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
シェル
$> dotnet Program.cs

コンパイル済みの実行ファイル (EXE) ではなく、
C#ソースコードを記述した、ただのテキストファイル 1つ をコマンドからキックしただけですが、
以下のように WinUI 3 のウィンドウが表示されて、ちゃんと問題なく動作します。

先ほどの File-based Apps で WinForms の例 では C# ソースコード 100行弱 もかかっていましたが、
WinUI 3 では 35 行程度で、サクッと実現できてしまいました。