棱镜6:多个视图从一个ViewModel获取数据

时间:2016-07-19 14:12:16

标签: c# wpf mvvm prism

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

来自网络研讨会的示例:我有名为ViewA + ViewB的View和名为ViewAViewModel + ViewBViewModel的ViewModel。现在我希望Locator为ViewA和ViewB提供ViewAViewModel。

Code ViewA:

<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

1 个答案:

答案 0 :(得分:3)

只要您不是在谈论相同的实例,就可以轻松完成。您只需将ViewModel注册到ViewModelLocationProvider中的View。

ViewModelLocationProvider.Register<ViewB, ViewAViewModel>();

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