WPF---Prism视图传参

Prism视图传参方式。
实际应用场景

点击tabitem中的列表数据,同步更新到ListStatic Region对应的界面。目前用两种方式实现了传参数据同步。

第一,事件聚合器(EventAggregator)

1. 定义事件

创建一个事件类,用于传递数据。

cs 复制代码
using Prism.Events;

public class DataUpdateEvent : PubSubEvent<string>
{
}

点击tabitem中的列表 ,示例传参数据是string类型,什么参数类型都可以。

2. 注册事件聚合器

App.xaml.cs 中注册事件聚合器。

cs 复制代码
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterSingleton<IEventAggregator, EventAggregator>();
    containerRegistry.RegisterForNavigation<Tab1View, Tab1ViewModel>();
    containerRegistry.RegisterForNavigation<ListStaticView, ListStaticViewModel>();
}

3. 定义视图和视图模型

Tab1View.xaml
cs 复制代码
<UserControl x:Class="YourNamespace.Tab1View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:prism="http://prismlibrary.com/">
    <Grid>
        <DataGrid ItemsSource="{Binding DataList}" SelectedItem="{Binding SelectedItem}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <prism:InvokeCommandAction Command="{Binding ItemSelectedCommand}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </DataGrid>
    </Grid>
</UserControl>

Tab1ViewModel.cs

cs 复制代码
using Prism.Commands;
using Prism.Mvvm;
using Prism.Events;
using System.Collections.ObjectModel;

public class Tab1ViewModel : BindableBase
{
    private readonly IEventAggregator _eventAggregator;
    private string _selectedItem;

    public ObservableCollection<string> DataList { get; private set; }
    public string SelectedItem
    {
        get { return _selectedItem; }
        set { SetProperty(ref _selectedItem, value); }
    }

    public DelegateCommand<string> ItemSelectedCommand { get; private set; }

    public Tab1ViewModel(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        DataList = new ObservableCollection<string> { "Item 1", "Item 2", "Item 3" };
        ItemSelectedCommand = new DelegateCommand<string>(OnItemSelected);
    }

    private void OnItemSelected(string selectedItem)
    {
        if (selectedItem != null)
        {
            _eventAggregator.GetEvent<DataUpdateEvent>().Publish(selectedItem);
        }
    }
}

ListStaticView.xaml

cs 复制代码
<UserControl x:Class="YourNamespace.ListStaticView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <TextBlock Text="{Binding UpdatedData}" />
    </Grid>
</UserControl>

ListStaticViewModel.cs

cs 复制代码
using Prism.Mvvm;
using Prism.Events;

public class ListStaticViewModel : BindableBase
{
    private readonly IEventAggregator _eventAggregator;
    private string _updatedData;

    public string UpdatedData
    {
        get { return _updatedData; }
        set { SetProperty(ref _updatedData, value); }
    }

    public ListStaticViewModel(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        _eventAggregator.GetEvent<DataUpdateEvent>().Subscribe(OnDataUpdate);
    }

    private void OnDataUpdate(string updatedData)
    {
        UpdatedData = updatedData;
    }
}

4. 定义主窗口布局

MainWindow.xaml
cs 复制代码
<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/">
    <Grid>
        <TabControl Grid.Column="0">
            <TabItem Header="患者列表" prism:RegionManager.RegionName="Tab1Region"/>
            <TabItem Header="到检列表" prism:RegionManager.RegionName="Tab2Region"/>
            <TabItem Header="WorkList" prism:RegionManager.RegionName="Tab3Region"/>
        </TabControl>
        <ContentControl Margin="300 0 0 0" prism:RegionManager.RegionName="ListStatic"/>
    </Grid>
</Window>

5. 配置导航

确保在应用启动时正确导航到初始视图。

MainWindowViewModel.cs
cs 复制代码
using Prism.Mvvm;
using Prism.Regions;

public class MainWindowViewModel : BindableBase
{
    private readonly IRegionManager _regionManager;

