zoukankan      html  css  js  c++  java
  • RX实例——MVVM

    RX理论学习

    1. 园子里比较好的系列:七篇文章
    2. 英文学习资料:下载了几篇已打包
    3. 官网:http://reactiveui.net/
    4. github:https://github.com/reactiveui

    RX实战

    先看效果(当密码与确认密码相同时,button自动Abled,不满足条件Enabled):

     
    前端XAML代码很一般:
     <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="90*" />
                <ColumnDefinition Width="160*" />
            </Grid.ColumnDefinitions>
            <TextBlock Text="请输入密码:" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" />
            <TextBox Width="150" Height="30" Grid.Column="1" HorizontalAlignment="Left" Text="{Binding Password,Mode=TwoWay}">
                <i:Interaction.Behaviors>
                    <Behaviors:TextBoxUpdatedBehavior>
                    </Behaviors:TextBoxUpdatedBehavior>
                </i:Interaction.Behaviors>
            </TextBox>
            <TextBlock Text="请确认密码:" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Grid.Row="1" />
            <TextBox Width="150" Height="30" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Text="{Binding ConfirmPassword,Mode=TwoWay}">
                <i:Interaction.Behaviors>
                    <Behaviors:TextBoxUpdatedBehavior>
                    </Behaviors:TextBoxUpdatedBehavior>
                </i:Interaction.Behaviors>
            </TextBox>
            <Button Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Center" Width="80" Height="30" Content="确认" Command="{Binding OkCommand}"/>
        </Grid>
    xaml

    ViewModel代码:

     public class MainPageViewModel : ReactiveObject
        {
            public MainPageViewModel()
            {
                OkCommand = new ReactiveCommand(this.WhenAny(x => x.Password,x => x.ConfirmPassword,(password,confirmpassword)=>(password.Value==confirmpassword.Value&&password.Value!=null)));
                OkCommand.Subscribe(_ => OpenChildWindow());
            }
            #region Property
            public ReactiveCommand OkCommand { get; set; }
    
            [IgnoreDataMember]
            public string  _password;
            public string Password
            {
                get { return _password; }
                set { this.RaiseAndSetIfChanged(x => x.Password, value); }
            }
    
            [IgnoreDataMember]
            public string _confirmPassword;
            public string ConfirmPassword
            {
                get { return _confirmPassword; }
                set { this.RaiseAndSetIfChanged(x => x.ConfirmPassword, value); }
            }
            #endregion
        }
    ViewModel
    设置变量格式:
            private void Application_Startup(object sender, StartupEventArgs e)
            {
                Func<string, string> UnderscoreFirstToLowerBehaviour = x =>
                {
                    char[] arr = x.ToCharArray();
                    arr[0] = char.ToLower(arr[0]);
                    return '_' + new String(arr);
                };
                RxApp.GetFieldNameForPropertyNameFunc = UnderscoreFirstToLowerBehaviour;
                this.RootVisual = new MainPage();
            }
    APP.cs

    及时刷新

     public class TextBoxUpdatedBehavior : Behavior<TextBox>
        {
            public TextBoxUpdatedBehavior()
            {
                // Insert code required on object creation below this point.
    
                //
                // The line of code below sets up the relationship between the command and the function
                // to call. Uncomment the below line and add a reference to Microsoft.Expression.Interactions
                // if you choose to use the commented out version of MyFunction and MyCommand instead of
                // creating your own implementation.
                //
                // The documentation will provide you with an example of a simple command implementation
                // you can use instead of using ActionCommand and referencing the Interactions assembly.
                //
                //this.MyCommand = new ActionCommand(this.MyFunction);
            }
    
            protected override void OnAttached()
            {
                base.OnAttached();
                this.AssociatedObject.TextChanged += AssociatedObject_TextChanged;
                // Insert code that you would want run when the Behavior is attached to an object.
            }
            void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
            {
                BindingExpression bindingExpression = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
                if (bindingExpression != null)
                    bindingExpression.UpdateSource();
            }
            protected override void OnDetaching()
            {
                base.OnDetaching();
                this.AssociatedObject.TextChanged -= AssociatedObject_TextChanged;
                // Insert code that you would want run when the Behavior is removed from an object.
            }
    
            /*
            public ICommand MyCommand
            {
                get;
                private set;
            }
             
            private void MyFunction()
            {
                // Insert code that defines what the behavior will do when invoked.
            }
            */
        }
    TextBoxUpdatedBehavior
    TextBoxUpdatedBehavior的作用是及时刷新,若不加该behaviour,则鼠标焦点切换时才触发
  • 相关阅读:
    数学之美
    作为一个程序员,你知道每天自己在做什么吗
    搭建ssm框架log4j日志
    webpack实践——DLLPlugin 和 DLLReferencePlugin的使用
    一些实用的技巧
    详解Vue 开发模式下跨域问题
    vue resource 携带cookie请求 vue cookie 跨域
    解决vue中element组件样式修改无效
    ES6 Promise 异步操作
    js 字符串操作函数
  • 原文地址:https://www.cnblogs.com/luqixinhe/p/3230207.html
Copyright © 2011-2022 走看看