gpt4 book ai didi

c# - 如何在 ShowDialog() 阻塞调用之前注册消息处理程序?

转载 作者:行者123 更新时间:2023-11-30 12:24:19 24 4
gpt4 key购买 nike

我正在使用 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>

最佳答案

您可以通过几种方式解决问题:

  1. 不要使用 ShowDialog()。使用 Show(),并将对话框窗口设置为 TopMost 并作为主窗口的父级。
  2. 在 DialogService 的构造函数中注册 ProductView(你真的每次都需要一个新的 ProductView 吗?)

  3. 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com