我一直在试图让基于视图的导航工作使用棱镜和地区。我尝试通过MSDN上的文档,但由于某种原因,我无法使其工作,并且我不知道我做错了什么。所以这是我走到这一步:Wpf Prism地区导航
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);
}
}
边注:在众多的教程我也跟着把我摆在那导航的方法,我需要它?我将MainShellViewModel用作启动时注入的主视图。
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?
}
这里是我注册我的容器/视图等
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
}
所以基本上我只是希望能够从仪表板(这是我当前的启动视图)获取到通过单击按钮在我的容器中注册的任何其他视图。
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"/>
不过,我不断收到错误,当我试图让我的区域采取另一种看法。我这样做的方式是使用我的DashboardViewModel,并创建名为_regionManager
的另一个IRegionManager
,并执行RequestNavigation。我没有得到任何错误,直到我运行它,并按下了应该链接到下一个视图的按钮。
任何帮助,将不胜感激!
谢谢!
您能否显示仪表板VM代码的相关部分,即您在何处创建IRegionManager以及您在哪里调用其RequestNavigation()方法?另外,您能否提供您所看到的错误消息的详细信息?为了仔细检查,您应该将IRegionManager注入仪表板VM(就像您对主VM做的一样),并且IRegionManager必须在引导程序中注册为* singleton *。 –
对,对不起。我刚刚添加了它。 xD – Hypergyzed