如何避免将XAML视图耦合以在WPF MVVM应用程序中启动代码? [英] How do I avoid coupling XAML Views to start up code in a WPF MVVM application?

查看:85
本文介绍了如何避免将XAML视图耦合以在WPF MVVM应用程序中启动代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢通过XAML在视图的DataContext上声明我的ViewModel的灵活性,但是我很难弄清楚如何将该ViewModel绑定到系统的其余部分。

I really like the flexibility of declaring my ViewModel on the DataContext of a View through XAML, but I am having a hard time figuring out how I can tie this ViewModel into the rest of the system.

Ex。

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ViewModels">

<Window.DataContext>
    <local:MainViewModel />
</Window.DataContext>    

这里的问题是,我的ViewModel经常拥有并与系统其他部分共享依赖项,而且我不确定如何将另一个对象的引用注入到上面声明的MainViewModel中。

The problem here, is that my ViewModels will often have and share dependencies with other parts of the system, and I am not sure how I can inject a reference of another object into the MainViewModel declared above.

如果我正在手动进行依赖注入,那么我将创建在应用程序开始时,有一堆工厂负责将所有内容连接起来。

If I were doing a manual sort of Dependency Injection, then I would create a bunch of factories at the start of the application responsible for wiring everything up.

如何在MVVM WPF应用程序中采用相同的方法?并获得所有ViewModel句柄的最有效方法是什么?

How can I take this same approach in a MVVM WPF application? And what is the most effective way to get a handle on all of my ViewModels?

推荐答案

要实现完全解耦,请将ViewModels设置为on在主App类上找到的 ResourceDictionary 。有两种方法可以执行此操作,在大多数情况下,使用哪种方法都无关紧要。但是,需要权衡取舍。

To achieve complete decoupling, set ViewModels on the ResourceDictionary found on the main App class. There are two ways to do this, and for the most part it doesn't matter which method is used. There are trade-offs however.

方法1

如果以编程方式完成,则必须确保字典键比赛。这会导致XAML中定义的字符串与以编程方式定义的字符串之间的弱耦合。不是理想的,但也不是世界末日。这里的优点是能够使用构造函数注入。

If it is done progamatically, you must ensure Dictionary keys match. This causes a weak coupling between the strings defined in the XAML and those defined programmatically. Not ideal, but not the end of the world either. The advantage here is that ability to use constructor injection.

//App.xaml.cs
//This line invoked somewhere after App OnStartUp function is called. 
MainViewModel mainViewModel = new MainViewModel( new Dependency() );
Current.ResourceDictionary["MainViewModel"] = mainViewModel; 

//MainView.xaml
<Window DataContext="{StaticResource MainViewModel}">

业务逻辑不在乎视图是什么,可以以任何方式引导应用程序...使用factory / builder对象或任何IOC容器。 (只要它们都从OnStartUp函数开始)。

The Business logic doesn't care what a View is, and the application can be Bootstrapped in any way... using factory/builder object or any IOC container. (As long as it all starts in the OnStartUp function).

方法2

使用 Application.Resource App.xaml 中定义ViewModel。使用此方法,所有键名都将位于XAML中,感觉非常不错。唯一的负面结果是.NET会自动构建ViewModels,从而强制提供默认的构造函数。有时,需要IOC容器构建对象或在自定义工厂/构建器中使用构造函数注入。

Define ViewModels in App.xaml using Application.Resource. Using this method all key names will be located in XAML, which feels pretty nice. The only negative result is that .NET automatically builds the ViewModels, forcing the to provide default constructors. Sometimes it is desirable for the IOC container to build your objects, or use Constructor Injection in custom factories/builders.

//App.xaml
<Application 
    xmlns:local="clr-namespace:ViewModels" />

<Application.Resources>
     <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <local:MainViewModel x:key="MainViewModel" />
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

//App.xaml.cs
MainViewModel mainViewModel = Current.ResourceDictionary["MainViewModel"];
mainViewModel.propertyInjection = new Dependency();

//MainView.xaml
<Window DataContext="{StaticResource MainViewModel}">

这两种方法都是有效的选择,并且通过少量的密钥管理,可以将混合搭配用于西装要求。

Both ways are valid options, and with a little key management, a mix and match can be used to suit requirements.

这篇关于如何避免将XAML视图耦合以在WPF MVVM应用程序中启动代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