- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Messenger class为了在 View 模型之间发送数据。有一个 AppView
在内容控件中承载两个主要 View ,到目前为止,以这种方式发送/接收数据没有问题。
问题:
现在我添加了一个 ProductView
向 AppView 显示一个单独的对话框。但是当我调用 Messenger.Default.Send<ProductModel>(SelectedProduct);
在调用 .ShowDetailDialog() 之后,这会阻止 Send
代码调用,直到对话框关闭。
我尝试了相反的方法,调用 Send
先编码,再打开对话框。但这意味着接收 VM 中的消息处理程序在发送消息之前没有及时注册。
有谁知道防止对话阻塞发送调用的解决方案?或者在发送消息和显示对话框之前注册 ProductVM 消息处理程序?
下面是相关类的总结:
CustomerOrdersVM(发送代码):
private void EditOrder(object obj)
{
_dialogService.ShowDetailDialog();
Messenger.Default.Send<ProductModel>(SelectedProduct);
}
ProductVM(接收码):
public ProductViewModel()
{
Messenger.Default.Register<ProductModel>(this, OnSelectedProductReceived);
}
对话服务:
class DialogService : IDialogService
{
Window productView = null;
public DialogService()
{
}
public void ShowDetailDialog()
{
productView = new ProductView();
productView.ShowDialog();
}
}
AppVM(主要 VM 已注册,ProductVM 独立于此 VM):
public ApplicationViewModel()
{
// Add available pages
PageViewModels.Add(new CustomerDetailsViewModel(customerDataService, countryDataService, dialogService));
PageViewModels.Add(new CustomerOrdersViewModel(orderDataService, dialogService));
PageViewModels.Add(new OrderStatisticsViewModel());
// Set starting page
CurrentPageViewModel = PageViewModels[0];
}
AppView:(保存 AppVM View ):
<Window x:Class="MongoDBApp.Views.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:MongoDBApp.Views"
xmlns:vm="clr-namespace:MongoDBApp.ViewModels">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:CustomerDetailsViewModel}">
<views:CustomerDetailsView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:CustomerOrdersViewModel}">
<views:CustomerOrdersView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:OrderStatisticsViewModel}">
<views:OrderStatisticsView />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<vm:ApplicationViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".07*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<TabControl Grid.Row="1"
ItemsSource="{Binding PageViewModels}"
SelectedItem="{Binding CurrentPageViewModel}"
TabStripPlacement="Top">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Grid>
</Window>
最佳答案
您可以通过几种方式解决问题:
ShowDialog()
。使用 Show()
,并将对话框窗口设置为 TopMost 并作为主窗口的父级。在 DialogService 的构造函数中注册 ProductView
(你真的每次都需要一个新的 ProductView
吗?)
让 DialogService
(或其中的实用程序类)在构建时注册消息,然后将消息传递给任何显示的 ProductView
我个人喜欢 #2- 因为您使用的是 ShowDialog
,这意味着一次只需要一个 ProductView
。例如:
class DialogService : IDialogService
{
Window productView = null;
ProductView _productView;
public DialogService()
{
_productView = new ProductView();
}
public void ShowDetailDialog()
{
_productView.ShowDialog();
}
}
关于c# - 如何在 ShowDialog() 阻塞调用之前注册消息处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34347091/
我是一名优秀的程序员,十分优秀!