当前位置:网站首页 / WPF / 正文

【ay wpf markup】AY XAML应该这样玩-写个计算器【3/18】

时间:2016年07月21日 | 作者 : aaronyang | 分类 : WPF | 浏览: 1493次 | 评论 0

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

DEMO 8    

使用ResourceObject定义个资源对象

ViewModel

<Window x:Class="WpfApplication3.Window1"
        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:WpfApplication3"
        mc:Ignorable="d"
         xmlns:p="http://www.ayjs.net/markup" 
        Title="DEMO1 www.ayjs.net" Height="800" Width="1000" WindowStartupLocation="CenterScreen">
    <Grid Background="White">
        <Grid.Resources>
            <p:ResourceObject x:Key="ViewModel">
                <p:Property PropertyName="Number" Path="999999.999"/>
                <p:Property PropertyName="KeyCommand">
                    <p:Command>
                        var CanExecute()
                        {
                            return true;
                        }
                        var Execute($p)
                        {
                            if ($p == "C") { Number = 0.0; return; }
                            if ($p.CompareTo("0") @gteq 0 @andand $p.CompareTo("9") @lteq 0)
                            {
                                Number = Number * 10.0 + [double].Parse($p);
                                return;
                            }
                            if ($p != "=")
                            {
                                Operator = $p;
                                Operand = Number;
                                Number = 0.0;
                                return;
                            }
                            if (Operator == "+") { Number = Operand + Number; return; }
                            if (Operator == "-") { Number = Operand - Number; return; }
                            if (Operator == "*") { Number = Operand * Number; return; }
                            if (Operator == "/") { Number = Operand / Number; return; }
                            [MessageBox].Show("Unknown operator: " + Operator);
                        }
                    </p:Command>
                </p:Property>
                <p:Property PropertyName="Operand"/>
                <p:Property PropertyName="Operator"/>
            </p:ResourceObject>
            <Style TargetType="Button">
                <Setter Property="FontSize" Value="30"/>
            </Style>
        </Grid.Resources>
        <Grid.DataContext>
            <Binding Path="Value" Source="{StaticResource ViewModel}"/>
        </Grid.DataContext>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Number}"
                       Margin="10" FontSize="30" FontWeight="Bold"
                       TextAlignment="Right" VerticalAlignment="Center"
                       Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4">
                <p:Attached.Operations>
                    <p:CallHandler Path="Loaded => KeyCommand.Execute('C')"/>
                </p:Attached.Operations>
            </TextBlock>
            <Button Content="7" Grid.Row="1" Grid.Column="0" Command="{Binding KeyCommand}" CommandParameter="7"/>
            <Button Content="8" Grid.Row="1" Grid.Column="1" Command="{Binding KeyCommand}" CommandParameter="8"/>
            <Button Content="9" Grid.Row="1" Grid.Column="2" Command="{Binding KeyCommand}" CommandParameter="9"/>
            <Button Content="+" Grid.Row="1" Grid.Column="3" Command="{Binding KeyCommand}" CommandParameter="+"/>
            <Button Content="4" Grid.Row="2" Grid.Column="0" Command="{Binding KeyCommand}" CommandParameter="4"/>
            <Button Content="5" Grid.Row="2" Grid.Column="1" Command="{Binding KeyCommand}" CommandParameter="5"/>
            <Button Content="6" Grid.Row="2" Grid.Column="2" Command="{Binding KeyCommand}" CommandParameter="6"/>
            <Button Content="-" Grid.Row="2" Grid.Column="3" Command="{Binding KeyCommand}" CommandParameter="-"/>
            <Button Content="1" Grid.Row="3" Grid.Column="0" Command="{Binding KeyCommand}" CommandParameter="1"/>
            <Button Content="2" Grid.Row="3" Grid.Column="1" Command="{Binding KeyCommand}" CommandParameter="2"/>
            <Button Content="3" Grid.Row="3" Grid.Column="2" Command="{Binding KeyCommand}" CommandParameter="3"/>
            <Button Content="*" Grid.Row="3" Grid.Column="3" Command="{Binding KeyCommand}" CommandParameter="*"/>
            <Button Content="0" Grid.Row="4" Grid.Column="0" Command="{Binding KeyCommand}" CommandParameter="0"/>
            <Button Content="C" Grid.Row="4" Grid.Column="1" Command="{Binding KeyCommand}" CommandParameter="C"/>
            <Button Content="=" Grid.Row="4" Grid.Column="2" Command="{Binding KeyCommand}" CommandParameter="="/>
            <Button Content="/" Grid.Row="4" Grid.Column="3" Command="{Binding KeyCommand}" CommandParameter="/"/>
        </Grid>
    </Grid>
</Window>

效果演示:

4gif.gif


代码讲解:

CompareTo这都是C#自带的,不要管。

blob.png


blob.png


下面列出了C#的操作符

  public enum Op
    {
        Plus = 1,
        Minus,
        Times,
        Mod,
        Divide,
        Negate,

        AndAnd,
        OrOr,

        And,
        Or,
        Not,

        BitwiseAnd,
        BitwiseOr,
        BitwiseXor,
        BitwiseNot,
        LeftShift,
        RightShift,

        Equals,
        NotEquals,
        LessThan,
        LessThanOrEqual,
        GreaterThan,
        GreaterThanOrEqual,

        Comma,
        Conditional,
        FirstNonNull,
        As,
        Is,
        Equate,
        Compare,
        ToArray,
        GetProperty,
        SetProperty,
        GetItem,
        SetItem,
        New,

        IsNull,
        NotIsNull,
        ToString,
        IsZero,
        NotIsZero,
        GreaterThanZero,
        LessThanZero,
        GreaterThanOrEqualToZero,
        LessThanOrEqualToZero,
        Format,
    }

上面的AndAnd就是  &&符号


翻译成C#就是                            if ($p.CompareTo("0") @gteq 0 @andand $p.CompareTo("9") @lteq 0)

$p这个变量 在0的前面还是后面,因为输入的是数字的话,比如1,就是>=0的  ,此时$p.CompareTo("0")就是大大于9的,这里通过这种方式少了字符串转数字的转换。

$p输入1是在9的前面,就是<0,   那么就是<= @lteq的,所以整句话的意思,小于等于9,大于等于0

如果是类型,使用[]方括号标记。

当初设计的时候,因为<=的这个符号,小于号和大于号在xaml中是特殊符号,所以就取了个规则表示。



关于Expression的Blend行为的命令写法如下

 <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Click">
                                                <cmd:EventToCommand Command="{Binding PrintApplyRecordCommand}"  CommandParameter="3"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>




    

DEMO9   使用Context指定 全局对象

    <Grid>
        <StackPanel Name="ContentHost">
            <Button Content="Add">
                <p:Attached.Operations>
                    <p:CallHandler Context="{Binding ElementName=ContentHost}">
                        <p:CallHandler.Path>
                            Click => Children.Add([Button] { Content = "Click Me!", Opacity = 0.5 })
                        </p:CallHandler.Path>
                    </p:CallHandler>
                </p:Attached.Operations>
            </Button>
        </StackPanel>
    </Grid>

这里CallHander使用Context指定了StackPanel环境,然后Path中 Click事件中,你理解为外面有个容器是StackPanel   直接使用了   Children了









推荐您阅读更多有关于“WPF4.5,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf,目前主maui

招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰

目前在合肥市企迈科技就职

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表