C# Wpf棱镜区域导航
我一直在尝试使用棱镜和区域实现基于视图的导航。我试着浏览MSDN上的文档,但由于某种原因,我无法让它工作,我不知道我做错了什么。这就是我到目前为止得到的结果: MainShellViewModel.csC# Wpf棱镜区域导航,c#,wpf,xaml,prism,C#,Wpf,Xaml,Prism,我一直在尝试使用棱镜和区域实现基于视图的导航。我试着浏览MSDN上的文档,但由于某种原因,我无法让它工作,我不知道我做错了什么。这就是我到目前为止得到的结果: MainShellViewModel.cs //Private Variables private readonly IRegionManager _regionManager; //Public Variables public DelegateCommand<string> NavigateCommand { get;
//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);
}
}
这里是我注册容器/视图等的地方
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,创建另一个名为\u regionManager
的IRegionManager
,并执行请求导航。在运行它并按下按钮将我链接到下一个视图之前,我没有收到任何错误
任何帮助都将不胜感激
谢谢 查看样本17、18和19。这将有助于您朝着正确的方向前进:
不确定你是否已经得到了答案,但我今天遇到了同样的事情。布赖恩的评论给了我必要的暗示 我在引导程序中有以下代码。这将注册两个视图以允许导航到它们:
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");
}
}
在XAML表单ViewA中,您最不需要的当然是按钮本身:
<Button Content="Navigate" Command="{Binding ButtonCommand}"/>
能否显示仪表板虚拟机代码的相关部分,即在何处创建IRegionManager以及在何处调用其RequestNavigation()方法?另外,您能否提供所看到的错误消息的详细信息?为了再次检查,您应该将IRegionManager注入您的仪表板虚拟机(就像您对主虚拟机所做的那样),并且IRegionManager必须在您的引导程序中注册为singleton。是的,对此感到抱歉。我刚刚加了。看着这些例子,复制它们总是让我出错。我注意到它们都只有一个视图,那就是主窗口。我有一个主壳,壳中有一个我注入的视图。当我按下视图中的一个按钮时,我试图让它在该shell中显示另一个视图。我一直在尝试不同的变化,但仍然没有运气。有什么想法吗?你做过样品吗?它有一个shell,允许您在区域中导航多个视图。通过单击按钮,可以轻松地在视图之间导航。你可能完全忽略了整个概念。这其实很简单。你所要做的就是确保你的视图已注册为导航,然后调用IRegionManager.RequestNaviagte,就这样。是的,我很可能错过了它,我是一个相对较新的lol。我仍在努力理解它。这个问题,还有这个:这就是我到目前为止得到的。所以当你说注册时,我想这就是我的引导程序中容器的用途?看看我提供的样本并运行它们。我得到了答案,谢谢!我就是这样做的。
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");
}
}
<Button Content="Navigate" Command="{Binding ButtonCommand}"/>