【发布时间】:2012-01-17 23:11:09
【问题描述】:
我有两个 Prism 模块。 我希望其中一个注册一个窗口,另一个使用“显示对话框”模式显示此窗口。 怎么做(如果可以的话)?
【问题讨论】:
我有两个 Prism 模块。 我希望其中一个注册一个窗口,另一个使用“显示对话框”模式显示此窗口。 怎么做(如果可以的话)?
【问题讨论】:
是的,可以做到。这是一个粗略的过程:
在您的“基础设施”项目中声明此视图的接口
public interface IMyDialogWindow
{
}
[Export] 在你的模块中实现这个接口的类
[Export(typeof(IMyDialogWindow))]
public class MyClassInModuleA : IMyDialogWindow
{
}
[Import] 这个类在其他模块中并用于Dialog
[Import]
public IMyDialogWindow PropertyInModuleB
【讨论】:
嗯。我想我通过关注this tip 解决了这个问题。但我不知道这是否是最好的解决方案。
我刚刚在我的 Shell 项目上创建了一个窗口。此窗口将作为对话窗口弹出。
这是它的代码:
Popup.xaml:
<Window x:Class="TryERP2.Shell.Views.Popup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Popup" Height="315" Width="411"
xmlns:prism="http://www.codeplex.com/prism">
<Grid>
<ContentControl x:Name="DialogRegion" Grid.Row="1" prism:RegionManager.RegionName="DialogRegion" />
</Grid>
</Window>
Popup.xaml.cs:
public partial class Popup : Window
{
private static Popup popup;
private Popup(IRegionManager regionManager)
{
InitializeComponent();
RegionManager.SetRegionManager(this, regionManager);
}
//Using the singleton pattern
public static Popup getPopup(IRegionManager regionManager)
{
if (popup == null)
popup = new Popup(regionManager);
return popup;
}
}
最后,当我想显示对话框时(在模块中的命令中),我只是实例化它并告知 RegionManager 是什么:
private void showDialog()
{
// Acquiring the RegionManager
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
// Getting the Popup object
Popup p = Popup.getPopup(regionManager);
// Looking for the view I want to show in the dialog
var x = new Uri("MyView", UriKind.Relative);
// Changing the view of the DialogRegion (which is within the Popup)
regionManager.RequestNavigate("DialogRegion", x);
// Showing the dialog
p.ShowDialog();
}
【讨论】: