インチキITポエマーもりゃきの静的サイト

盛っても焼いても終わってるもりゃきのブログです。 比較的プログラミング寄りの話が最近増えています、といってもほぼ C# です。 インチキITポエマーなんで、信憑性についてはあまり期待しないでください。いじめないでね。

このサイト記事については、Fedibirdアカウント経由でお願いね、MastodonなりMisskeyなりで何とかなると思うから、良いよFediverse世界。

Fediverse世界にどうしても足を踏み入れたくない人のためにメアドも置いておくけど、あまりに過度な要求はしないでくださいね。

WPFにおけるPrism集中講座(7) ラストスパート

22-ConfirmCancelNavigation Descriptionには Use the IConfirmNavigationReqest interface to confirm or cancel navigation とあります。 実際に実行したら、ViewA と ViewB を切り替えるボタンがありますが… ViewA から ViewB に切り替える時にはメッセージボックスが出るのに、 ViewB から ViewA に切り替える時にメッセージボックスが出ない、え?これ欠陥品じゃないの? ああ、ソースコードを読んだら納得しました。 ModuleA 内の ViewModels\ViewAViewModel.cs using System.Windows; namespace ModuleA.ViewModels { public class ViewAViewModel : BindableBase, IConfirmNavigationRequest { public ViewAViewModel() { } public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback) { bool result = true; if (MessageBox.Show("Do you to navigate?", "Navigate?", MessageBoxButton.YesNo) == MessageBoxResult.No) result = false; continuationCallback(result); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { } } } ModuleA 内の ViewModels\ViewBViewModel....

2025年01月21日 · (2025年01月22日 更新) · 4 分 · もりゃき

WPFにおけるPrism集中講座番外編:16-RegionContextをCommunityToolkit.Mvvmでリファクタリングする

まずは下準備! はーい、またやってきました!CommunityToolkit.Mvvm大好きの、もりゃきお姉さんだよ! 今回はパパも、基本的な画面構成以外は好きにやって良いとお許しがでましたので、バンバン変えていきますよ! プロジェクトを作りましょう。 Visual Studioで「WPFアプリケーション」を選んで、ソリューション名は「BusinessPersonsSample」、フレームワークは「.NET 9」選択ね! 次に、NuGet経由で「CommunityToolkit.Mvvm」と「Microsoft.Extensions.DependencyInjection」をいつもの通りにインストールしましょうね! 繰り返すけど、CommunityToolkit.Mvvm V8 を使うから、エラーが出たらとりあえずリビルドしてね、お姉さんとの約束よ! DI の設定よ はい、これを App.xaml.cs に適切にコピペしちゃいましょう! public partial class App : Application { /// <summary> /// サービスの登録をします /// </summary> public App() { Services = ConfigureServices(); Ioc.Default.ConfigureServices(Services); } /// <summary> /// 現在の App インスタンスを取得します /// </summary> public new static App Current => (App)Application.Current; /// <summary> /// サービスプロバイダです /// </summary> public IServiceProvider Services { get; } /// <summary> /// サービスを登録します /// </summary> /// <returns></returns> private static ServiceProvider ConfigureServices() { var services = new ServiceCollection(); services....

2025年01月20日 · (2025年01月22日 更新) · 3 分 · もりゃき

WPFにおけるPrism集中講座(6) EventAggregatorとregionを使う

14-UsingEventAggregator Description には Using the IEventAggregator とあります。 まんま過すぎですね。 動かしてみると、TextBox に入力した内容を「Send Message」で送ると、 ListBox に追加されるというサンプルのようです。 UsingEventAggregator 内の App.xaml.cs using System.Windows; using UsingEventAggregator.Views; namespace UsingEventAggregator { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : PrismApplication { protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { } protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) { moduleCatalog.AddModule<ModuleA.ModuleAModule>(); moduleCatalog.AddModule<ModuleB.ModuleBModule>(); } } } UsingEventAggregator 内の Views\MainWindow.xaml <Window x:Class="UsingEventAggregator.Views.MainWindow" xmlns="http://schemas....

2025年01月19日 · (2025年01月22日 更新) · 11 分 · もりゃき

WPFにおけるPrism集中講座番外編:Prismの複雑怪奇なCommand周りをCommunityToolkit.Mvvmで実装する

まずはプロジェクトの作成と整備ね さあ、CommunityToolkit.Mvvm 大好きな、もりゃきお姉ちゃんがやってきましたよ! ますは、サクサクプロジェクトを作りましょう。 Visual Studioで「WPFアプリケーション」を選んで、ソリューション名は「TabCommandSample」、フレームワークは「.NET 9」選択でいいでしょう! 次に、NuGet経由で「CommunityToolkit.Mvvm」と「Microsoft.Extensions.DependencyInjection」をいつもの通りにインストールしましょうね! あ、CommunityToolkit.Mvvm V8 を使うから、エラーが出たらとりあえずリビルドしてね、お姉さんとの約束よ! 次は DI の設定よ!こんなの App.xaml.cs に書くだけなんだから、たくさんアプリケーション作る人はコピペできるようにしとくのが賢い子☆ public partial class App : Application { /// <summary> /// サービスの登録をします /// </summary> public App() { Services = ConfigureServices(); Ioc.Default.ConfigureServices(Services); } /// <summary> /// 現在の App インスタンスを取得します /// </summary> public new static App Current => (App)Application.Current; /// <summary> /// サービスプロバイダです /// </summary> public IServiceProvider Services { get; } /// <summary> /// サービスを登録します /// </summary> /// <returns></returns> private static ServiceProvider ConfigureServices() { var services = new ServiceCollection(); services....

2025年01月18日 · (2025年01月22日 更新) · 4 分 · もりゃき

WPFにおけるPrism集中講座(5) ViewModelとCommandを使う

08-ViewModelLocator Descriptionには using the ViewModelLocator と書かれています。 このサンプルでは、アプリケーションのタイトルが変更されます。 Views\MainWindow.xaml <Window x:Class="ViewModelLocator.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" Title="{Binding Title}" Width="525" Height="350" prism:ViewModelLocator.AutoWireViewModel="True"> <Grid> <ContentControl prism:RegionManager.RegionName="ContentRegion" /> </Grid> </Window> ViewModels\MainWindowViewModel.cs namespace ViewModelLocator.ViewModels { public class MainWindowViewModel : BindableBase { private string _title = "Prism Unity Application"; public string Title { get { return _title; } set { SetProperty(ref _title, value); } } public MainWindowViewModel() { } } } まあ、これ CommunityToolkit.Mvvm の覚え書き をきちんと読んでくれた人なら「ほとんど同じ」となりますよね? CommunityToolkit.Mvvm では ObservableObject だったところが BindingBase になっているだけです。...

2025年01月17日 · (2025年01月22日 更新) · 7 分 · もりゃき

WPFにおけるPrism集中講座(4) ソリューション内のプロジェクトを利用する

07-Modules-AppConfig Description には Load modules using an App.config file と書かれています。 さて、これは何でしょうね?実行したら「View A」と表示されます。 ソリューションを見てみると、プロジェクト「Modules」と「ModuleA」があります。 おそらく「Modules」から「ModuleA」のView を使っているのでしょう。 App.config <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf" /> </configSections> <startup> </startup> <modules> <module assemblyFile="ModuleA.dll" moduleType="ModuleA.ModuleAModule, ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleAModule" startupLoaded="True" /> </modules> </configuration> 更新されたファイル単体では分かりにくいので、06-ViewActivationDeactivation の App.config を引用してみます。 <?xml version="1.0" encoding="utf-8"?> <configuration> <startup> </startup> </configuration> はい、見事に空っぽですね。 実際 ModuleA フォルダを bin を基準に掘っていくと ModuleA.dll があります。 では ModuleA にある肝心の変更部分を見てみましょう。 ModuleA\ModuleAModule.cs using ModuleA.Views; namespace ModuleA { public class ModuleAModule : IModule { public void OnInitialized(IContainerProvider containerProvider) { var regionManager = containerProvider....

2025年01月16日 · (2025年01月22日 更新) · 3 分 · もりゃき

WPFにおけるPrism集中講座(3) 試しにViewのサンプルを書いてみる

プロジェクトの下準備 まず、MainWindow からメニューで「設定」を開いて、設定画面から「設定終了」ボタンで MainWindow に戻るサンプルです。 新規ソリューションを「WPFアプリケーション」で作成します。.NET 9 を利用すれば良いでしょう。 ここでは SimplePrismViewSample という名前にしています。 そして、NuGet から Prism.Unity をインストールします。自分は Visual Studio の NuGet 管理から入れていますが、dotnet 等を扱える人に、改めての説明は不要でしょう。 App.xaml 周辺の設定 Views の設定 ソリューションエクスプローラで Views フォルダを作り、MainWindow.xaml を Views フォルダに移動します。 デフォルトのソリューション直下に MainWindow.xaml を置いていたら、以下の書き換えでエラーを吐きます。 …一体どこに Views が含まれてるんですかね?ブラックボックス臭い… MainWindow.xaml の修正 <Window x:Class="SimplePrismViewSample.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" Title="SimplePrismViewSample" Width="525" Height="350"> <Grid> <ContentControl prism:RegionManager.RegionName="ContentRegion" /> </Grid> </Window> MainWindow.xaml の位置を移動したので、x:Class="SimplePrismViewSample.MainWindow" を x:Class="SimplePrismViewSample.Views.MainWindow" に書き換える必要があります。 App.xaml の記述 App.xaml を以下のように書き換えます。 <prism:PrismApplication x:Class="SimplePrismViewSample.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SimplePrismViewSample" xmlns:prism="http://prismlibrary.com/"> <Application.Resources /> </prism:PrismApplication> App....

2025年01月15日 · (2025年01月22日 更新) · 2 分 · もりゃき

WPFにおけるPrism集中講座(2) region の謎

04-ViewDiscovery Descriptionには Automatically inject views with View Discovery とあります。 やっと動きがありました。とはいえ、アプリケーションを動かしても「View A」と表示されるだけですけどね… App.xaml <prism:PrismApplication x:Class="ViewDiscovery.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ViewDiscovery" xmlns:prism="http://prismlibrary.com/"> <Application.Resources /> </prism:PrismApplication> App.xaml.cs using System.Windows; using ViewDiscovery.Views; namespace ViewDiscovery { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : PrismApplication { protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { } } } MainWindow.xaml <Window x:Class="ViewDiscovery.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" Title="Shell" Width="525" Height="350"> <Grid> <ContentControl prism:RegionManager....

2025年01月14日 · (2025年01月22日 更新) · 3 分 · もりゃき

WPFにおけるPrism集中講座(1) 序盤のサンプルについて

Prismについて 正直、この記事を読んでいる方には、今さら説明は不要でしょう。 特にこの記事はWPFを中心に書いているため、WPFとPrismといえばMVVMのためのライブラリ。そのPrismについて徹底解説をしていこうと思います。 PrismLibrary/Prism-Samples-Wpf 01-BootstrapperShell 正直、このサンプルに戸惑った人は多いのではないでしょうか? Descriptionoには Create a basic bootstrapper and shell と書かれています。 初期プロジェクトから書き換え、追記されているのは以下の通りです。 App.xaml <Application x:Class="BootstrapperShell.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BootstrapperShell"> <Application.Resources /> </Application> App.xaml.cs using System.Windows; namespace BootstrapperShell { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var bootstrapper = new Bootstrapper(); bootstrapper.Run(); } } } MainWindow.xaml <Window x:Class="BootstrapperShell.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Shell" Width="525" Height="350"> <Grid> <ContentControl Content="Hello from Prism" /> </Grid> </Window> Bootstrapper....

