2021-09-15に更新

WPFのViewModelCommandって何なんだぜ(^~^)?

ぱぎゃー(^◇^) ぷんす(^~^) 公開下書き

WPFのViewModelCommandって何なんだぜ(^~^)?

ramen-tabero-futsu2.png
「 ViewModelCommand 知ってないと 詰む……」

kifuwarabe-futsu.png
「 そんな大事なものの 説明が どこにあるんだぜ?」

ohkina-hiyoko-futsu2.png
「 困っていて 探していたら たまたま 見つけたのよ」

前回の話し

📖 WPFって何だぜ(^~^)?

気の早い人向け

📖 wpf-view-model-command-practice - Git hub に上げたもの

今回の話し

20210914wpf32.png

ramen-tabero-futsu2.png
「 👆 プロジェクト作成後、速攻 Livetを NuGet でインストール」

20210914wpf33a1.png

ramen-tabero-futsu2.png
「 👆 ViewsViewModels フォルダーを作れだぜ」

20210914wpf34a1.png

ramen-tabero-futsu2.png
「 👆 MainWindow.xamlViews フォルダーの下に移動しろだぜ。
それとともに MainWindow.xaml の冒頭の x:Class="" のとこのパスに .Views を足しとけだぜ」

20210914wpf35a1.png

ramen-tabero-futsu2.png
「 👆 MainWindow.xaml.cs の方も namespaceに .Views を足しとけだぜ」

20210914wpf36a1.png

ramen-tabero-futsu2.png
「 👆 App.xaml の StartupUri の方も Views/ を足しとけだぜ」

20210914wpf37.png

namespace WpfViewModelCommandPractice.ViewModels
{
    using Livet.Commands;
    using System;

    public class MainWindowModel
    {
        private ViewModelCommand _calledHello;

        /// <summary>
        /// ハローと表示するコマンド
        /// </summary>
        public ViewModelCommand CalledHello
        {
            get
            {
                if (_calledHello == null)
                {
                    _calledHello = new ViewModelCommand(Hello);
                }
                return _calledHello;
            }
        }

        /// <summary>
        /// ハローと表示します
        /// </summary>
        public void Hello()
        {
            Console.WriteLine("Hello, world!!");
        }
    }
}

ramen-tabero-futsu2.png
「 👆 ViewModel の方は こんな感じでクラスを作れだぜ。
ViewModelCommand のコンストラクタが メソッドを受け取れるのが分かるな」

ohkina-hiyoko-futsu2.png
「 メソッドを プロパティに閉じ込めたのね」

20210914wpf38a1.png

<Window x:Class="WpfViewModelCommandPractice.Views.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:WpfViewModelCommandPractice"
        xmlns:viewModels="clr-namespace:WpfViewModelCommandPractice.ViewModels"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <!-- ViewModel を、Windowに紐づけます -->
    <Window.DataContext>
        <viewModels:MainWindowModel/>
    </Window.DataContext>

    <Grid>
        <Button Width="100" Height="100"
                Content="ハロー"
                Command="{Binding CalledHello}"/>
    </Grid>
</Window>

ramen-tabero-futsu2.png
「 👆 そして ビューの方のデザインは こんな感じ……、あれ? ボタンが機能しね?」

kifuwarabe-futsu.png
「 Console.WriteLine(...) が働いてないのかだぜ?」

20210914wpf39a1.png

namespace WpfViewModelCommandPractice.ViewModels
{
    using Livet.Commands;
    using System;
    using System.Diagnostics;

    public class MainWindowModel
    {
        private ViewModelCommand _calledHello;

        /// <summary>
        /// ハローと表示するコマンド
        /// </summary>
        public ViewModelCommand CalledHello
        {
            get
            {
                if (_calledHello == null)
                {
                    _calledHello = new ViewModelCommand(Hello);
                }
                return _calledHello;
            }
        }

        /// <summary>
        /// ハローと表示します
        /// </summary>
        public void Hello()
        {
            // Console.WriteLine("Hello, world!!"); // デバッグ中は働かなかった
            Trace.WriteLine("Hello, world!!"); // デバッグ中に働いた
        }
    }
}

ramen-tabero-futsu2.png
「 👆 Console を Trace に変えたら 動いたぜ。デバッグで動きを確認するだけなんで これで十分だぜ」

ramen-tabero-futsu2.png
「 これで View から、 ViewModel にあるメソッドを呼び出す指示を書けたな」

ohkina-hiyoko-futsu2.png
「 コードビハインドの ロジックの方に ロジックを書き込みたくないという意地を感じるわね」

kifuwarabe-futsu.png
「 コマンドを使い回したいんだろうけど」

何度でもクリック!→

むずでょ

光速のアカウント凍結されちゃったんで……。ゲームプログラムを独習中なんだぜ☆電王戦IIに出た棋士もコンピューターもみんな好きだぜ☆▲(パソコン将棋)WCSC29一次予選36位、SDT5予選42位▲(パソコン囲碁)AI竜星戦予選16位

Crieitは個人で開発中です。 興味がある方は是非記事の投稿をお願いします! どんな軽い内容でも嬉しいです。
なぜCrieitを作ろうと思ったか

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください!

ボードとは?

むずでょ の最近の記事