    public MainWindowViewModel(IRegionManager regionManager)
    {
        _regionManager = regionManager;
        _regionManager.RegisterViewWithRegion("Tab1Region", typeof(Tab1View));
        _regionManager.RegisterViewWithRegion("ListStatic", typeof(ListStaticView));
    }
}
第二,使用共享服务

使用共享服务可以在视图之间共享数据,并在一个视图中更新数据时通知另一个视图进行更新。

1. 定义共享服务
cs 复制代码
public interface ISharedDataService
{
    string SharedData { get; set; }
    event Action<string> DataChanged;
    void UpdateData(string data);
}

public class SharedDataService : ISharedDataService
{
    private string _sharedData;
    public string SharedData
    {
        get => _sharedData;
        set
        {
            _sharedData = value;
            DataChanged?.Invoke(_sharedData);
        }
    }

    public event Action<string> DataChanged;

    public void UpdateData(string data)
    {
        SharedData = data;
    }
}
2. 注册服务

App.xaml.cs 中注册服务。

cs 复制代码
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterSingleton<ISharedDataService, SharedDataService>();
    containerRegistry.RegisterForNavigation<Tab1View, Tab1ViewModel>();
    containerRegistry.RegisterForNavigation<ListStaticView, ListStaticViewModel>();
}
3. 使用共享服务
Tab1ViewModel.cs
cs 复制代码
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.ObjectModel;

public class Tab1ViewModel : BindableBase
{
    private readonly ISharedDataService _sharedDataService;
    private string _selectedItem;

    public ObservableCollection<string> DataList { get; private set; }
    public string SelectedItem
    {
        get { return _selectedItem; }
        set { SetProperty(ref _selectedItem, value); }
    }

    public DelegateCommand<string> ItemSelectedCommand { get; private set; }

    public Tab1ViewModel(ISharedDataService sharedDataService)
    {
        _sharedDataService = sharedDataService;
        DataList = new ObservableCollection<string> { "Item 1", "Item 2", "Item 3" };
        ItemSelectedCommand = new DelegateCommand<string>(OnItemSelected);
    }

    private void OnItemSelected(string selectedItem)
    {
        if (selectedItem != null)
        {
            _sharedDataService.UpdateData(selectedItem);
        }
    }
}

ListStaticViewModel.cs

cs 复制代码
using Prism.Mvvm;

public class ListStaticViewModel : BindableBase
{
    private readonly ISharedDataService _sharedDataService;
    private string _updatedData;

    public string UpdatedData
    {
        get { return _updatedData; }
        set { SetProperty(ref _updatedData, value); }
    }

    public ListStaticViewModel(ISharedDataService sharedDataService)
    {
        _sharedDataService = sharedDataService;
        _sharedDataService.DataChanged += OnDataChanged;
    }

    private void OnDataChanged(string updatedData)
    {
        UpdatedData = updatedData;
    }
}

以上两种方法都可以实现从 Tab1Region 中的列表数据同步更新到 ListStatic 区域。

相关推荐
师太,答应老衲吧37 分钟前
SQL实战训练之,力扣:2020. 无流量的帐户数(递归)
数据库·sql·leetcode
Channing Lewis2 小时前
salesforce case可以新建一个roll up 字段,统计出这个case下的email数量吗
数据库·salesforce
毕业设计制作和分享3 小时前
ssm《数据库系统原理》课程平台的设计与实现+vue
前端·数据库·vue.js·oracle·mybatis
ketil273 小时前
Redis - String 字符串
数据库·redis·缓存
Hsu_kk4 小时前
MySQL 批量删除海量数据的几种方法
数据库·mysql
编程学无止境4 小时前
第02章 MySQL环境搭建
数据库·mysql
knight-n4 小时前
MYSQL库的操作
数据库·mysql
包饭厅咸鱼5 小时前
QML----复制指定下标的ListModel数据
开发语言·数据库
生命几十年3万天5 小时前
redis时间优化
数据库·redis·缓存
Elastic 中国社区官方博客5 小时前
释放专利力量:Patently 如何利用向量搜索和 NLP 简化协作
大数据·数据库·人工智能·elasticsearch·搜索引擎·自然语言处理