UserControl上的MVVMLight ViewModelLocator [英] MVVMLight ViewModelLocator on a UserControl

查看:312
本文介绍了UserControl上的MVVMLight ViewModelLocator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在UserControl上使用MVVMLight ViewModelLocator. 我已经按照与MainWindow相同的方式将其添加到用户控件中,但是在VS2010中出现错误/弹出窗口,提示找不到名为"Locator"的资源.资源名称区分大小写."

Is it possible to use the MVVMLight ViewModelLocator on a UserControl. I have added it to my user control in the same way as is done on the MainWindow, but i get an error/popup in VS2010 stating "Cannot find resource named 'Locator'. Resource names are case sensitive."

有人尝试过吗?

到目前为止,我拥有的代码几乎是标准的MVVMLight WPF入门应用程序...

Code i have thus far is pretty much a standard MVVMLight WPF starter application...

UserControl

<UserControl x:Class="NavTest3.PersonControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         Height="116" MinWidth="250" Width="300"
         DataContext="{Binding Person, Source={StaticResource Locator}}"
         >

<!---->
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

App.xaml包含..

App.xaml contains..

<Application.Resources>
    <!--Global View Model Locator-->
    <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />
</Application.Resources>

所以问题在于在userControl上设置了"DataContext ="{Binding Person,Source = {StaticResource Locator}}"".

so the issue is with setting "DataContext="{Binding Person, Source={StaticResource Locator}}" on the userControl."

如前所述,这样做将意味着该用户控件的每个实例都将共享相同的ViewModel,但是我想在继续之前先了解这个问题.

As mentioned, doing this will mean every instance of this user control will share the same ViewModel, but I want to start with understanding this issue before moving on..

推荐答案

是的,您需要在用户控件中创建静态资源

Yes you can, you need to create a static resource in your user control

<UserControl x:Class="MvvmLight1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"

             xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
             >

    <UserControl.Resources>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </UserControl.Resources>
    <Grid>

    </Grid>
</UserControl>

但是IMO最好不要将MVVM Light ViewModelLocator用于UserControles,因为它是静态属性,并且当您实例化用户控件的多个实例时,它们将具有相同的通用ViewModel,因此它们的行为均相同如果您决定在整个项目中一次使用UserControl,这不是我们想要的.

but IMO it's not a good idea to use MVVM Light ViewModelLocator for UserControles because it is a static property and when you are going to instantiate multiple instances of your user control there are going to have the same common ViewModel so they all act same and this is not what we want for a UserControl in case you decide to use it once in your entire project.

要解决此问题,您需要通过将所有属性设置为非静态来修改ViewModelLocator:

to get around this problem you need to modify the ViewModelLocator by making all the properties Non static for instance :

 public class ViewModelLocator
    {
        //         v--- You got to comment this out
        private /*static*/ MainViewModel _main;

        public ViewModelLocator()
        {            
            CreateMain();
        }

        public /*static*/ MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        public /*static*/ void ClearMain()
        {
            _main.Cleanup();
            _main = null;
        }

        public /*static*/ void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel();
            }
        }

        public /*static*/ void Cleanup()
        {
            ClearMain();
        }
    }

这篇关于UserControl上的MVVMLight ViewModelLocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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