Prism和MVVM Light工具包

本文关键字:工具包 Light MVVM Prism | 更新日期: 2023-09-27 18:20:12

我试图将Prism(仅用于CompositeUI)和MVVM Light Toolkit(用于MVVM架构=D)放在一起工作,但Light ViewModelLocator遇到了一些问题。当我们在"简单"的WPF应用程序中使用定位器时,我们可以在App.xaml上使用应用程序资源,如下所示:

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

但是当我们使用Prism时,我们的业务代码在Module项目中,并且它没有App.xaml,然后我尝试将该资源放入View资源中:

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

它刚刚关闭了我的设计模式(以及运行时)错误,但视图没有出现在分配给它的区域中

有人已经试着做这样的事了?有可能让Prism和MVVM Light一起工作吗?

这是我的"完整"代码:

(ViewLight.xaml)

<Window x:Class="TryERP2.Financeiro.View.ViewLight"
    ... a lot of NS ...
    mc:Ignorable="d"
    xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel"
    d:DesignHeight="150" d:DesignWidth="245" SizeToContent="WidthAndHeight">
    <Window.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
        </ResourceDictionary>
    </Window.Resources>
    <Grid DataContext="{Binding Light, Source={StaticResource Locator}}">
        <Button Content="{Binding MyButtonText}" HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch" />
    </Grid>
</Window>

ViewLightModel.cs:

public class ViewLightModel : ViewModelBase
{
    public ViewLightModel()
    { }
    public const string MyButtonTextPropertyName = "MyButtonText";
    private string _myButtonText = "Button Text";
    public string MyButtonText
    {
        get
        { return _myButtonText; }
        set
        {
            if (_myButtonText == value)
            { return; }
            _myButtonText = value;
            RaisePropertyChanged(MyButtonTextPropertyName);
        }
    }
}

Financeiro.cs(模块初始化器类…部分显示…仅在我注册并"调用"视图的位置):

        var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
        container.RegisterType<Object, ViewLight>("ViewLight");
        var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        var main = new Uri("ViewLight", UriKind.Relative);
        regionManager.RequestNavigate("Desktop", main);

"普通"MVVM Light应用程序有一个App.xaml文件,(我认为)在Prism模块视图中没有使用。该文件的结构如下:

<Application x:Class="TryERP2.Financeiro.App"
         ... a lot of NS ...
         xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel"
         StartupUri="MainWindow.xaml"
         mc:Ignorable="d">
    <Application.Resources>
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True" />
    </Application.Resources>
</Application>

当我执行应用程序时,就会发生这种情况。模块上的视图应该加载到这个空白区域,显示其按钮,但什么也没发生:

http://imageshack.us/photo/my-images/818/capturarwy.png

当我试图改变这个视图并放置一个"简单视图"(不使用MVVMLight)时,它可以完美地工作,如下图所示:

http://imageshack.us/photo/my-images/513/capturar1a.png

Prism和MVVM Light工具包

在ViewLight.xaml中,您应该更改

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

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

因为Window.Resources已经是一个ResourceDictionary,并且您在其中创建了另一个。您不能在此处访问Locator资源:

<Grid DataContext="{Binding Light, Source={StaticResource Locator}}">

另一种选择是使用MergedDictionary。

对不起。我犯了一个大错误。您可能已经注意到,我正在构建一个功能区应用程序,并试图在空白区域下显示控件。可以在那里显示的项目是控件。它不起作用,因为我正试图显示一个窗口:
<Window x:Class="TryERP2.Financeiro.View.ViewLight"
... a lot of NS ...
mc:Ignorable="d"
xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel"
d:DesignHeight="150" d:DesignWidth="245" SizeToContent="WidthAndHeight">
... etc

我通过将其更改为用户控件而不是窗口来修复它,它运行得很好。

我的新ViewLight.xaml:

<UserControl x:Class="TryERP2.Financeiro.View.ViewLight"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel"
        d:DesignHeight="150" d:DesignWidth="245">
    <UserControl.Resources>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </UserControl.Resources>
    <Grid DataContext="{Binding Light, Source={StaticResource Locator}}">
        <Button Content="{Binding MyButtonText}" HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch" />
    </Grid>
</UserControl>

我的新ViewLight.xaml.cs(代码隐藏):

public partial class ViewLight : UserControl
{
    public ViewLight()
    {
        InitializeComponent();
    }
}

ViewModel仍然是一样的。