WPF---Prism视图传参

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

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

第一,事件聚合器(EventAggregator)

1. 定义事件

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

using Prism.Events;public class DataUpdateEvent : PubSubEvent<string>
{
}

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

2. 注册事件聚合器

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

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

3. 定义视图和视图模型

Tab1View.xaml
<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

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

<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

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
<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
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. 定义共享服务
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 中注册服务。

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{containerRegistry.RegisterSingleton<ISharedDataService, SharedDataService>();containerRegistry.RegisterForNavigation<Tab1View, Tab1ViewModel>();containerRegistry.RegisterForNavigation<ListStaticView, ListStaticViewModel>();
}
3. 使用共享服务
Tab1ViewModel.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

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 区域。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/50328.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

手持式气象检测设备:便携科技,气象探测

一、手持式气象检测设备&#xff1a;小巧身躯&#xff0c;大能量 手持式气象检测设备&#xff0c;顾名思义&#xff0c;是一种可以手持操作的气象监测工具。它集成了温度、湿度、气压、风速风向等多种传感器&#xff0c;能够实时获取气象数据&#xff0c;并通过显示屏或手机APP…

Leetcode—240. 搜索二维矩阵 II【中等】

2024每日刷题&#xff08;149&#xff09; Leetcode—240. 搜索二维矩阵 II 实现代码 class Solution { public:bool searchMatrix(vector<vector<int>>& matrix, int target) {int r 0;int c matrix[0].size() - 1;while(r < matrix.size() &&…

服务器数据恢复—raid信息丢失导致RAID无法被识别的数据恢复案例

服务器数据恢复环境&故障&#xff1a; 某单位机房搬迁&#xff0c;将所有服务器和存储搬迁到新机房并重新连接线路&#xff0c;启动所有机器发现其中有一台服务器无法识别RAID&#xff0c;提示未做初始化操作。 发生故障的这台服务器安装LINUX操作系统&#xff0c;配置了NF…

vue3创建vite项目

一、创建vue3 vite项目&#xff1a; 命令行创建&#xff1a;npm create vitelatest vue3-tdly-demo -- --template vue (1)先进入项目文件夹&#xff0c;cd vue3-tdly-demo (2)之后执行&#xff0c; npm install (3)最后运行&#xff0c;npm run dev 将main.js文件内容改成…

【leetcode】两数相加【中等】(C++递归解法)

总体来说&#xff0c;链表类问题往往是蛮适合用递归的方式求解的 要写出有效的递归&#xff0c;关键是要考虑清楚&#xff1a; 0. return的条件 1. 每步递归的操作&#xff0c;以及何时调用下一步递归 2. 鲁棒性&#xff08;头&#xff0c;尾结点等特殊情况是否依旧成立&am…

Golang学习笔记20240725,Go语言基础语法

第一个Go程序 package mainimport "fmt"func main() {fmt.Println("hello world") }运行方式1&#xff1a; go run main.go运行方式2&#xff1a; go build .\hello_go.exe运行方式3&#xff1a;goland右键运行 字符串拼接 使用加号可以对字符串进行…

Codeforces Round 874 (Div. 3)(A~D题)

A. Musical Puzzle 思路: 用最少的长度为2的字符串按一定规则拼出s。规则是&#xff1a;前一个字符串的尾与后一个字符串的首相同。统计s中长度为2的不同字符串数量。 代码: #include<bits/stdc.h> #include <unordered_map> using namespace std; #define N 20…

【python】PyQt5中QPushButton的用法详细解析与应用实战

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…

【全面介绍Python多线程】

🎥博主:程序员不想YY啊 💫CSDN优质创作者,CSDN实力新星,CSDN博客专家 🤗点赞🎈收藏⭐再看💫养成习惯 ✨希望本文对您有所裨益,如有不足之处,欢迎在评论区提出指正,让我们共同学习、交流进步! 🦇目录 1. 🦇前言2. 🦇threading 模块的基本用法3. 🦇Thre…

Unity中有关Animation的一点笔记

