Prism 6 with Unity - 在没有命名约定的情况下解析视图的视图模型

2023-11-30

我正在尝试在我的 WPF 应用程序中使用 DI 与 Prism 6 和 Unity 来解析视图模型,并且这有效。但是我不知道如何告诉框架哪个视图应该与哪个视图模型合并。

如果我使用约定,即具有 ViewModels、Views 命名空间以及类 ViewA 和 ViewAViewModel 一切正常,但是我希望有更大的灵活性来命名和组织我的类,这就是为什么我想以某种方式明确告诉框架哪个视图与哪个视图模型匹配。我尝试了很多东西,但没有任何效果。当前的“解决方案”使应用程序运行,但未设置视图模型。

这是代码:

ViewA.xaml

<UserControl x:Class="WPFDITest.Views.ViewA"
             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" 
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <TextBlock Text="{Binding ViewAMessage}"/>
        <TextBox Text="{Binding ViewAMessage, UpdateSourceTrigger=PropertyChanged}"/>
    </StackPanel>
</UserControl>

主窗口.xaml

<UserControl x:Class="WPFDITest.Views.ViewA"
             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" 
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <TextBlock Text="{Binding ViewAMessage}"/>
        <TextBox Text="{Binding ViewAMessage, UpdateSourceTrigger=PropertyChanged}"/>
    </StackPanel>
</UserControl>

ViewAVM.cs

public class ViewAVM : BindableBase
{
    private string viewAMessage;

    public ViewAVM(IModelA model)
    {
        viewAMessage = model.HelloMsgA();
    }

    public string ViewAMessage
    {
        get { return viewAMessage; }
        set { SetProperty(ref viewAMessage, value); }
    }
}

Model.cs

public interface IModelA
{
    string HelloMsgA();
}

public class ModelA : IModelA
{
    public string HelloMsgA()
    {
        return "Hello from A!";
    }
}

应用程序.xaml.cs

public partial class App
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        var bootstraper = new Bootstrapper();
        bootstraper.Run();
    }
}

Bootstrapper.cs

public class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void InitializeShell()
    {
        Application.Current.MainWindow.Show();
    }

    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        Container.RegisterType<IModelA, ModelA>(new ContainerControlledLifetimeManager());
        Container.RegisterType<object, ViewAVM>("ViewA");
    }

    protected override void ConfigureViewModelLocator()
    {
        ViewModelLocationProvider.SetDefaultViewModelFactory(type => Container.Resolve(type));
    }
}

经过一番挖掘 Prism 资源后,我发现了如何做我想做的事。我可以注册每个视图ViewModelLocationProvider.Register传入视图模型的工厂方法。我创建了一种方法,它使用方便的语法来实现这一点,并使用容器来解析给定类型的视图模型:

public void BindViewModelToView<TViewModel, TView>()
{
    ViewModelLocationProvider.Register(typeof(TView).ToString(), () => Container.Resolve<TViewModel>());
}

这就是我如何使用它来绑定ViewAVM to ViewA and ViewB两者都使用相同的单例实例。

public class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void InitializeShell()
    {
        Application.Current.MainWindow.Show();
    }

    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        Container.RegisterType<IModelA, ModelA>(new ContainerControlledLifetimeManager());
        Container.RegisterType<ViewAVM>(new ContainerControlledLifetimeManager());
    }

    protected override void ConfigureViewModelLocator()
    {
        BindViewModelToView<ViewAVM, ViewA>();
        BindViewModelToView<ViewAVM, ViewB>();
    }
}

