我的应用是一个winforms应用程序,它引用了ReactiveUI 6.5。当用户输入"退出"我试图关闭窗口。在TextBox中,但由于某种原因没有任何反应。
这是我的观点:
public partial class HomeView : Form, IViewFor<HomeViewModel>
{
public HomeView()
{
InitializeComponent();
ViewModel = new HomeViewModel();
this.Bind(ViewModel, x => x.EnteredText, x => x.textBox.Text);
this.ViewModel.WhenAnyObservable(x => x.ExitCmd).Subscribe(_ => this.Close());
}
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (HomeViewModel)value; }
}
public HomeViewModel ViewModel { get; set; }
}
这是我的ViewModel:
public class HomeViewModel : ReactiveUI.ReactiveObject
{
string _text;
public string EnteredText
{
get { return _text; }
set { this.RaiseAndSetIfChanged(ref _text, value); }
}
public ReactiveCommand<object> ExitCmd { get; private set; }
public HomeViewModel()
{
ExitCmd = ReactiveCommand.Create(this.WhenAny(x => x.EnteredText, y => y.Value == "Exit"));
}
}
答案 0 :(得分:1)
在ViewModel中,您应该描述Exit Command和EntertedText之间的关系
public class HomeViewModel : ReactiveUI.ReactiveObject
{
private string _text;
public string EnteredText
{
get { return _text; }
set { this.RaiseAndSetIfChanged(ref _text, value); }
}
public ReactiveCommand<object> ExitCmd { get; private set; }
public HomeViewModel()
{
ExitCmd = ReactiveCommand.Create();
this.WhenAny (x => x.EnteredText, x => x.Value == "Exit")
.Where(k => k == true)
.DistinctUntilChanged()
.InvokeCommand (ExitCmd);
}
}
然后在您看来,您可以简单地订阅它
public partial class HomeView : Form, IViewFor<HomeViewModel>
{
public HomeView()
{
InitializeComponent();
ViewModel = new HomeViewModel();
this.Bind(ViewModel, x => x.EnteredText, x => x.textBox.Text);
this.ViewModel.ExitCmd.Subscribe (_ => this.Close());
}
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (HomeViewModel)value; }
}
public HomeViewModel ViewModel { get; set; }
}
答案 1 :(得分:0)
你几乎得到了它,除了没有告诉你Execute
ReactiveCommand.Create
。您传递给canExecute
的值是ExitCmd = ReactiveCommand.Create();
this.WhenAnyValue(x => x.EnteredText)
.Where(x => x == "Exit")
.InvokeCommand(ExitCmd);
属性。您应该将其更改为:
InvokeCommand
ExecuteAsync
实际执行命令参数,将可观察序列中的每个值作为参数传递给curl
方法。