2016-11-12 108 views
1

这是我的情景,我需要创建一个简单的uwp应用程序,我有一个单一的viewmodel和多个视图..我使用棱镜mvvm /团结。ViewModelLocator棱镜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我可以结合我secondpage到mainpageviewmodel ???如何使用我的ViewModelLocator?

回答

0

要注册MainpageViewModelSecondPageViewModelLocationProvider,从而覆盖了约定:

ViewModelLocationProvider.Register<SecondPage, MainpageViewModel>(); 

...最好导航到第二页:-)

+0

在此之前,不工作..无论我需要解决容器还是什么? –

+0

你有例外吗?你怎么知道它不起作用? – Haukinger

+0

导航到第二页时,它不会显示与名称绑定的列表框......虽然没有例外或错误... –