61.异步编程1 C#例子 WPF例子

和普通的任务绑定不太相同的部分如下:

cs 复制代码
 public MainWindowViewModel()
 {
     FetchUserInfoCommand = new RelayCommand(async (param) => await FetchUserInfoAsync());
 }

 private async Task FetchUserInfoAsync()
 {
     // 模拟异步操作,比如网络请求
     await Task.Delay(2000); // 模拟网络延迟
     UserInfo = "用户信息:新员工,需要学会WPF的异步,并巩固之前的知识。";
 }
cs 复制代码
        public MainWindowViewModel()
        {
            FetchUserInfoCommand = new RelayCommand(param => FetchUserInfoAsync());
        }

        //private async Task FetchUserInfoAsync()
        private void FetchUserInfoAsync()
        {
            Thread.Sleep(1000);
            UserInfo = "用户信息:新员工,需要学会WPF的异步,并巩固之前的知识。";
        }

前者是异步,后者则是普通的绑定任务。在前台实现的功能完全相同。但是后者却会导致前台卡死。

后台代码:

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace 异步
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private string _userInfo;
        public string UserInfo
        {
            get { return _userInfo; }
            set
            {
                _userInfo = value;
                OnPropertyChanged(nameof(UserInfo));
            }
        }


        public ICommand FetchUserInfoCommand { get; }

        //public MainWindowViewModel()
        //{
        //    //FetchUserInfoCommand = new RelayCommand(async (param) => await FetchUserInfoAsync());

        //    FetchUserInfoCommand = new RelayCommand(param => FetchUserInfoAsync());
        //}

        private async Task FetchUserInfoAsync()
        //private void FetchUserInfoAsync()
        //{
        //    // 模拟异步操作,比如网络请求
        //    //await Task.Delay(2000); // 模拟网络延迟
        //    Thread.Sleep(1000);
        //    UserInfo = "用户信息:新员工,需要学会WPF的异步,并巩固之前的知识。";
        //}

        public MainWindowViewModel()
        {
            FetchUserInfoCommand = new RelayCommand(async (param) => await FetchUserInfoAsync());
        }

        private async Task FetchUserInfoAsync()
        {
            // 模拟异步操作,比如网络请求
            await Task.Delay(2000); // 模拟网络延迟
            UserInfo = "用户信息:新员工,需要学会WPF的异步,并巩固之前的知识。";
        }




        //固定
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }

    public class RelayCommand : ICommand
    {
        private readonly Action<object> _execute;
        public event EventHandler CanExecuteChanged;

        public RelayCommand(Action<object> execute) => _execute = execute;

        public bool CanExecute(object parameter) => true; // 总是可执行(简化)
        public void Execute(object parameter) => _execute(parameter);
    }
}
cs 复制代码
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 异步
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext=new MainWindowViewModel();
        }
    }
}

前台:

XML 复制代码
<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:异步"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <Button Content="获取用户信息" Command="{Binding FetchUserInfoCommand}" Margin="10"/>
            <TextBox Text="{Binding UserInfo, UpdateSourceTrigger=PropertyChanged}" Margin="10,20,10,10" TextWrapping="Wrap" Height="100"/>
        </StackPanel>
    </Grid>
</Window>
相关推荐
say_fall5 分钟前
C语言容易忽略的小知识点(1)
c语言·开发语言
津津有味道5 分钟前
Ntag 424 DNA写入URI网址配置开启动态UID计数器镜像C#源码
c#·uri·ndef·424dna·动态uid·计数器镜像
不会编程的小寒10 分钟前
C++初始继承,继承中构造、析构顺序
开发语言·python
运维管理22 分钟前
Linux系统笔记--Base
开发语言·php
全栈软件开发1 小时前
最新版T5友价互站网源码商城PHP源码交易平台 完整带手机版源码网系统源码
android·开发语言·php
Mos_x1 小时前
关于我们的python日记本
开发语言·python
The Sheep 20231 小时前
Dotnet-Dapper的用法
java·开发语言
马克学长1 小时前
SSM基于Java的医疗器械销售系统oy281(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·开发语言·用户管理·ssm 框架·医疗器械销售系统
lqj_本人2 小时前
Rust与Go:现代系统编程语言的深度对比
开发语言·golang·rust
星释2 小时前
Rust 练习册 :Macros与宏系统
开发语言·后端·rust