CommunityToolkit.Mvvm8.1 viewmodel使用-舊式寫法(2)

2023-04-09 12:01:10

 

本系列文章導航
  1. https://www.cnblogs.comhttps://www.cnblogs.com/aierong/p/17300066.html
  2. https://github.com/aierong/WpfDemo (自我Demo地址)

 

 

0.說明

CommunityToolkit.Mvvm8.1有一個重大更新的功能:源生成器功能,它極大簡化我們的mvvm程式碼

但是本篇先總結一下原寫法,下篇再總結源生成器功能

 

1.模型定義

必須繼承:ObservableObject

 

2.viewmodel程式碼實現

幾個關鍵點:

SetProperty是給屬性賦值,並且通知更改通知
ButtonClickCommand.NotifyCanExecuteChanged(); //通知命令 已經改變
RelayCommand ButtonClickCommand //定義命令

namespace WpfDemoNet6.Demo
{
    public class DataViewModel1 : ObservableObject
    {
        private string title = "hello";

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                //title = value;
                //PropertyChanged?.Invoke( this , new PropertyChangedEventArgs( "Name" ) );

                //SetProperty 相當與設定值,並且PropertyChanged通知呼叫
                SetProperty( ref title , value );
            }
        }

        private bool isEnabled = false;

        /// <summary>
        /// 是否可以使用
        /// </summary>
        public bool IsEnabled
        {
            get => isEnabled;
            set
            {
                SetProperty( ref isEnabled , value );

                //通知命令 已經改變
                ButtonClickCommand.NotifyCanExecuteChanged();
            }
        }

        /// <summary>
        /// 命令
        /// </summary>
        public RelayCommand ButtonClickCommand
        {
            get;
        }

        public DataViewModel1 ()
        {
            //RelayCommand的第一個引數是命令呼叫語句
            //              第2個引數(可選)是否允許使用
            ButtonClickCommand = new RelayCommand( () =>
            {
                //點選按鈕,修改標題
                Title = "hello(改)";
            } , () =>
            {
                return IsEnabled;
            } );

            ButtonClickCommandPar = new RelayCommand<double>( ( double val ) =>
            {
                Title = $"hello(改):{val}";
            } );
        }


        public RelayCommand<double> ButtonClickCommandPar
        {
            get;
        }
    }
}

 

3.非同步命令

非同步命令會自動控制控制元件的可見性,並且提供一個IsRunning屬性可以判斷非同步是否完成

public DataViewModel1 ()
{
    AsyncButtonClickCommand = new AsyncRelayCommand( RunTxtAsync );
    AsyncButtonParClickCommand = new AsyncRelayCommand<double>( RunTxtParAsync );
}

/*
特別說明:非同步命令會自動控制控制元件的可見性,並且提供一個IsRunning屬性可以判斷非同步是否完成
*/

/// <summary>
/// 命令
/// </summary>
public IAsyncRelayCommand AsyncButtonClickCommand
{
    get;
}

private async Task RunTxtAsync ()
{
    await Task.Delay( 4800 );
    Title = "hello(Task改)";
}


/// <summary>
/// 命令(帶引數的)
/// </summary>
public IAsyncRelayCommand<double> AsyncButtonParClickCommand
{
    get;
}

private async Task RunTxtParAsync ( double val )
{
    await Task.Delay( 4800 );
    Title = $"hello(Task改):{val}";
}
<!--   
特別說明:非同步命令會自動控制控制元件的可見性,並且提供一個IsRunning屬性可以判斷非同步是否完成   
-->
<Button Width="100"
        Height="30"
        Command="{Binding AsyncButtonClickCommand}"
        Content="非同步" />
<TextBlock HorizontalAlignment="Center"
           FontSize="20"
           FontStyle="Italic"
           FontWeight="Bold"
           Foreground="Green"
           Text="loading......"
           Visibility="{Binding AsyncButtonClickCommand.IsRunning, Converter={StaticResource myboolconvert}}" />