WPF-MVVM中.NET(Rx)的反应性扩展 [英] Reactive Extensions for .NET (Rx) in WPF - MVVM
问题描述
我在WPF应用程序中使用带有Caliburn.Micro的NET(Rx)反应性扩展.我正在尝试将WPF应用程序移植为使用MVVM体系结构,并且需要监视TextBox控件的Text属性中的更改.
I am using Reactive extensions for NET (Rx) with Caliburn.Micro in my WPF app. I'm trying to port my WPF app to use an MVVM architecture and I need to monitor changes in the Text property of a TextBox control.
如果Text属性的最后一次更改超过3秒钟,则需要调用服务的LoadUser方法.
If the last change of the Text property was more than 3 seconds ago I need to call the LoadUser method of a service.
将逻辑从我的旧解决方案移植到具有MVVM架构的新解决方案.
Porting the logic from my old solution to the new solution with MVVM architecture.
XAML:
<TextBox Name="Nick"
Grid.Row="0"
FontSize="14"
Margin="2,2,2,2"
HorizontalAlignment="Stretch"
TextChanged="Nick_TextChanged" />
在后面的代码中,我有以下内容:
...
Observable.FromEvent<TextChangedEventArgs>(Nick, "TextChanged")
.Select(e => ((TextBox)e.Sender).Text)
.Where(text => text.Length > 3)
.Do(LoadUser)
.Throttle(TimeSpan.FromSeconds(3000))
.Subscribe(LoadUser);
...
private void LoadUser(string text){...}
我想在我的视图模型类中使用Observable.FromEvent.像这样
I would like use Observable.FromEvent in my view model class. Something like this
查看:
<TextBox Name="Nick"
Grid.Row="0"
FontSize="14"
Margin="2,2,2,2"
HorizontalAlignment="Stretch"
Micro:Message.Attach="[TextChanged]=[Action TextChanged()]"/>
查看模型:
[Export(typeof(IAddFriendViewModel))]
public class AddFriendViewModel : Screen, IAddFriendViewModel
{
private string _nick;
public string Nick
{
get { return _nick; }
set
{
_nick = value;
NotifyOfPropertyChange(()=>Nick);
}
}
...
//how can I access to textbox control Nick in view from view model class?
Observable.FromEvent<TextChangedEventArgs>(Nick, "TextChanged")
.Select(e => ((TextBox)e.Sender).Text)
.Where(text => text.Length > 3)
.Do(LoadUser)
.Throttle(TimeSpan.FromSeconds(3000))
.Subscribe(LoadUser);
...
private void LoadUser(string text)
{ }
public void TextChanged()
{
}
}
我的问题是Observable.FromEvent使用TextBox控件,我不知道如何从ViewModel类访问此控件.
My problem is Observable.FromEvent uses the TextBox control and I don’t know how can I access this control from my ViewModel class.
我可以绑定到TextBox的Text属性,也可以将命令绑定到TextBox控件的某些事件,但是此方法使用View中的一个对象.
I can bind to the Text property of TextBox or I can bind a command to some event of the TextBox control, but this method uses an object in the View.
感谢创意.
推荐答案
将Nick
属性添加到ViewModel并实现INotifyPropertyChanged
.那你就可以做到
Add a Nick
property to your ViewModel and implement INotifyPropertyChanged
. Then you can do this
Observable.FromEventPattern<PropertyChangedEventArgs>(this, "PropertyChanged")
.Where(e => e.EventArgs.PropertyName == "Nick")
.Select(_ => this.Nick)
.Where(text => text.Length > 3)
//Subscribe will call LoadUser, no need for the extra Do(...)
.Throttle(TimeSpan.FromSeconds(3000))
.Subscribe(LoadUser);
然后您的XAML就是这样
and then your XAML would be something like this
<TextBox Name="Nick"
Grid.Row="0"
FontSize="14"
Margin="2,2,2,2"
HorizontalAlignment="Stretch"
Text="{Binding Nick, UpdateSourceTrigger=PropertyChanged}" />
这篇关于WPF-MVVM中.NET(Rx)的反应性扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!