Wpf 棱镜区域导航
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44161739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wpf Prism Region Navigation
提问by Hypergyzed
I've been trying to get view-based navigation to work using Prism and regions. I tried going through the documentation on MSDN, but for some reason I can't get it to work, and I don't know what I am doing wrong. So this is what I got so far:
我一直在尝试使用 Prism 和区域让基于视图的导航工作。我尝试浏览 MSDN 上的文档,但由于某种原因我无法让它工作,而且我不知道我做错了什么。所以这就是我到目前为止所得到的:
MainShellViewModel.cs
MainShellViewModel.cs
//Private Variables
private readonly IRegionManager _regionManager;
//Public Variables
public DelegateCommand<string> NavigateCommand { get; set; }
//Functions and Methods
public MainShellViewModel(IRegionManager regionManager)
{
//Region Manager
_regionManager = regionManager;
NavigateCommand = new DelegateCommand<string>(Navigate);
Initialize();
}
public void Initialize()
{
//Startup View
_regionManager.RegisterViewWithRegion("ViewMainFrame", typeof(Views.Dashboard));
}
public void Navigate(string uri)
{
//Navigation
if(uri != null)
{
_regionManager.RequestNavigate("ViewMainFrame", uri);
}
}
Side note: One of the many tutorials I followed had me put in that Navigate method, do I need it? I am using the MainShellViewModel as the Main view that is injected on startup.
旁注:我遵循的众多教程之一让我使用了 Navigate 方法,我需要它吗?我使用 MainShellViewModel 作为在启动时注入的主视图。
DashboardViewModel.cs: (Contains the error)
DashboardViewModel.cs:(包含错误)
{
//Private Variables
private bool _canExercise = true;
//Public Variables
public bool CanExercise()
{
return _canExercise;
}
RelayCommand _exerciseSelCommand;
public ICommand ExerciseSelCommand
{
get
{
if (_exerciseSelCommand == null)
_exerciseSelCommand = new RelayCommand(ExerciseSel, CanExercise);
return _exerciseSelCommand;
}
}
//Dashboard Functions and Methods
IRegion _regionManager;
private void ExerciseSel()
{
SoundPlayers.ButtonSound();
_regionManager.RequestNavigate(new Uri("ExerciseView", UriKind.Relative)); //This gives me the error, it says it can't be nullable?
}
Here is where I register my containers/views, etc.
这是我注册容器/视图等的地方。
Bootstrapper.cs:
Bootstrapper.cs:
protected override void ConfigureContainer()
{
base.ConfigureContainer();
#region Region Register Zone
//register views here!
Container.RegisterType(typeof(object), typeof(Views.LoginView), "LoginView");
Container.RegisterType(typeof(object), typeof(Views.Dashboard), "Dashboard");
Container.RegisterType(typeof(object), typeof(Views.ExerciseView), "SettingsView");
Container.RegisterType(typeof(object), typeof(Views.ResultsView), "ResultsView");
Container.RegisterType(typeof(object), typeof(Views.UserCreationView), "UserCreationView");
#endregion
}
So basically I just want to be able to get from the Dashboard (Which is my current startup view) to any other view that's registered in my containers on a click of a button.
所以基本上我只是希望能够通过单击按钮从仪表板(这是我当前的启动视图)转到在我的容器中注册的任何其他视图。
MainShell.xaml:
MainShell.xaml:
<Window x:Name="Shell"
x:Class="Equinox.Views.MainShell"
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:prism="http://www.codeplex.com/prism"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="Equinox"
FontFamily="Quicksand"
Height="900"
Width="1500"
SizeToContent="WidthAndHeight"
ResizeMode="CanResize"
Background="#EEF3F4"
WindowStyle="SingleBorderWindow"
Icon="/Equinox;component/favicon.ico"
WindowStartupLocation="CenterScreen">
<!-- Main View Region -->
<ContentControl x:Name="ContentControlMain"
prism:RegionManager.RegionName="ViewMainFrame"
Focusable="False"/>
However, I keep getting errors when I try to make my region take another view. The way I was doing it was by using my DashboardViewModel, and creating another IRegionManagernamed _regionManager, and doing the RequestNavigation. I got no errors until I ran it and pressed the button that should link me to the next view.
但是,当我尝试让我的地区采取另一种观点时,我不断收到错误消息。我这样做的方式是使用我的 DashboardViewModel,然后创建另一个IRegionManager名为 的_regionManager,然后执行 RequestNavigation。在我运行它并按下应该将我链接到下一个视图的按钮之前,我没有遇到任何错误。
Any help would be appreciated!
任何帮助,将不胜感激!
Thanks!
谢谢!
回答by Matthijs de Regt
Not sure if you got the answer already, but I was running into the same thing today. The comment from Brian gave me the necessary hint.
不确定你是否已经得到了答案,但我今天遇到了同样的事情。布赖恩的评论给了我必要的提示。
I have the following code in the bootstrapper. This registers the two views to allow navigation to them:
我在引导程序中有以下代码。这将注册两个视图以允许导航到它们:
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.RegisterTypeForNavigation<ViewA>("ViewA");
Container.RegisterTypeForNavigation<ViewB>("ViewB");
}
}
This gives a MainWindow, which registers ViewA and ViewB. To allow for navigation to ViewB from a button on ViewA, the following needs to be done in ViewAViewModel:
这给出了一个 MainWindow,它注册了 ViewA 和 ViewB。要允许从 ViewA 上的按钮导航到 ViewB,需要在 ViewAViewModel 中完成以下操作:
public class ViewAViewModel: BindableBase
{
private readonly IRegionManager _regionManager;
public ViewAViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
ButtonCommand= new DelegateCommand(ButtonClicked);
}
private void ButtonClicked()
{
_regionManager.RequestNavigate("ContentRegion", "ViewB");
}
}
In the XAML form ViewA the last thing you need is of course the button itself:
在 XAML 表单 ViewA 中,您需要的最后一件事当然是按钮本身:
<Button Content="Navigate" Command="{Binding ButtonCommand}"/>
回答by Brian Lagunas
Check out sampels 17, 18, and 19. This should help get you going in the right direction:
查看样本 17、18 和 19。这应该有助于让您朝着正确的方向前进:

