RX理论学习
- 园子里比较好的系列:七篇文章
- 英文学习资料:下载了几篇已打包
- 官网:http://reactiveui.net/
- github:https://github.com/reactiveui
RX实战
先看效果(当密码与确认密码相同时,button自动Abled,不满足条件Enabled):
前端XAML代码很一般:
xaml
ViewModel
<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>
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 }
设置变量格式:
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(); }
及时刷新
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的作用是及时刷新,若不加该behaviour,则鼠标焦点切换时才触发
出处:http://www.cnblogs.com/luqixinhe/
作者:许欣欣
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。