简体   繁体   English

如何将我的自定义用户控件及其自定义视图 model 包含到我的 xaml 视图中?

[英]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 IMyUserControlViewModelMyUserControl并从您的视图 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.

相关问题 如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件? - How can I make WPF instantiate a custom control in my view, using another custom control base class in my XAML? 包括动态自定义 Xamarin Xaml 视图控件 - Include Dynamic Custom Xamarin Xaml View Control 从用户控件中xaml的依赖项属性为我的视图模型分配值 - Assigning a value to my view model from my dependency property from xaml in a user control 如何将文本绑定到我的自定义XAML控件? - How to bind text to my custom XAML control? 使用ListView时,如何在Xamarin Forms中将自定义控件绑定设置为父视图模型属性? - How can I set custom control binding to parent view model property in Xamarin Forms when using a ListView? 绑定到选项卡控件后,如何延迟加载视图模型属性? - How can I lazy load my view model properties when bound to a tab control? 如何在我的视图中访问模型中的属性 - How can I access the attributes from my model in my view OnPost之后如何在我的视图中保留模型? - How can I keep my model in my View after OnPost? 在WPF中,如何实现ICommandSource以使我的自定义控制能力能够使用来自xaml的命令? - In WPF how do I implement ICommandSource to give my custom control ability to use Command from xaml? 如何从其视图模型关闭用户控件 - How to close User Control from its View Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM