prism mvvm中的ViewModelLocator
这是我的场景,我需要创建一个简单的uwp应用程序,我有一个单视图模型和多个视图。我使用的是prism mvvm/unity MainPage.xamlprism mvvm中的ViewModelLocator,mvvm,uwp,prism,Mvvm,Uwp,Prism,这是我的场景,我需要创建一个简单的uwp应用程序,我有一个单视图模型和多个视图。我使用的是prism mvvm/unity MainPage.xaml <prism:SessionStateAwarePage x:Class="MvvmSample.Views.MainPage" xmlns:prism="using:Prism.Windows.Mvvm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<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);
);
}
}
public类MainPageViewModel:ViewModelBase
{
公共字符串标题{get;set;}
公共INavigationService导航页面;
公共静态列表名称=新列表(){“Anzal”、“Rashid”、“Kamil”、“Fahad”};
公共ObservableCollection Mynames{get;set;}
公共主页视图模型(INavigationService导航服务)
{
this.Title=“运行时”;
NavigateToPage=导航服务;
for(int i=0;i
NavigateToPage.Navigate(App.expirences.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
}
}
密封部分类应用程序:PrismUnityApplication
{
公共应用程序()
{
this.InitializeComponent();
}
受保护的重写任务OnInitializeAsync(IActivatedEventArgs args)
{
容器.RegisterInstance(this.NavigationService);
返回base.OnInitializeAsync(args);
}
受保护的重写任务OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
this.NavigationService.Navigate(expirences.Main.ToString(),null);
Window.Current.Activate();
返回Task.FromResult(true);
}
公共枚举经验
{
主要的,
第二
}
}
现在问题出现了..我如何将我的第二页绑定到mainpageviewmodel???如何使用我的ViewModelLocator???您想向
ViewModelLocationProvider
注册mainpageviewmodel
,从而覆盖约定:
ViewModelLocationProvider.Register<SecondPage, MainpageViewModel>();
ViewModelLocationProvider.Register();
…最好在导航到第二页之前:-)您希望使用
ViewModelLocationProvider
为SecondPage
注册MainpageViewModel
,从而覆盖约定:
ViewModelLocationProvider.Register<SecondPage, MainpageViewModel>();
ViewModelLocationProvider.Register();
…最好在导航到第二页之前:-)这不起作用..我是否需要解决容器或其他问题???是否出现异常?如何知道它不起作用?当导航到第二个页面时,它不会显示绑定了名称的列表框…虽然没有异常或错误…在视图模型定位器解析方法和视图模型的构造函数中添加断点。如果它到达第一个断点,看看哪里出了问题(例如,它想要解决哪种类型的问题)?事实上,我没有实现viewmodellocator解析方法..而且我已经按照您的指定在app.xaml.cs中的onlaunchapplicationasync方法中注册了viemodel。。我很困惑。你能解释清楚吗???这不起作用。我是否需要解决容器或其他问题???你有例外吗?如何知道它不起作用?当导航到第二个页面时,它不会显示绑定了名称的列表框…虽然没有异常或错误…在视图模型定位器解析方法和视图模型的构造函数中添加断点。如果它到达第一个断点,看看哪里出了问题(例如,它想要解决哪种类型的问题)?事实上,我没有实现viewmodellocator解析方法..而且我已经按照您的指定在app.xaml.cs中的onlaunchapplicationasync方法中注册了viemodel。。我很困惑……你能解释清楚吗???