The*_*ker 4 wpf mvvm mvvm-foundation
MVVM问题.ViewModel和View之间的消息传递,如何最好地实现?
该应用程序有一些"用户通信"点,例如:"您已为此选择输入了注释.当"是/否/ NA"选择的值发生变化时,您是希望保存还是"丢弃".所以我需要一些被禁止的View绑定到ViewModel的"消息"的方式.
我从MVVM Foundation的Messenger开始走下去.然而,这更像是系统范围的广播,而不是事件/订户模型.因此,如果应用程序有两个View实例(Person1 EditView和Person2 EditView)打开,当一个ViewModel发布"您要保存"消息时,它们都会收到消息.
你用了什么方法?
谢谢安迪
对于所有这些,您将使用绑定作为"通信"的方法.例如,可能会根据ViewModel中设置的属性显示或隐藏确认消息.
这是视图
<Window.Resources>
<BoolToVisibilityConverter x:key="boolToVis" />
</Window.Resources>
<Grid>
<TextBox Text="{Binding Comment, Mode=TwoWay}" />
<TextBlock Visibility="{Binding IsCommentConfirmationShown,
Converter={StaticResource boolToVis}"
Text="Are you sure you want to cancel?" />
<Button Command="CancelCommand" Text="{Binding CancelButtonText}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
这是你的ViewModel
// for some base ViewModel you've created that implements INotifyPropertyChanged
public MyViewModel : ViewModel
{
//All props trigger property changed notification
//I've ommited the code for doing so for brevity
public string Comment { ... }
public string CancelButtonText { ... }
public bool IsCommentConfirmationShown { ... }
public RelayCommand CancelCommand { ... }
public MyViewModel()
{
CancelButtonText = "Cancel";
IsCommentConfirmationShown = false;
CancelCommand = new RelayCommand(Cancel);
}
public void Cancel()
{
if(Comment != null && !IsCommentConfirmationShown)
{
IsCommentConfirmationShown = true;
CancelButtonText = "Yes";
}
else
{
//perform cancel
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不是一个完整的示例(唯一的选择是肯定!:)),但希望这说明您的View和ViewModel几乎是一个实体,而不是两个互相打电话的实体.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
2120 次 |
| 最近记录: |