2017-05-24 53 views
0

我一直在试图让基于视图的导航工作使用棱镜和地区。我尝试通过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。我没有得到任何错误,直到我运行它,并按下了应该链接到下一个视图的按钮。

任何帮助,将不胜感激!

谢谢!

+0

您能否显示仪表板VM代码的相关部分,即您在何处创建IRegionManager以及您在哪里调用其RequestNavigation()方法?另外,您能否提供您所看到的错误消息的详细信息?为了仔细检查,您应该将IRegionManager注入仪表板VM(就像您对主VM做的一样),并且IRegionManager必须在引导程序中注册为* singleton *。 –

+0

对,对不起。我刚刚添加了它。 xD – Hypergyzed

回答

1

不知道,如果你已经得到了答案,但我也陷入了同样的今天的事情。布赖恩的评论给了我必要的提示。

我在引导程序中有以下代码。这注册了两个视图以允许导航到它们:

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"); 
    } 
} 

这给出了一个MainWindow,它注册了ViewA和ViewB。 为了允许导航ViewB从ViewA一个按钮,在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"); 
    } 
} 

在XAML形式ViewA你需要的最后一件事是当然的按钮本身:

<Button Content="Navigate" Command="{Binding ButtonCommand/> 
+0

是的,我得到了答案,谢谢你的男人!这是我做到的。 – Hypergyzed

0

退房sampels 17,18和19这应该有助于让你在正确的方向前进:

https://github.com/PrismLibrary/Prism-Samples-Wpf

+0

看着例子,并复制它们不断给我错误。我注意到他们都只有一个视图,那就是MainWindow。那么我有一个Mainshelll,在那个shell里面是我注入的视图。我试图让它在该视图内按下按钮时在该shell中显示另一个视图。我一直在尝试不同的变化,但仍然没有运气。有任何想法吗? – Hypergyzed

+0

你运行过样品吗?它有一个可以让你浏览该地区多个视图的shell。您可以通过单击按钮轻松导航视图。你可能完全忽略了整个概念。这其实很简单。 –

+0

你所要做的就是确保你的视图已经注册了导航,然后调用IRegionManager.RequestNaviagte,就是这样。 –