我下面的代码在我的WPF App.xaml.cs文件:问题与app.xaml.cs到MainViewmodel通信
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
var mainVM = MainWindowViewModel.Instance;
mainVM.DisplayMessage = string.Format("Something went wrong and it has been logged...If the problem persists, please contact {0}.", mainVM.NotificationsReceiver);
mainVM.DisplayMessageForegroundColor = "Red";
e.Handled = true;
}
MainWindowViewModel.cs
public string DisplayMessage
{
get
{
return m_displayMessage;
}
set
{
m_displayMessage = value;
OnPropertyChanged("DisplayMessage");
}
}
public string DisplayMessageForegroundColor
{
get
{
return m_displayMessageForegroundColor;
}
set
{
m_displayMessageForegroundColor = value;
OnPropertyChanged("DisplayMessageForegroundColor");
}
}
MainWindow.xaml
<Label Content="{Binding DisplayMessage}" Foreground="{Binding DisplayMessageForegroundColor}" Grid.Column="1" HorizontalAlignment="Left" Height="33" Margin="14,660,0,0" Grid.Row="1"
VerticalAlignment="Top" Width="693" Grid.ColumnSpan="3"/>
但是,这似乎并没有工作。虽然在app.xaml.cs中的方法被调用,我没有看到显示错误m在UI上发送消息。请问这里有什么不对吗? (我可以在MainWindowViewModel中设置DisplayMessage和DisplayMessageForegroundColor属性时看到消息)。
请指教。
感谢。
我猜MainWindowViewModel.Instance不是绑定到视图的视图模型的实例。 – ibebbs
向我们展示如何将'MainWindowViewModel.Instance'赋值给'MainWindow.DataContext'。这可能是问题所在。 @ibebbs几乎肯定是正确的。 –
你好...我在我的MainWindow.xaml文件中设置上下文---> ....这就是你需要吗?谢谢。 –