C# 如何在PRISM模块内使用ReactiveUI
我们有一个现有的WPF应用程序,它使用PRISM(6.x)和Unity(3.5.x)进行DI。我们将向该应用程序添加更多功能,并希望开始为我们需要实现的任何新模块使用ReactiveUI 我们希望最小化学习曲线,并继续将Unity和DI用于我们的ViewModels;然而,ReactiveUI用于查看位置,到目前为止,我还无法让我的ReactiveUI模块在主应用程序中的棱柱区域中实际显示,无论我尝试了什么 我真正找到的唯一一篇文章是这篇文章,有人在写他们自己的集成 因此,给定一些视图模型:C# 如何在PRISM模块内使用ReactiveUI,c#,wpf,unity-container,prism,reactiveui,C#,Wpf,Unity Container,Prism,Reactiveui,我们有一个现有的WPF应用程序,它使用PRISM(6.x)和Unity(3.5.x)进行DI。我们将向该应用程序添加更多功能,并希望开始为我们需要实现的任何新模块使用ReactiveUI 我们希望最小化学习曲线,并继续将Unity和DI用于我们的ViewModels;然而,ReactiveUI用于查看位置,到目前为止,我还无法让我的ReactiveUI模块在主应用程序中的棱柱区域中实际显示,无论我尝试了什么 我真正找到的唯一一篇文章是这篇文章,有人在写他们自己的集成 因此,给定一些视图模型: p
public class HelloWorldViewModel : ReactiveObject
{
string title = "Hello ReactiveUI";
public string Title
{
get { return title; }
set { this.RaiseAndSetIfChanged(ref title, value); }
}
}
以及其相应的观点:
<UserControl x:Class="PBI.ObjectMover.ReactSample.Views.HelloWorldView"
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">
<Grid>
<TextBlock Text="{Binding Title}" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>
</Grid>
</UserControl>
我已尝试将棱镜定位器添加到视图的XAML中:
<UserControl x:Class="PBI.ObjectMover.ReactSample.Views.HelloWorldView"
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:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
我还尝试在模块的初始化中初始化Splat:
public void Initialize()
{
/* Register types */
Locator.CurrentMutable.InitializeSplat();
Locator.CurrentMutable.InitializeReactiveUI();
Locator.CurrentMutable.Register(() => new HelloWorldView(), typeof(IViewFor<HelloWorldViewModel>));
/* Register views */
this.RegionManager.RegisterViewWithRegion("RightRegion", typeof(HelloWorldView));
}
public void Initialize()
{
/*寄存器类型*/
Locator.CurrentMutable.InitializeSplat();
Locator.CurrentMutable.InitializeReactiveUI();
Locator.CurrentMutable.Register(()=>new HelloWorldView(),typeof(IViewFor));
/*注册视图*/
this.RegionManager.RegisterViewWithRegion(“RightRegion”,typeof(HelloWorldView));
}
到目前为止,一切都没有起作用,但我怀疑我们是唯一看到将PRISM的模块化体系结构与ReactiveUI框架结合使用的好处的人。PRISM和RxUI以及视图绑定的不同语义 Prism尝试为给定视图解析VM类型,而RxUI则相反,为当前VM解析视图 您的实现正在(几乎)工作: 这足以显示视图,如果添加
AutoWireViewModel=True
,Prism将创建一个VM实例,并将其设置为视图DataContext
注意这句话:
this.WhenAnyValue(x => x.ViewModel).BindTo(this, x => x.DataContext);
尝试将ViewModel
属性绑定到同一个DataContext
,并很乐意使用null,覆盖由Prism创建/设置的实例(我相信这是您的问题)
简单地删除这个绑定是可行的,但总的来说,它似乎不是一个混合Prism和RxUI的好方法
也许您可以查看RxUIRoutedViewHost
和ViewModelViewHost
并将其中一个绑定到区域中。然后RxUI视图解析将自动启动,并根据控件上设置的当前VM刷新内部视图。此时您需要注册视图,就像您所做的那样:
Locator.CurrentMutable.Register(() => new HelloWorldView(), typeof(IViewFor<HelloWorldViewModel>));
Locator.CurrentMutable.Register(()=>new HelloWorldView(),typeof(IViewFor));
this.RegionManager.RegisterViewWithRegion("RightRegion", typeof(HelloWorldView));
this.WhenAnyValue(x => x.ViewModel).BindTo(this, x => x.DataContext);
Locator.CurrentMutable.Register(() => new HelloWorldView(), typeof(IViewFor<HelloWorldViewModel>));