如何在 View with Prism 7.2 之间导航?
How to navigate between View with Prism 7.2?
我正在编写一个应用程序,我想在其中放置导航。此应用程序使用 WPF,我使用 Prism 7.2.0.1367。
所以我创建了 2 个视图:一个 HomePage
和一个 ModeFileAttente
视图。
我也在使用 MVVM 模式。
这是我的app.xaml.cs
:
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<V.HomePage>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterDialog<V.HomePage, VM.HomePageViewModel>();
containerRegistry.RegisterDialog<V.ModeFileAttente, VM.ModeFIleAttenteViewModel>();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<Bootstrapper>();
}
}
我的HomePage.xaml
:
<Window x:Class="TestWPF.View.HomePage"
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"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="HomePage" Height="347.667" Width="664">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="277*"/>
<ColumnDefinition Width="16*"/>
</Grid.ColumnDefinitions>
<DockPanel Grid.ColumnSpan="2" Margin="0,0,0.333,-0.333">
<Button Padding="0" DockPanel.Dock="Left" Content="Mode normal" Command="{Binding ClickCommand}" CommandParameter="ModeFileAttente"/>
<Button Padding="0" DockPanel.Dock="Right" Content="Mode tournois" HorizontalAlignment="Stretch"/>
</DockPanel>
<ContentControl prism:RegionManager.RegionName="ContentRegion" Margin="5" />
</Grid>
</Window>
这是视图模型:
class HomePageViewModel : BindableBase, IDialogAware, INotifyPropertyChanged
{
#region Private region
private readonly IRegionManager _regionManager;
public event Action<IDialogResult> RequestClose;
#endregion
public DelegateCommand<string> ClickCommand { get; private set; }
public string Title => throw new NotImplementedException();
public HomePageViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
ClickCommand = new DelegateCommand<string>(ExecuteClickCommand);
}
private void ExecuteClickCommand(string path) {
_regionManager.RequestNavigate("ContentRegion", "FileAttente");
}
public bool CanCloseDialog()
{
throw new NotImplementedException();
}
public void OnDialogClosed()
{
throw new NotImplementedException();
}
public void OnDialogOpened(IDialogParameters parameters)
{
throw new NotImplementedException();
}
}
我还有一个 Bootstrapper
class :
public class Bootstrapper : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ModeFileAttente>();
}
}
我的 MVVM 模式运行良好,但是当我想在我的 "HomePage" 和我的 "ModeFileAttente" 视图之间导航时,除了我的视图上的 "System.Object" 之外没有任何反应。
我已经搜索了可能的问题所在,但没有找到可以解决我的问题的方法。
有人在使用这个版本的 prism 时仍然遇到这个错误吗?
PS:我的 2 个视图位于 "View" 文件夹中,与 ViewModel 文件夹中的视图模型相同
"FileAttente" 应该是 "ModeFileAttente":
_regionManager.RequestNavigate("ContentRegion", "ModeFileAttente");
没有 "FileAttente" 视图注册导航。
我认为您应该在初始化时向区域注册视图。试试下面的代码
public class Bootstrapper : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
_regionManager.RegisterViewWithRegion("RegionName", typeof(View));
_regionManager.RegisterViewWithRegion("RegionName", typeof(View2));
}
}
现在您可以使用
_regionManager.RequestNavigate("RegionName", "View");
或
_regionManager.RequestNavigate("RegionName", "View2");
更新
如果我们使用 prism 那么应该有一个与之关联的依赖注入框架,例如 MEF
或 Unity
对于我的项目我正在使用 Prism.Unity.Wpf
如果您使用 Unity
然后你可以要求你的依赖注入器通过添加一个构造函数来注入 RegionManager,该构造函数将由 IRegionManager
注入,见下文
readonly IRegionManager _regionManager;
public Bootstrapper(IRegionManager regionManager)
{
_regionManager = regionManager;
}
当您将 IRegionManager
注入构造函数时 Unity
知道要始终提供相同的实例,因为它在 Unity
中维护,因此无需担心使用相同的实例IRegionManager
别处。
当我们在 XAML 中使用 prism:RegionManager.RegionName="ContentRegion"
时,我们实际上保留了一个占位符来表示 Prism
有一个区域将被任何 VIEW
填充有些点寻找它。
这是我的理解。如果您觉得这有用,请告诉我谢谢。
我正在编写一个应用程序,我想在其中放置导航。此应用程序使用 WPF,我使用 Prism 7.2.0.1367。
所以我创建了 2 个视图:一个 HomePage
和一个 ModeFileAttente
视图。
我也在使用 MVVM 模式。
这是我的app.xaml.cs
:
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<V.HomePage>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterDialog<V.HomePage, VM.HomePageViewModel>();
containerRegistry.RegisterDialog<V.ModeFileAttente, VM.ModeFIleAttenteViewModel>();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<Bootstrapper>();
}
}
我的HomePage.xaml
:
<Window x:Class="TestWPF.View.HomePage"
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"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="HomePage" Height="347.667" Width="664">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="277*"/>
<ColumnDefinition Width="16*"/>
</Grid.ColumnDefinitions>
<DockPanel Grid.ColumnSpan="2" Margin="0,0,0.333,-0.333">
<Button Padding="0" DockPanel.Dock="Left" Content="Mode normal" Command="{Binding ClickCommand}" CommandParameter="ModeFileAttente"/>
<Button Padding="0" DockPanel.Dock="Right" Content="Mode tournois" HorizontalAlignment="Stretch"/>
</DockPanel>
<ContentControl prism:RegionManager.RegionName="ContentRegion" Margin="5" />
</Grid>
</Window>
这是视图模型:
class HomePageViewModel : BindableBase, IDialogAware, INotifyPropertyChanged
{
#region Private region
private readonly IRegionManager _regionManager;
public event Action<IDialogResult> RequestClose;
#endregion
public DelegateCommand<string> ClickCommand { get; private set; }
public string Title => throw new NotImplementedException();
public HomePageViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
ClickCommand = new DelegateCommand<string>(ExecuteClickCommand);
}
private void ExecuteClickCommand(string path) {
_regionManager.RequestNavigate("ContentRegion", "FileAttente");
}
public bool CanCloseDialog()
{
throw new NotImplementedException();
}
public void OnDialogClosed()
{
throw new NotImplementedException();
}
public void OnDialogOpened(IDialogParameters parameters)
{
throw new NotImplementedException();
}
}
我还有一个 Bootstrapper
class :
public class Bootstrapper : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ModeFileAttente>();
}
}
我的 MVVM 模式运行良好,但是当我想在我的 "HomePage" 和我的 "ModeFileAttente" 视图之间导航时,除了我的视图上的 "System.Object" 之外没有任何反应。
我已经搜索了可能的问题所在,但没有找到可以解决我的问题的方法。
有人在使用这个版本的 prism 时仍然遇到这个错误吗?
PS:我的 2 个视图位于 "View" 文件夹中,与 ViewModel 文件夹中的视图模型相同
"FileAttente" 应该是 "ModeFileAttente":
_regionManager.RequestNavigate("ContentRegion", "ModeFileAttente");
没有 "FileAttente" 视图注册导航。
我认为您应该在初始化时向区域注册视图。试试下面的代码
public class Bootstrapper : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
_regionManager.RegisterViewWithRegion("RegionName", typeof(View));
_regionManager.RegisterViewWithRegion("RegionName", typeof(View2));
}
}
现在您可以使用
_regionManager.RequestNavigate("RegionName", "View");
或
_regionManager.RequestNavigate("RegionName", "View2");
更新
如果我们使用 prism 那么应该有一个与之关联的依赖注入框架,例如 MEF
或 Unity
对于我的项目我正在使用 Prism.Unity.Wpf
如果您使用 Unity
然后你可以要求你的依赖注入器通过添加一个构造函数来注入 RegionManager,该构造函数将由 IRegionManager
注入,见下文
readonly IRegionManager _regionManager;
public Bootstrapper(IRegionManager regionManager)
{
_regionManager = regionManager;
}
当您将 IRegionManager
注入构造函数时 Unity
知道要始终提供相同的实例,因为它在 Unity
中维护,因此无需担心使用相同的实例IRegionManager
别处。
当我们在 XAML 中使用 prism:RegionManager.RegionName="ContentRegion"
时,我们实际上保留了一个占位符来表示 Prism
有一个区域将被任何 VIEW
填充有些点寻找它。
这是我的理解。如果您觉得这有用,请告诉我谢谢。