棱镜6:多个视图从一个ViewModel获取数据
本文关键字:一个 ViewModel 数据 获取 视图 棱镜 | 更新日期: 2023-09-27 17:57:35
所以我现在用Prism工作了一周,这很好:)我现在的问题是:我能从一个ViewModel到多个视图(在大多数情况下是两个视图)获取数据吗。我在这次网络研讨会中使用了像Brian Lagunas这样的ViewModelLocator->https://www.youtube.com/watch?v=ZfBy2nfykqY
网络研讨会的例子:我有名为ViewA+ViewB的视图和名为ViewAViewModel+ViewBViewModel的ViewModels。现在,我希望定位器对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>();
*请注意,此特定方法是最新预览中的一个新功能。否则,您必须提供一个工厂。