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

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函数上加个特性就行了。

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特性中

 

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) 。

WPF Caliburn 学习笔记(一)Action 续

DependentActions

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

在Calculator.cs类中。

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运行了。

开始:

WPF Caliburn 学习笔记(一)Action 续
输入后:

WPF Caliburn 学习笔记(一)Action 续

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

WPF Caliburn 学习笔记(一)Action 续

问题记录:

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

附代码

相关文章: