如何使用用户控制ViewModelLocator?(How to use a ViewModelLo

2019-10-29 19:11发布

有人问我转换使用MVVM模式到用户控制一个独立的WPF应用程序。 这个应用程序由一个主窗口和其他几个窗口。 不过,我想这样做,都指向我的App.xaml类,并声明如下转换器等资源时获得了一些错误:

Library project file cannot specify ApplicationDefintion element. The project file contains a property value that is not valid. The name "ViewModelLocator" does not exist in the namespace "clr-namespace:MapperX.ViewModels"

所以它看起来像错误在我ViewModelLocator旋转。

目前该项目的目录结构设置,如下所示:

顶级 - >文件夹的ViewModels - > ViewModelLocator

在App.xaml中设置像这样:

<Application x:Class="MapperX.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MapperX"
             xmlns:vm="clr-namespace:MapperX.ViewModels"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <vm:ViewModelLocator x:Key="ViewModelLocator" />
    </Application.Resources>
</Application>

而这里的一个片断ViewModelLocator类:

namespace MapperX.ViewModels
{
    /// <summary>
    /// This class instantiates all the viewmodels
    /// </summary>
    public class ViewModelLocator
    {
        WpfMap map = new WpfMap();

        private MainViewModel _mainViewModel;
        public MainViewModel MainViewModel
        {
            get
            {
                if (_mainViewModel == null)
                {
                    _mainViewModel = new MainViewModel(map)
                }
                return _mainViewModel;
            }
        }

        private LayersViewModel _layersViewModel;
        public LayersViewModel LayersViewModel
        {
            get
            {
                if (_layersViewModel == null)
                {
                    _layersViewModel = new LayersViewModel(map)
                }
                return _layersViewModel;
            }
        }
    }
}

然后,我设置DataContext的意见.XAML像这样: DataContext="{Binding Path=MainViewModel, Source={StaticResource ViewModelLocator}}"

什么是正确的方式仍然能够使用ViewModelLocator没有App.xaml中?

Answer 1:

而不是依靠资源的,您可以创建一个附加属性来设置DataContext的控件库中的观点:

namespace ControlsAndResources
{
    public class View
    {
        private static readonly ViewModelLocator s_viewModelLocator = new ViewModelLocator();

        public static readonly DependencyProperty ViewModelProperty = DependencyProperty.RegisterAttached("ViewModel", typeof(string), 
            typeof(ViewModelLocator), new PropertyMetadata(new PropertyChangedCallback(OnChanged)));

        public static void SetViewModel(UserControl view, string value) => view.SetValue(ViewModelProperty, value);

        public static string GetViewModel(UserControl view) => (string)view.GetValue(ViewModelProperty);

        private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UserControl view = (UserControl)d;
            string viewModel = e.NewValue as string;
            switch (viewModel)
            {
                case "MainViewModel":
                    view.DataContext = s_viewModelLocator.MainViewModel;
                    break;
                case "LayersViewModel":
                    view.DataContext = s_viewModelLocator.LayersViewModel;
                    break;
                default:
                    view.DataContext = null;
                    break;
            }
        }
    }
}

用法:

<UserControl xmlns:local="clr-namespace:ControlsAndResources" ... 
             local:View.ViewModel="MainViewModel">


Answer 2:

为什么不把ViewModelLocator在顶层UserControl

<UserControl.Resources>
   <vm:ViewModelLocator x:Key="ViewModelLocator" />
</UserControl.Resources>


文章来源: How to use a ViewModelLocator in a user control?