【发布时间】:2020-11-04 18:34:39
【问题描述】:
我正在考虑在 shellview 的特定区域向用户显示错误消息。
所以我在我的 ShellViewModel 中实例化 ErrorViewModel 并且 ErrorModel 显示正确。带有 ViewModel 的 Textblock 正在显示它的初始值。
但是,如果我通过来自另一个 ViewModel(例如 LoginViewModel)的公共方法传递字符串,则错误字符串将传递给 ErrorViewModel,并且还会触发 NotifyOfPropertyChange(() => PublishErrorMsg),但 Textblock 没有改变。
看不出为什么文本块的内容不会改变。
错误视图模型:
public class ErrorViewModel : Screen
{
private string publishErrorMsg;
public string PublishErrorMsg
{
get { return publishErrorMsg; }
set
{
publishErrorMsg = value;
NotifyOfPropertyChange(() => PublishErrorMsg);
}
}
public void ShowError(string msg) {
PublishErrorMsg = msg;
// MessageBox.Show(msg);
NotifyOfPropertyChange(() => PublishErrorMsg);
}
}
XAML 错误视图:
<UserControl x:Class="Views.ErrorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Views"
mc:Ignorable="d"
d:DesignHeight="40" d:DesignWidth="800" Background="White">
<Grid>
<DockPanel VerticalAlignment="Bottom" Height="40" Background="#FF474A57">
<TextBlock Text="{Binding PublishErrorMsg}"
Width="200" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FFE69393" FontSize="14">
</TextBlock>
</DockPanel>
</Grid>
跟随触发事件的部分:
public void LogIn() {
this.container.GetInstance<ErrorViewModel>().ShowError("User logged in");
try {
ResetPublishMsg();
ActiveUserModel.CreateIfAuthenticated(UserName, Password);
events.PublishOnUIThread(new LogOnEvent());
}
catch (Exception ex) {
this.container.GetInstance<ErrorViewModel>().PublishErrorMsg = ex.Message;
}
}
感谢您的帮助!
【问题讨论】:
-
要最终确定答案,如果您的原始问题已解决,请记住接受并投票赞成答案,如果您有其他问题,请提出新问题:stackoverflow.com/help/someone-answers
标签: wpf mvvm caliburn.micro