Prism 6:多个View从一个ViewModel获取数据

Prism 6: Multiple Views get data from one ViewModel

所以我现在用 Prism 工作一周,这很棒 :) 我现在的问题是:我可以将数据从一个 ViewModel 获取到多个视图(在大多数情况下是 2 个视图)。在本次网络研讨会中,我像 Brian Lagunas 一样使用 ViewModelLocator -> https://www.youtube.com/watch?v=ZfBy2nfykqY

来自网络研讨会的示例:我有名为 ViewA + ViewB 的视图和名为 ViewAViewModel + ViewBViewModel 的 ViewModel。现在我希望定位器为 ViewA 和 ViewB 采用 ViewAViewModel。

代码视图A:

<UserControl x:Class="MVVM_Prism_Mein_einfaches_Beispiel.Views.ViewA"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MVVM_Prism_Mein_einfaches_Beispiel.Views"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Label x:Name="lbl_Firstname" Content="Firstname:" HorizontalAlignment="Left" Margin="61,38,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="tb_Firstname" HorizontalAlignment="Left" Height="23" Margin="129,38,0,0" TextWrapping="Wrap" Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lbl_Lastname" Content="Lastname:" HorizontalAlignment="Left" Margin="61,70,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="tb_Lastname" HorizontalAlignment="Left" Height="23" Margin="129,70,0,0" TextWrapping="Wrap" Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lbl_Updated" Content="Updated:" HorizontalAlignment="Left" Margin="61,102,0,0" VerticalAlignment="Top"/>
        <TextBlock x:Name="txb_Update" HorizontalAlignment="Left" Margin="129,107,0,0" TextWrapping="Wrap" Text="{Binding LastUpdated}" VerticalAlignment="Top"/>
        <Button x:Name="btn_Update" Content="Update" HorizontalAlignment="Left" Margin="129,141,0,0" VerticalAlignment="Top" Width="75" Command="{Binding UpdateCommand}"/>
        <Button x:Name="btn_Upview" Content="Update + View" HorizontalAlignment="Left" Margin="129,171,0,0" VerticalAlignment="Top" Width="75" Command="{Binding NavigateCommand}" CommandParameter="ViewTablet"/>
    </Grid>
</UserControl>

ViewAViewModel:

using MVVM_Prism_Mein_einfaches_Beispiel.Events;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MVVM_Prism_Mein_einfaches_Beispiel.ViewModels
{
    public class ViewAViewModel : BindableBase
    {
        private string _firstName = "Brian";
        public string FirstName
        {
            get { return _firstName; }
            set { SetProperty(ref _firstName, value); }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { SetProperty(ref _lastName, value); }
        }

        private DateTime? _testupdate;
        public DateTime? TestUpdate
        {
            get { return _testupdate; }
            set { SetProperty(ref _testupdate, value); }
        }

        public ICommand UpdateCommand { get; set; }
        private IEventAggregator _eventAggregator;

        public ViewAViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            _eventAggregator = eventAggregator;
            UpdateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => FirstName).ObservesProperty(() => LastName);

            _regionManager = regionManager;
            NavigateCommand = new DelegateCommand<string>(Navigate);
        }

        private IRegionManager _regionManager;

        public DelegateCommand<string> NavigateCommand { get; set; }

        private void Navigate(string view)
        {
            _regionManager.RequestNavigate("ContentRegion", view);
        }

        private bool CanExecute()
        {
            return !String.IsNullOrWhiteSpace(FirstName) && !String.IsNullOrWhiteSpace(LastName);
        }

        private void Execute()
        {
            LastUpdated = DateTime.Now; 
            _eventAggregator.GetEvent<UpdateEvent>().Publish(LastUpdated.ToString());
        }
    }
}

ViewB 应该像 ViewA。

感谢您的帮助。 最好的问候 Shazzar

只要您谈论的不是同一个实例,就可以轻松完成。您只需将 ViewModel 注册到 ViewModelLocationProvider 中的视图即可。

ViewModelLocationProvider.Register<ViewB, ViewAViewModel>();

*请注意,此特定方法是最新预览中的新功能。否则,您必须提供一个工厂。