WPF Caliburn 学习笔记(一)Action 续

本文深入探讨了异步行为AsyncAction与依赖属性DependentActions在编程中的应用,通过实例讲解了如何使用AsyncAction特性实现多线程操作,不影响UI交互,并介绍了如何通过依赖属性实现实时更新界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上次的笔记,主要讲的Action。这次讨论下Action的其他两个行为。

AsyncAction

AsyncAction(异步行为)

基于上次的笔记内容,我们在此基础上加一个3秒的时间延迟

       [Preview("CanDivide")]
       public int Divide(int left,int right)
       {
           Thread.Sleep(3000);
           return left / right;
       }

       public bool CanDivide(int left,int right)
       {
           return right != 0;
       }
当我们输入数字,点button后,这时我们拖动窗体,发现窗体不能动,因为现在还在执行中。

我们只要在Divide函数上加个特性就行了。

代码 [Preview("CanDivide")]
       [AsyncAction(BlockInteraction=true)]
       public int Divide(int left,int right)
       {
           Thread.Sleep(3000);
           return left / right;
       }

       public bool CanDivide(int left,int right)
       {
           return right != 0;
       }

现在我们点button后,可以拖动窗体了吧,这里Button按过后不能按咯,发现了没,估计正在执行那个函数吧。

这里它相当于多线程一样,不会影响UI的操作。

我们还可以对结果产生些变化。比如我们 12 / 2得到的是6.可我想要600。

那我们可以加一个CallBack在AsyncAction特性中

 

代码[Preview("CanDivide")]
       [AsyncAction(BlockInteraction=true,Callback="Result")]
       public int Divide(int left,int right)
       {
           Thread.Sleep(3000);
           return left / right;
       }

       public bool CanDivide(int left,int right)
       {
           return right != 0;
       }

       public int Result(int result)
       {
           return result * 100;
       }

结果等到600,这里先执行完Divide(int left,int right) 后调用Result(int result) 。

image

DependentActions

在前面我们通过控件的Name来对它进行操作的,现在我们试下依赖属性的行为。

在Calculator.cs类中。

代码public class Calculator:INotifyPropertyChanged
    {
        private int _left;
        private int _right;
        private int _result;

        public int Left
        {
            get { return _left; }
            set { _left = value; OnPropertyChanged("Left"); }
        }

        public int Right
        {
            get { return _right; }
            set { _right = value; OnPropertyChanged("Right"); }
        }

        public int Result
        {
            get { return _result; }
            set { _result = value; OnPropertyChanged("Result"); }
        }

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

       [Preview("CanDivide")]
       [Dependencies("Left","Right")]
       public void Divide()
       {
           Result= Left / Right;
       }

       public bool CanDivide()
       {
           return Right != 0;
       }

       public event PropertyChangedEventHandler PropertyChanged;
    }


这里我们定义了三个属性,分别实现了OnPropertyChanged,也就是Calculator.cs类要实现INotifyPropertyChanged接口

来触发属性值的改变。

[Dependencies("Left","Right")]这个特性不要忘了加哦~~ 。

具体为什么要加这个属性,把这个问题先记下来。

我们再看.xaml页面

代码<cal:Action.Target>
        <local:Calculator/>
    </cal:Action.Target>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="32*" />
            <RowDefinition Height="279*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" >
            <TextBox Text="{Binding Path=Left, Mode=TwoWay}" Width="150" />
            <TextBlock Margin="5 0"> /</TextBlock>
            <TextBox Text="{Binding Path=Right, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="150"/>
            <TextBlock Margin="5 0"> =</TextBlock>
            <TextBlock Text="{Binding Path=Result}" Width="150"/>
        </StackPanel>
        <StackPanel Grid.Row="1" >
            <Button Content="Divide" 
                    cal:Message.Attach="[Event Click]=[Action Divide]"/>
        </StackPanel>
    </Grid>

在Text上绑定了相应的属性。

可以按F5运行了。

开始:

image
输入后:

image

分母输入0,按钮不能按了:

image

问题记录:

关于特性(attribute)的运用,这方面不太清楚。

附代码

转载于:https://www.cnblogs.com/dingli/archive/2011/03/09/1977797.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值