2016-11-12 5 views
1

ここに私のシナリオがあります。私は単純なuwpアプリケーションを作成する必要があり、私は単一のviewmodelと複数のビューを持っています。プリズムmvvm/unityを使用しています。ViewModelLocator in prism mvvm

MainPage.xamlを

<prism:SessionStateAwarePage 
x:Class="MvvmSample.Views.MainPage" 
xmlns:prism="using:Prism.Windows.Mvvm" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:MvvmSample" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
prism:ViewModelLocator.AutoWireViewModel="True" 
mc:Ignorable="d"> 
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <TextBlock Text="{Binding Title}" FontSize="29.333" /> 
    <Button Content="Navigate" Command="{Binding del}"/> 
</Grid> 

Viewmodels.MainpageViewModel

public class MainPageViewModel : ViewModelBase 
    { 
     public string Title { get; set; } 
     public INavigationService NavigateToPage; 
     public static List<string> names = new List<string>() { "Anzal", "Rashid", "Kamil", "Fahad" }; 
     public ObservableCollection<string> Mynames { get; set; } 
     public MainPageViewModel(INavigationService navigationservice) 
     { 
      this.Title = "Run Time"; 
      NavigateToPage = navigationservice; 
      for (int i = 0; i < names.Count; i++) 
      { 
       Mynames.Add(names[i]); 

      } 
      del = new DelegateCommand(
       () =>          
      NavigateToPage.Navigate(App.Expeirences.Second.ToString(),null); 
       ); 
     } 
    } 

SecondPage.xaml

<prism:SessionStateAwarePage 
x:Class="Design3.Views.SecondPage" 
xmlns:prism="using:Prism.Windows.Mvvm" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:Design3" 
prism:ViewModelLocator.AutoWireViewModel="True" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"> 
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <ListBox ItemsSource="{Binding names}"/> 
</Grid> 

App.xaml.cs今

 sealed partial class App : PrismUnityApplication 
{ 
    public App() 
    { 
     this.InitializeComponent(); 
    } 
    protected override Task OnInitializeAsync(IActivatedEventArgs args) 
    { 
     Container.RegisterInstance<INavigationService>(this.NavigationService); 
     return base.OnInitializeAsync(args); 
    } 
    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args) 
    { 
     this.NavigationService.Navigate(Expeirences.Main.ToString(), null); 
     Window.Current.Activate(); 
     return Task.FromResult(true); 
    } 
    public enum Expeirences 
    { 
     Main, 
     Second 
    } 

} 

occurs..Howは私ViewModelLocatorを使用する方法??? mainpageviewmodelために私secondpageをバインドすることができ、問題?

答えて

0

あなたは、このように規則をオーバーライドし、ViewModelLocationProviderSecondPageためMainpageViewModelを登録したい:

ViewModelLocationProvider.Register<SecondPage, MainpageViewModel>(); 

...好ましくは動作しません

+0

:-) 2番目のページに移動する前に...私はコンテナか何かを解決する必要があるかどうか??? –

+0

例外はありますか?それがうまくいかないことをどのように知っていますか? – Haukinger

+0

2番目のページに移動すると、リストボックスに名前がバインドされていません...例外もエラーもありません。 –

関連する問題