也许更好的阅读体验 Animation Unity中Animation类并不是直接记载了和播放动画有关的信息&#xff0c;可以简单理解Animation为一个动画播放器&#xff0c;播放的具体内容就像卡带一样&#xff0c;当我们有了卡带后我们可以播放动画。 对应的则是编辑器中的组件 所以Anima…

【学术会议征稿】第十一届电气工程与自动化国际会议 (IFEEA 2024)

第十一届电气工程与自动化国际会议 &#xff08;IFEEA 2024&#xff09; 2024 11th International Forum on Electrical Engineering and Automation IFEEA论坛属一年一度的国际学术盛会。因其影响力及重要性&#xff0c;IFEEA论坛自创建筹办以来&#xff0c;便受到国内外高等…

网站打包封装成app,提高用户体验和商业价值

网站打包封装成app的优势 随着移动互联网的普及&#xff0c;用户对移动应用的需求越来越高。网站打包封装成app可以满足用户的需求&#xff0c;提高用户体验和商业价值。 我的朋友是一名电商平台的运营负责人&#xff0c;他曾经告诉我&#xff0c;他们的网站流量主要来自移动…

由bext安装“异常”引出的话题:windows上转义字符的工作原理

由bext安装“异常”引出的话题&#xff1a;Windows上转义字符的工作原理&#xff0c;与ai“闲扯”不经意学习知识点。 (笔记模板由python脚本于2024年07月25日 19:21:13创建&#xff0c;本篇笔记适合喜欢用ai学习的coder翻阅) 【学习的细节是欢悦的历程】 Python 官网&#xff…

GitLab添加TortoiseGIT生成SSH Key

文章目录 前言一、PuTTYgen二、GitLab 前言 GitLab是一个用于托管代码仓库和项目管理的Web平台&#xff0c;公司搭建自己的gitlab来管理代码&#xff0c;我们在clone代码的时候可以选择http协议&#xff0c;也可以选择ssh协议来拉取代码。 SSH (Secure Shell)是一种通过网络进…

【脚本】清空指定文件夹内容

main执行一次&#xff0c;1.txt就会写入一些东西。 原来的想法是覆盖重写&#xff0c;结果却是接着往后面写&#xff0c;检查源代码有点费事&#xff0c;不如在每次程序执行前&#xff0c;先直接清空文件夹&#xff01; 部分代码&#xff1a; 修改路径就能用。 import os im…

微信小程序-自定义tabBar

通过官网给出的示例自己实现了自定义的tabBar&#xff0c;但结果发现 无法监听页面生命周期函数 结语&#xff1a;原想的是实现不一样的效果&#xff08;如下&#xff09; 故尝试了自定义tabBar&#xff0c;虽然做出来了&#xff0c;但也发现这个做法存在不足&#xff1a; 在…

记一次Mycat分库分表实践

接了个活,又搞分库分表。 一、分库分表 在系统的研发过程中,随着数据量的不断增长,单库单表已无法满足数据的存储需求,此时就需要对数据库进行分库分表操作。 分库分表是随着业务的不断发展,单库单表无法承载整体的数据存储时,采取的一种将整体数据分散存储到不同服务…

Golang | 腾讯一面

go的调度 Golang的调度器采用M:N调度模型&#xff0c;其中M代表用户级别的线程(也就是goroutine)&#xff0c;而N代表的事内核级别的线程。Go调度器的主要任务就是N个OS线程上调度M个goroutine。这种模型允许在少量的OS线程上运行大量的goroutine。 Go调度器使用了三种队列来…

vue3 常用的知识点

setup:容许在script当中书写组合式API 并且vue3的template不再要求唯一的根元素 <script setup>const name app; </script>组合式API的用法&#xff1a; 可以直接在script标签中定义变量或者函数&#xff0c;然后直接在template当中使用 <template>{{mes…

编程类精品GPTs

文章目录 编程类精品GPTs前言种类ChatGPT - GrimoireProfessional-coder-auto-programming 总结 编程类精品GPTs 前言 代码类的AI, 主要看以下要点: 面对含糊不清的需求是否能引导出完整的需求面对完整的需求是否能分步编写代码完成需求编写的代码是否具有可读性和可扩展性 …