[英]WPF,MEF,Prism - How to set DataContext in shell
我将WPF / PRISM / MEF用于桌面应用程序。
这是一个简单的测试应用程序,具有三个区域。 视图在外部模块中定义。
看来我需要设置外壳DataContext。 因此,将其设置为如下所示的视图模型-应用程序运行正常。
我对做出明确的定义不满意。 在初始化过程中,是否无法加载某些模块,并找到一些视图并将其分配给我的Shell的DataContext,这是不可能的吗? 我在哪里可以找到文档-我一定在开发人员指南(和示例应用程序)中错过了它。 或者,有人提出建议吗?
Shell.xaml:
<Window x:Class="TestMenuTaskbarDT.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://www.codeplex.com/prism"
xmlns:local_viewmodels="clr-namespace:TestMenuTaskbarModuleMain.ViewModels;assembly=TestMenuTaskbarModuleMain"
. . .
>
<Window.InputBindings>
<KeyBinding Key="S" Modifiers="Control" Command="{Binding Path=CloseProjectCommand}" />
<KeyBinding Key="O" Modifiers="Control" Command="{Binding Path=OpenProjectCommand}" />
</Window.InputBindings>
<StackPanel>
<ItemsControl Name="MainMenuRegion" prism:RegionManager.RegionName="MainMenuRegion" />
<ItemsControl Name="MainToolbarRegion" prism:RegionManager.RegionName="MainToolbarRegion" />
<ItemsControl Name="MainContentRegion" prism:RegionManager.RegionName="MainContentRegion" />
</StackPanel>
<!-- How does one set the datacontext, when it should be dynamically loaded? -->
<Window.DataContext>
<local_viewmodels:MainWindowViewModel />
</Window.DataContext>
</Window>
(或者应该将local_viewmodels ...放在资源中并以某种方式设置在那里?)
然后可以在Bootstrapper中放入以下内容吗? 还是有完全不同的技术?
Bootstrapper.cs:
. . .
class Bootstrapper : MefBootstrapper
{
. . .
protected override IModuleCatalog CreateModuleCatalog()
{
// All dlls are expected to reside in the same directory as the *.exe
return new DirectoryModuleCatalog() { ModulePath = System.AppDomain.CurrentDomain.BaseDirectory };
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
// Module registration remains the same *IT* registers the views with the region catalog
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(TestMenuTaskbarModuleMain.TestMenuTaskbarModuleMain).Assembly));
}
. . .
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)this.Shell;
// Is something like the following possible?
_MyBaseViewModel = GetAViewModelSomehowFromAModule("MyViewModelKey");
Application.Current.MainWindow.DataContext = _MyBaseViewModel;
//
Application.Current.MainWindow.Show(); // Displays MainWindow
}
在我的Shell.xaml.cs中,将定义以下属性:
[Import]
ShellViewModel ViewModel
{
set
{
this.DataContext = value;
}
}
然后在我的ShellViewModel类中,构造函数将像这样被装饰...
[Export]
public class ShellViewModel
因此,依靠MEF组合导入/导出属性来实现DataContext设置。 这是一个简化的示例,但您可能需要进一步了解有关构图纯度错误等问题。
如果我了解Dynamoid,则应使用以下内容:
namespace TestMenuTaskbarDT
{
[Export]
public partial class Shell : Window
{
[ImportingConstructor] public Shell([Import("ShellViewModel")]object aShellViewModel)
{
InitializeComponent();
//NOTE: DataContext is magically set from the imported object "aShellViewModel."
// I assume MEF uses Reflection to magically resolve all internal references.
DataContext = aShellViewModel;
}
/* removed, ImportingConstructor does it all in one step.
Note: DataContext is magically set from "value."
[Import("ShellViewModel")]
public object ViewModel
{
set { this.DataContext = value; }
}
*/
}
}
显然,带有初始化的单步构造比先构造然后初始化的两步形式简单。 (因此,人们应该单击动态注释以给予他应有的待遇-不仅是出于他的暗示,而且是出于他对表达自己观点的热情。)
(这里剩下的唯一一件事就是让它在设计时正确显示-但这是另外一个困惑。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.