LoginSignup
3
1

More than 1 year has passed since last update.

.NET 5 + VB でも Wpf がしたい!

Last updated at Posted at 2020-12-06

はじめに

これは、Visual Basic Advent Calendar 2020の6日目の記事となります。
前回記事で「.NET 5 + VB でも WinForms がしたい!」で、Windows フォームアプリケーションをやりましたので、WPF アプリケーションを今回やります。

昨年に「.NET Core 3.x + VB でも Wpf がしたい!」という記事を書きました。
当時はC#のプロジェクトとVBのプロジェクトを書き換えるという方法で実現させました。

テンプレートにVBがない

.NET 5になり、Windows Forms App(.NET)はプロジェクト テンプレートに追加されたのに、WPF App (.NET) がプロジェクト テンプレートにありません。C#にはもちろんあります。
VBorCsharpIDEテンプレート.png

Visual Basic 2020年の振り返り」のコメント欄にて、@tfukumori さんから下記の情報をいただきました。

こちらを見ると、Visual Basicでの.NET 5.0のWPFについて記載がありました。
https://docs.microsoft.com/ja-jp/dotnet/core/dotnet-five#visual-basic-updates

以下のコマンドでVisual Basicの.NET 5.0のWPFのプロジェクトが作成され、Visual Studio 2019で開いて実行できることは確認できました。

dotnet new wpf -lang VB

IDEからVisual Basic の.NET 5のWPFプロジェクトを追加できないことに対して、Microsoftのデベロッパーコミュニティで報告はされているようです。
https://developercommunity.visualstudio.com/content/problem/1254553/net-wpf-visual-basic-template.html

テンプレート追加

【2021/08/21追記】
Visual Studo 2019 Version 16.10.0に更新しました。※Version 16.09.0の時は未確認です。
WPF アプリケーションがプロジェクト テンプレートに追加されているのを確認しました。
赤枠が.NET 5、青枠が.NET Frameworkです。
image.png

環境

  • Visual Studo 2019 Version 16.8.0→16.10.0(2021/08/21)
  • .NET 5

プロジェクトの作成

テンプレートはないですが、下記のコマンドで作成することはできます。
プロジェクト名を「VBWpfApp」で作成しました。

dotnet new wpf -n VBWpfApp -lang VB

生成結果

>dotnet new wpf -n VBWpfApp -lang VB

.NET 5.0 へようこそ!
---------------------
SDK バージョン: 5.0.100

テレメトリ
---------
.NET ツールは、エクスペリエンスの向上のために利用状況データを収集します。データは Microsoft によって収集され、コミュニティと共有されます。テレメトリをオプトアウトするには、好みのシェルを使用して、DOTNET_CLI_TELEMETRY_OPTOUT 環境変数を '1'  または 'true' に設定できます。

.NET CLI ツールのテレメトリの詳細をご覧ください: https://aka.ms/dotnet-cli-telemetry

----------------
ASP.NET Core の HTTPS 開発証明書をインストールしました。
証明書を信頼するには、'dotnet dev-certs https --trust' (Windows および macOS のみ) を実行します。
HTTPS の詳細については、https://aka.ms/dotnet-https を参照してください
----------------
最初のアプリを作成するには、https://aka.ms/dotnet-hello-world を参照してください
最新情報については、https://aka.ms/dotnet-whats-new を参照してください
ドキュメントを探索するには、https://aka.ms/dotnet-docs を参照してください
GitHub で問題の報告とソースの検索を行うには、https://github.com/dotnet/core を参照してください
'dotnet --help' を使用して使用可能なコマンドを確認するか、https://aka.ms/dotnet-cli にアクセスしてください
--------------------------------------------------------------------------------------
Getting ready...
The template "WPF Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on VBWpfApp\VBWpfApp.vbproj...
  復元対象のプロジェクトを決定しています...
  C:\Workspace\source\repos\VBWpfApp\VBWpfApp.vbproj を復元しました (96 ms)。
Restore succeeded.

VBWpfApp1.png

※キャプチャ画像のプロジェクト名が「VBWpfAp」と最後pが足りません。記事を書く時に間違いに気がつきました。

ソースコード

VBWpfApp2.png

テキストボックスとボタンを配置した画面になります。

MainWindow.xaml
<Window x:Class="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:VBWpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="280" Width="430">
    <Grid>
        <TextBox x:Name="testText" Margin="5,66,5,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Height="22"/>
        <Button Content="Button" HorizontalAlignment="Left" Height="27" Margin="162,135,0,0" VerticalAlignment="Top" Width="96" RenderTransformOrigin="0.467,-0.146" Click="Button_Click"/>

    </Grid>
</Window>

ボタンイベントでテキストボックスに"Hello! .NET 5"をセットします。

MainWindow.xaml.vb
Class MainWindow
    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        testText.Text = "Hello! .NET 5"
    End Sub
End Class

結果

ボタンをクリックすると、テキストボックスに"Hello! .NET 5"がセットされました。

最後に

IDEからVisual Basic の.NET 5のWPFプロジェクトを追加できないことに対して、Microsoftのデベロッパーコミュニティで報告はされているので、そのうち追加されると思います。→Version 16.10.0で追加されているのを確認しました。
https://developercommunity.visualstudio.com/content/problem/1254553/net-wpf-visual-basic-template.html

なんで、忘れられてしまったんでしょう、Microsoftさんしっかりしてくださいよ。

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1