2025年01月13日 · (2025年01月22日 更新) · 2 分 · もりゃき

カップ麺シリーズ:CommunityToolkit.Mvvm V8の覚え書き

前提条件 まずは以下の記事内容を踏まえてください。 カップ麺シリーズ:一から学べるWPF における CommunityToolkit.Mvvm(MVVM ToolKit) 覚え書き(1) 簡単な依存性注入とデータバインディング カップ麺シリーズ:一から学べる、WPF における CommunityToolkit.Mvvm(MVVM ToolKit) 覚え書き(2) 依存性注入を利用した簡単なサンプル カップ麺シリーズ:一から学べるWPF における CommunityToolkit.Mvvm(MVVM ToolKit) 覚え書き(3) メッセージングの簡単なサンプル CommunityToolkit.Mvvm V8について 今回、プロジェクト作成は行いません。覚え書き(1)に沿って、最低限コンパイルできる所まで進めてください。 CommunityToolkit.Mvvm V8 は、ざっくり言うと、繰り返しとなる記述を省略できる仕組みが取り入れられています。 [ObservableProperty] 例えば以下のコード private string _username = string.Empty; public string Username { get => _username; set => SetProperty(ref _username, value); } であれば [ObservableProperty] private string _Username = string.Empty; で済みます。 実際はこの時クラスが partial になり、裏側で同等のコードが生成あされています。コンパイルエラーが出ることもあるので、partial クラスになることは最低限覚えておきましょう。 そして、このようなコード private string _username = string.Empty; public string Username { get => _username; set { SetProperty(ref _username, value); NameCommand....

2025年01月12日 · (2025年01月22日 更新) · 3 分 · もりゃき