2017-05-24 10 views
0

私は、プリズムと地域を使ってビューベースのナビゲーションを動作させようとしています。私はMSDNのドキュメントを調べてみましたが、何らかの理由でそれを動作させることができず、何が間違っているのか分かりません。だから、これは私がこれまでに得たものである:Wpf Prism Region Navigation

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

サイドノート:私は私はNavigateメソッドということで入れていた、その後多くのチュートリアルの一つ、私はそれが必要なのですか? 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 
} 

だから、基本的にはIダッシュボード(私の現在のスタートアップビュー)から、ボタンをクリックしてコンテナに登録されている他のビューにアクセスできるようにしたいだけです。

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()メソッドを呼び出す場所を表示できますか?また、表示されているエラーメッセージの詳細を提供できますか?(あなたがメインのVMで行うように)ただ、ダブルチェックに、あなたはあなたのダッシュボードVMにIRegionManagerを注入しなければならない、とIRegionManagerはあなたのブートストラップの*シングルトン*として登録する必要があります。 –

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

これは、ViewAとViewBを登録するMainWindowを与えます。 がViewA上のボタンからViewBへのナビゲーションを可能にするには、以下のニーズがViewAViewModelで行われる:

<Button Content="Navigate" Command="{Binding ButtonCommand/> 
:ViewAはあなたが必要とする最後の事はもちろん、ボタンそのものであるXAML形式で

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

+0

そう、私は答え、感謝の男を持っている!それが私のやり方です。 – Hypergyzed

0

チェックアウトsampels 17、18、および19これは、あなたが正しい方向に行く得るのを助ける必要があります:あなたはすでに答えを得た場合

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

+0

例を見ると、それらを複製するとエラーが表示され続けます。私はそれらがすべて1つのビューしか持たず、それがMainWindowだと気付きました。まあ、私はメインシェルを持っていて、そのシェルには私が注入したビューです。私はビュー内のボタンを押すと、そのシェルの中に別のビューを表示するようにしています。私はさまざまなバリエーションを試してきましたが、まだ運がありません。何か案は? – Hypergyzed

+0

サンプルを実行しましたか?それはあなたが地域の複数のビューをナビゲートすることができるシェルを持っています。ビューを簡単に移動するには、ボタンをクリックします。あなたはたぶん全体の概念を完全に欠落しているでしょう。それは実際には非常に簡単です。 –

+0

あなたのビューはナビゲーション用に登録されていることを確認してから、IRegionManager.RequestNaviagteを呼び出してください。 –

関連する問題