[英]How can I include my custom user control with its custom view model into my xaml view?
I'd have a little question on prism / xaml in general.一般来说,我对棱镜/xaml 有一个小问题。 If someone has a clue, I'd be happy to know about it.如果有人有线索,我很乐意知道。
I made up a standard Prism application with a Prism module (Prism 6.3.0).我用 Prism 模块(Prism 6.3.0)制作了一个标准的 Prism 应用程序。 There I have a main view consisting of the following code:我有一个由以下代码组成的主视图:
<!-- MainView.xaml -->
<UserControl prism:ViewModelLocator.AutoWireViewModel="True"
...>
[...]
<dxb:BarManager>
[...]
<local:MyUserControl/>
[...]
</dxb:BarManager>
</UserControl>
In that view, you notice在那个观点中,你注意到
<local:MyUserControl/>
That thing is implemented as follows:那件事是这样实现的:
<!-- MyUserControl.xaml -->
<dxr:RibbonPageGroup x:Class="MyUserControl" ...>
[...]
<dxb:BarButtonItem Content="Import"
Command="{Binding ImportDataCommand}" />
[...]
</dxr:RibbonPageGroup>
and和
// MyUserControl.xaml.cs
public partial class MyUserControl
{
public MyUserControl()
{
InitializeComponent();
}
}
Now, the idea is that MyUserControl
uses its own view model, because it needs to contain special logic related to that particular control.现在,想法是MyUserControl
使用自己的视图 model,因为它需要包含与该特定控件相关的特殊逻辑。 All that logic is defined by an interface IMyUserControlViewModel
, which I would like to inject into the MyUserControl
and make it available as its DataContext
.所有这些逻辑都由接口IMyUserControlViewModel
定义,我想将其注入MyUserControl
并使其作为DataContext
可用。 Of course, MyUserControlViewModel
, which implements IMyUserControlViewModel
and which I want to inject into the view, has dependencies in services.当然,实现IMyUserControlViewModel
并且我想注入到视图中的MyUserControlViewModel
在服务中具有依赖关系。 How can I wire that up?我怎样才能把它连接起来? I tried many things without success.我尝试了很多事情都没有成功。 I am well-aware of these advanced ways of wiring up and these basic ways of wiring up .我很清楚这些高级接线方法和这些基本接线方法。 My guess is that it is not possible because, in the MainView.xaml
, we use我的猜测是这是不可能的,因为在MainView.xaml
中,我们使用
prism:ViewModelLocator.AutoWireViewModel="True"
Am I seeing that right?我看对了吗? If not, how can make my idea happen?如果没有,我的想法怎么可能实现? Is it possible to do without changing the ViewModelLocator
's configuration, as explained here ?是否可以不更改ViewModelLocator
的配置,如此处所述? In the project I am working for, they want to stick to automatic view model wiring for some obscure reasons.在我正在工作的项目中,由于一些模糊的原因,他们想坚持自动查看 model 接线。 Is there a way to override the automatic view model wiring in the module initialization code?有没有办法覆盖模块初始化代码中的自动查看 model 接线? The module initialization code I am talking about is something like我正在谈论的模块初始化代码类似于
public class MyModule : IModule
{
private readonly IUnityContainer _container;
private readonly IRegionManager _regionManager;
public MyModule (IUnityContainer container, IRegionManager regionManager)
{
_container = container;
_regionManager = regionManager;
}
public void Initialize()
{
//_container.RegisterType<>();
_regionManager.AddToRegion(RegionNames.MainRegion, ServiceLocator.Current.GetInstance<MainView>());
}
}
As a general rule, try to go view model first, if you can, as it makes things easier and clearer.作为一般规则,如果可以的话,首先尝试 go 查看 model,因为它使事情变得更容易和更清晰。
So in your case, instead of所以在你的情况下,而不是
<UserControl prism:ViewModelLocator.AutoWireViewModel="True">
<dxb:BarManager>
<local:MyUserControl/>
</dxb:BarManager>
</UserControl>
do做
<UserControl prism:ViewModelLocator.AutoWireViewModel="True">
<dxb:BarManager>
<ContentControl Content={Binding SpecialViewModel}/>
</dxb:BarManager>
</UserControl>
with a DataTemplate
that maps IMyUserControlViewModel
to MyUserControl
and expose an instance of the sub-view model from your view model.使用将DataTemplate
IMyUserControlViewModel
到MyUserControl
并从您的视图 model 公开子视图 model 的实例的 DataTemplate。 This way, you can use a specialized instance, tailored for the use case at hand without having to jump through loops just to please the ViewModelLocator
.这样,您可以使用专门为手头的用例量身定制的实例,而不必为了取悦ViewModelLocator
而跳过循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.