C#WPF界面MVVM框架下异步命令的启动和停止及绑定

4 篇文章 0 订阅
2 篇文章 0 订阅

框架采用CommunityToolkit.Mvvm,界面如下

 View的XAML代码

<Window x:Class="WpfApp6.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:WpfApp6"
        xmlns:FF="clr-namespace:WpfApp6"  
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="300">

    <Grid>
        <StackPanel Height="200" Background="AliceBlue" Orientation="Vertical"   Width="300" >
            <TextBlock Name="Lb" Text="{Binding myMessage.Message,Mode=OneWay}" Height="40" Background="LightCoral"/>
            <Button x:Name="btnclick" Content="Run"  Background="Gold" Command="{Binding ExecCommand }" Height="40" BorderThickness="3,2,3,2"/>
            <CheckBox Content="按钮状态" IsChecked="{Binding  ExecCommand.IsRunning,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
            <Button x:Name="btnCancelclick" Content="Stop"  Background="Gold" Command="{Binding CancleExecCommand }" Height="40" BorderThickness="3,2,3,2"/>
        </StackPanel>
    </Grid>
</Window>

ViewModel如下

// Ignore Spelling: App

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;

using System;
using System.Threading;
using System.Threading.Tasks;

namespace WpfApp6
{

    public class MainWinViewModel : ObservableRecipient, IRecipient<MyMessage>
    {
        public MainWinViewModel()
        {
            this.IsActive = true; 
            ExecCommand = new AsyncRelayCommand(ExecAsync);
            CancleExecCommand = new RelayCommand(ExecCommand.Cancel);
        }
        public void Receive(MyMessage message)
        {
            myMessage.Message = $"Num={message.Id},Str={message.Message}";
        }
        public MyMessage myMessage { get; set; }=new MyMessage() { Id =1, Message="原始信息"};
        public AsyncRelayCommand ExecCommand { get; }
        private  async Task ExecAsync( CancellationToken cancellationToken)
        {
            while (true)
            {
                if (cancellationToken.IsCancellationRequested)
                    return;
                await Task.Delay(1);
                WeakReferenceMessenger.Default.Send(new MyMessage { Id = 123, Message = DateTime.Now.ToString() });
            } 
        }
        public RelayCommand CancleExecCommand { get; set; }

    }
}

Model如下:

using CommunityToolkit.Mvvm.ComponentModel;

namespace WpfApp6
{
    public partial class MyMessage : ObservableObject
    {
        [ObservableProperty]
        private int? id;
        [ObservableProperty]
        private string? message;  
    }
}

ViewModel和View的连接:

 public partial class MainWindow : Window
 {
     public MainWindow()
     {
         InitializeComponent();
         DataContext = new MainWinViewModel();//建立viewmodel和view的联系
     }
 }

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值