C# 在VS 2010中编译时未触发的反应UI
我用一个简单的例子来使用反应式UI 我希望在用户在文本框中键入正确的短语时启用按钮的命令。我已按如下方式设置视图模型:C# 在VS 2010中编译时未触发的反应UI,c#,visual-studio-2010,visual-studio-2012,msbuild,reactiveui,C#,Visual Studio 2010,Visual Studio 2012,Msbuild,Reactiveui,我用一个简单的例子来使用反应式UI 我希望在用户在文本框中键入正确的短语时启用按钮的命令。我已按如下方式设置视图模型: public class MainWindowViewModel : ReactiveObject, IMainWindowViewModel { private string _text; private bool? _isCorrect; private IReactiveCommand _closeCommand; private read
public class MainWindowViewModel : ReactiveObject, IMainWindowViewModel
{
private string _text;
private bool? _isCorrect;
private IReactiveCommand _closeCommand;
private readonly IObservable<bool> _canClose;
public MainWindowViewModel()
{
_canClose = this.WhenAny(c => c.IsCorrect, (c) =>
{
// this fires once on initialization if compiled in VS 2010.
return c.Value == true;
});
//var canClose = this.WhenAny(m => m.Text, c =>
// {
// return "Correct Text".Equals(c.Value);
// });
this.CloseCommand = new ReactiveCommand( _canClose);
this.CloseCommand.Subscribe(c => Debug.WriteLine("Ok pressed"));
}
public IReactiveCommand CloseCommand
{
get { return _closeCommand; }
set { this.RaiseAndSetIfChanged(ref _closeCommand, value); }
}
public string Text
{
get { return _text; }
set
{
this.RaiseAndSetIfChanged(ref _text, value);
CheckTextStatus();
}
}
public bool? IsCorrect
{
get { return _isCorrect; }
set { this.RaiseAndSetIfChanged(ref _isCorrect, value); }
}
private void CheckTextStatus()
{
this.IsCorrect = null;
if (Text.Equals("Correct Text"))
{
this.IsCorrect = true;
}
else if (!"Correct Text".StartsWith(Text))
{
this.IsCorrect = false;
}
}
}
public类MainWindowViewModel:ReactiveObject,IMainWindowViewModel
{
私有字符串_文本;
私人住宅是正确的;
私有IReactiveCommand\u closeCommand;
私有只读IObservable\u canClose;
公共主窗口视图模型()
{
_canClose=this.WhenAny(c=>c.IsCorrect,(c)=>
{
//如果在VS 2010中编译,则在初始化时触发一次。
返回c.Value==true;
});
//var canClose=this.WhenAny(m=>m.Text,c=>
// {
//返回“正确文本”。等于(c值);
// });
this.CloseCommand=新的反应命令(\u canClose);
Subscribe(c=>Debug.WriteLine(“按下Ok”);
}
公共IReactiveCommand关闭命令
{
获取{return\u closeCommand;}
设置{this.RaiseAndSetIfChanged(ref _closeCommand,value);}
}
公共字符串文本
{
获取{return\u text;}
设置
{
此.RaiseAndSetIfChanged(参考文本,值);
CheckTextStatus();
}
}
公立学校是正确的
{
获取{return\u isCorrect;}
设置{this.RaiseAndSetIfChanged(ref_isCorrect,value);}
}
私有void CheckTextStatus()
{
this.IsCorrect=null;
if(Text.Equals(“正确文本”))
{
this.IsCorrect=true;
}
否则如果(!“更正文本”。开始使用(文本))
{
this.IsCorrect=false;
}
}
}
我使用的是反应式UI 4.6,因为我们必须支持XP(.NET 4.0)一段时间
当任何仅在初始化时触发一次,而当属性更改时,\u将关闭
但是,这种行为仅在VS 2010中编译时发生。如果我在VS2012或使用MSBuild进行编译,它将正常工作
我假设它与BCL.target MSBuild任务有关,但不确定到底发生了什么。这是因为[CallerMethodName]
仅在通过VS2012编译时有效,如果要在VS2010上编译,则需要使用另一个重载RaiseAndSetIfChanged
。比如:
bool? _IsCorrect; // The naming is Significant
public bool? IsCorrect {
get { return _IsCorrect; }
set { this.RaiseAndSetIfChanged(x => x.IsCorrect, value); }
}
这是因为[CallerMethodName]
仅在通过VS2012编译时有效,如果要在VS2010上编译,则需要使用不同的RaiseAndSetIfChanged
重载。比如:
bool? _IsCorrect; // The naming is Significant
public bool? IsCorrect {
get { return _IsCorrect; }
set { this.RaiseAndSetIfChanged(x => x.IsCorrect, value); }
}
然而,这在RXUI5.0中是不推荐的,对吗?我们应该坚持使用this.RaiseAndSetIfChanged(ref\u isCorrect,value)
进行未来验证(并摆脱VS2010)?如果你能摆脱VS2010,那绝对是最好的选择:)因为RxUI 5.0仅是.NET 4.5+,它可以依赖[CallerMethodName]
工作,所以它不推荐所有的替代方案,除了原始方法raisePropertyChang[ing/ed]
(您实际上应该只在工具或其他codegen场景中使用)之外,RxUI 5.0中不推荐这种方法,对吗?我们应该坚持使用this.RaiseAndSetIfChanged(ref\u isCorrect,value)
进行未来验证(并摆脱VS2010)?如果你能摆脱VS2010,那绝对是最好的选择:)因为RxUI 5.0仅是.NET 4.5+,它可以依赖[CallerMethodName]
工作,所以它不推荐所有的替代方案,除了原始方法raisePropertyChang[ing/ed]
,您实际上应该只在工具或其他codegen场景中使用这些方法