顺便说一句,据我所知,只有通过注册工厂或使用它们的或自定义约定,才能通过 ViewModelLocator 将视图与视图模型关联起来,不要寻找一些 DI 魔法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Prism 6 with Unity - 在没有命名约定的情况下解析视图的视图模型 的相关文章

  • WPF 中的 IWin32Window

    我有一个 excel AddIn 它公开 IWin32Window 作为它的主 UI 我想显示一个使用它作为父窗口的 WPF 窗口 我该如何去做呢 您可以使用窗口互操作助手 http msdn microsoft com en us lib
  • strtok() 和空字段

    我正在将一些 C 结构序列化为字符串 然后将其反序列化strtok 但不幸的是 strtok 不检测空字段 例如 1 2 4 有没有替代功能 在linux上有strsep http www mkssoftware com docs man3
  • 氧图。如何将轴旁边的值格式从 1000 更改为 1k

    我正在尝试更改轴旁边的值的格式 例如从 1000 更改为 1k 或 1000000 更改为 1M 这在 LinearAxis 中可能吗 这是我的代码 m Axes Add new LinearAxis Position AxisPositi
  • 委托和接口如何互换使用?

    我可以使用接口方法代替委托吗 如何 我发现搜索接口方法比使用委托更快 我希望有一个简单的代码片段 理论上 可以通过包含单个方法的接口 例如 Java 没有委托 来完成委托完成的所有工作 然而 它使代码变得更加冗长并且没有带来什么好处 话又说
  • 以概率从列表中选择随机元素

    我有一个包含四个项目 A B C D 的列表 每个项目都有被选择的概率 例如 A 有 74 的机会被选中 B 15 C 7 D 4 我想创建一个函数 根据其概率随机选择一个项目 有什么帮助吗 为您的项目定义一个类 如下所示 class It
  • 运行 C# exe 文件

    复制 为什么我的 NET 应用程序在从网络驱动器运行时会崩溃 https stackoverflow com questions 148879 why does my net application crash when run from
  • .NET Core 2 - 从启动中调用存储库方法[重复]

    这个问题在这里已经有答案了 我有以下存储库和类 public interface IValueService GetAll public class ValueService IValueService private DataContex
  • 更新 OSX 命令行工具 6.3 后缺少 C++ 标头 <__debug>

    从 App Store 更新到 Command Line Tools 6 3 后 程序包括
  • 如何使用 DesignData 帮助开发 Metro 应用程序?

    我一直在 Windows Phone 应用程序中愉快地使用 DesignData 我希望使用它来帮助在 VS2012 Blend for VS 中的 Metro 风格应用程序中可视化设计 我已经尝试过希望显而易见的方法
  • dlopen 或 dlclose 未调用信号处理程序

    我在随机时间内收到分段错误 我注册了信号 但发生分段错误时未调用信号处理程序 include
  • C++ fill() 与 uninitialized_fill()

    您好 我是初学者 我想知道容器的 fill 和 uninitialized fill 之间的区别 我在谷歌上进行了快速搜索 但没有得到很好的答案 有人可以帮助我吗 fill 将值 使用赋值运算符 分配给已构造的对象 uninitialize
  • Bool类型返回规则

    我使用 dapper ORM 所以我使用两个规则Query
  • 使用 unrar 库 - 将文件提取到文件流缓冲区中

    我需要的是能够将 rar 文件中的文件提取到流中 我正在创建一个测试用例来了解如何使用解压源文件 http www rarlab com rar unrarsrc 3 9 9 tar gz 我已经搜索和修补了一段时间 但我不知道如何使用该库
  • 如何修改 edmx 的默认代码生成策略?

    我想修改默认的代码生成策略 该怎么做 我只是想修改类名 lt code Escape container gt to Entities并将默认连接字符串更改为name Default 我不想为该项目创建模板文件 我想编辑它以便它可以在全球范
  • 通过 MVC 将数据写入数据库的最佳方法是什么?

    我正在使用 MVC 和 EF Core 开发一个家庭作业项目 我正在寻找将数据写入数据库的最佳方法 我是初学者 有两张桌子 Predbilje ba 报名 和Seminari 研讨会 public class Predbilje ba Ke
  • 如何在 C# 中停止程序进一步执行

    string FirstName Console ReadLine if FirstName Length gt 12 Console WriteLine if FirstName Length lt 3 Console WriteLine
  • C# 的 xml 序列化中是否有一个属性可以跳过空数组?

    C 的 xml 序列化中是否有一个属性可以跳过空数组 这将提高 xml 输出的可读性 好吧 你也许可以添加一个ShouldSerializeFoo method using System using System ComponentMode
  • 如何将这个基于代码的 WPF 工具提示转换为 Silverlight?

    以下工具提示代码适用于WPF 我正在努力让它发挥作用银光 但它给了我这些errors TextBlock does not contain a definition for ToolTip Cursors does not contain
  • 当前线程中的单例

    我的单身人士如下 public class CurrentSingleton private static CurrentSingleton uniqueInstance null private static object syncRoo
  • “while(true) { Thread.Sleep }”的原因是什么?

    我有时会遇到以下形式的代码 while true do something Thread Sleep 1000 我想知道这是否被认为是好的做法还是坏的做法以及是否有任何替代方案 通常我在服务的主函数中 找到 这样的代码 我最近在 Windo

随机推荐