【问题标题】:WPF MVVM Caliburn Displaying all ErrorMsgs in a specific ViewModelWPF MVVM Caliburn 在特定 ViewModel 中显示所有 ErrorMsgs
【发布时间】: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;
        }
    }

感谢您的帮助!

【问题讨论】:

标签: wpf mvvm caliburn.micro


【解决方案1】:

也许您忘记订阅活动了?见EventAggregator

public class ErrorViewModel : Screen, IHandle<LogOnEvent> 
{
        private readonly IEventAggregator _eventAggregator;

        public ErrorViewModel(IEventAggregator eventAggregator) {
            _eventAggregator = eventAggregator;
            _eventAggregator.Subscribe(this);
        }



       public void Handle(LogOnEvent message) {
            // Handle the message here.
        }

}

【讨论】:

  • 好吧,我打算在不通过事件传递 em 的情况下处理 Errormsges。但我已经看到我在上一张照片之前将其称为事件。这只是在设置新值后更新 UI (NotifyOfPropertyChange(() => PublishErrorMsg);)。
  • Hey Frenchy,最后我通过 eventtaggregator 解决了 ErrorView 中错误消息的显示问题。
猜你喜欢
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多