gpt4 book ai didi

mvvm - Prism mvvm 中的 ViewModelLocator

转载 作者:行者123 更新时间:2023-12-03 10:43:33 25 4
gpt4 key购买 nike

这是我的场景,我需要创建一个简单的 uwp 应用程序,并且我有一个 View 模型和多个 View ..我正在使用 prism mvvm/unity 。

主页.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);
);
}
}

第二页.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>

应用程序.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
}

}

现在问题出现了..如何将我的第二页绑定(bind)到 mainpageviewmodel???如何使用我的 ViewModelLocator??

最佳答案

您要注册MainpageViewModel对于 SecondPageViewModelLocationProvider ,从而覆盖约定:

ViewModelLocationProvider.Register<SecondPage, MainpageViewModel>();

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

关于mvvm - Prism mvvm 中的 ViewModelLocator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40567134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com