WPF Prism

news/2025/9/23 14:09:06/文章来源:https://www.cnblogs.com/Fred1987/p/19107086
Install-Package Prism.WPF;
Install-Package Prism.DryIOC;

 

 

//App.xaml
<prism:PrismApplication x:Class="WpfApp37.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp37"xmlns:prism="http://prismlibrary.com/"><Application.Resources></Application.Resources>
</prism:PrismApplication>//App.xaml.cs
using System.Configuration;
using System.Data;
using System.Windows;
using Prism.DryIoc;
using Prism.Ioc;
using WpfApp37.ViewModels;
using WpfApp37.Views;namespace WpfApp37
{/// <summary>/// Interaction logic for App.xaml/// </summary>public partial class App : PrismApplication{protected override Window CreateShell(){return Container.Resolve<MainWindow>();}protected override void RegisterTypes(IContainerRegistry containerRegistry){containerRegistry.RegisterForNavigation<ViewA, ViewAViewModel>("ViewA");containerRegistry.RegisterForNavigation<ViewB, ViewBViewModel>("ViewB");containerRegistry.RegisterForNavigation<MainWindow, MainWindowViewModel>("MainWindow");containerRegistry.RegisterDialog<MessageBoxView, MessageBoxViewModel>("MessageBoxView");}protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings){base.ConfigureRegionAdapterMappings(regionAdapterMappings);}protected override void OnInitialized(){base.OnInitialized();var regionManager=Container.Resolve<IRegionManager>();regionManager.RequestNavigate("MainRegion", "ViewA");}protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);}}}

 

//ViewA.xaml
<UserControl x:Class="WpfApp37.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:WpfApp37.Views"mc:Ignorable="d"  xmlns:prism="http://prismlibrary.com/"prism:ViewModelLocator.AutoWireViewModel="True"VerticalAlignment="Stretch"HorizontalAlignment="Stretch"><Grid Margin="20"><StackPanel><TextBlock Text="{Binding Message}"FontSize="50"FontWeight="Bold"Foreground="Blue"/><Button Content="Click Me!"Command="{Binding ClickCommand}"Margin="0,10,0,0"Padding="10"/><TextBlock Text="{Binding ClickCount}"FontSize="50"Margin="0,10,0,0"/></StackPanel>            </Grid>
</UserControl>//ViewA.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApp37.Views
{/// <summary>/// Interaction logic for ViewA.xaml/// </summary>public partial class ViewA : UserControl{public ViewA(){InitializeComponent();}}
}//using System;
using System.Collections.Generic;
using System.Text;namespace WpfApp37.ViewModels
{public class ViewAViewModel: ViewModelBase{private int clickCount;public int ClickCount{get{return clickCount;}set{SetProperty(ref clickCount, value);}}private string message = "Welcome to View A!";public string Message{get{return message;}set{SetProperty(ref message, value);}}public DelegateCommand ClickCommand { get; set; }public ViewAViewModel(){Title="View A";ClickCommand=new DelegateCommand(ClickCommandExecuted);}private void ClickCommandExecuted(){ClickCount++;Message=$"Button clicked {ClickCount} times";}}
}

 

 

//ViewB.xaml
<UserControl x:Class="WpfApp37.Views.ViewB"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:WpfApp37.Views"mc:Ignorable="d"VerticalAlignment="Stretch"HorizontalAlignment="Stretch"xmlns:prism="http://prismlibrary.com/"prism:ViewModelLocator.AutoWireViewModel="True"><Grid Margin="20"><Grid.RowDefinitions><RowDefinition Height="60"/><RowDefinition Height="*"/><RowDefinition Height="80"/></Grid.RowDefinitions><TextBlock Text="View B -Data Display"Grid.Row="0"FontSize="50"FontWeight="Bold"/><DataGrid  Grid.Row="1"ItemsSource="{Binding Items,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"AutoGenerateColumns="False"FontSize="30"Margin="0,10,0,0"VerticalAlignment="Stretch"><DataGrid.Columns><DataGridTextColumn Header="Id" Binding="{Binding Id}"/><DataGridTextColumn Header="Name" Binding="{Binding Name}"/><DataGridTextColumn Header="ISBN" Binding="{Binding ISBN}"/><DataGridTextColumn Header="Author" Binding="{Binding Author}"/></DataGrid.Columns></DataGrid><Button Grid.Row="2"FontSize="50"Content="Load Data"Command="{Binding LoadDataCommand}"/></Grid>
</UserControl>//ViewB.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApp37.Views
{/// <summary>/// Interaction logic for ViewB.xaml/// </summary>public partial class ViewB : UserControl{public ViewB(){InitializeComponent();}}
}//ViewBViewModel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using WpfApp37.Models;namespace WpfApp37.ViewModels
{public class ViewBViewModel:ViewModelBase{private ObservableCollection<Book> items;public ObservableCollection<Book> Items{get{return items;}set{SetProperty(ref items, value);}}public DelegateCommand LoadDataCommand { get; set; }public ViewBViewModel(){Title="View B";Items=new ObservableCollection<Book>();LoadDataCommand=new DelegateCommand(LoadDataCommandExecuted);}private void LoadDataCommandExecuted(){Items.Clear();for(int i = 1; i<1001; i++){Items.Add(new Book(){Id=i,Name=$"Name_{i}",ISBN=$"ISBN_{i}_{Guid.NewGuid()}",Author=$"Author_{i}_{Guid.NewGuid()}"});}}}
}//mainView.xaml
<Window x:Class="WpfApp37.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp37"mc:Ignorable="d"WindowState="Maximized"Title="{Binding MainTitle}" Height="450" Width="800"xmlns:prism="http://prismlibrary.com/"prism:ViewModelLocator.AutoWireViewModel="True"><Grid><Grid.RowDefinitions><RowDefinition Height="100"/><RowDefinition Height="100"/><RowDefinition Height="*"/></Grid.RowDefinitions><!--Header--><StackPanel Grid.Row="0"Orientation="Horizontal"Background="LightBlue"><TextBlock Text="{Binding Title}" FontSize="50" FontWeight="Bold"/><TextBlock Text="Prism DryIOC Application" Margin="10,0,0,0"/></StackPanel><!--Navigation Menu--><StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10"><Button Content="View A" Command="{Binding NavigateCommand}"CommandParameter="ViewA"Margin="5" Padding="10"/><Button Content="View B" Command="{Binding NavigateCommand}"CommandParameter="ViewB"Margin="5" Padding="10"/><Button Content="Show Dialog" Command="{Binding ShowDialogCommand}"Margin="5" Padding="10"/></StackPanel><!--Content Region--><!--<Border Grid.Row="2" Margin="10" BorderBrush="DarkGray" BorderThickness="1"Background="LightGray">--><!-- Remove this background later --><!--<ContentControlprism:RegionManager.RegionName="MainRegion"VerticalAlignment="Stretch"HorizontalAlignment="Stretch"/></Border>--><ContentControl Grid.Row="2"prism:RegionManager.RegionName="MainRegion"VerticalAlignment="Stretch"HorizontalAlignment="Stretch"/></Grid>
</Window>//MainViewModel
<Window x:Class="WpfApp37.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp37"mc:Ignorable="d"WindowState="Maximized"Title="{Binding MainTitle}" Height="450" Width="800"xmlns:prism="http://prismlibrary.com/"prism:ViewModelLocator.AutoWireViewModel="True"><Grid><Grid.RowDefinitions><RowDefinition Height="100"/><RowDefinition Height="100"/><RowDefinition Height="*"/></Grid.RowDefinitions><!--Header--><StackPanel Grid.Row="0"Orientation="Horizontal"Background="LightBlue"><TextBlock Text="{Binding Title}" FontSize="50" FontWeight="Bold"/><TextBlock Text="Prism DryIOC Application" Margin="10,0,0,0"/></StackPanel><!--Navigation Menu--><StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10"><Button Content="View A" Command="{Binding NavigateCommand}"CommandParameter="ViewA"Margin="5" Padding="10"/><Button Content="View B" Command="{Binding NavigateCommand}"CommandParameter="ViewB"Margin="5" Padding="10"/><Button Content="Show Dialog" Command="{Binding ShowDialogCommand}"Margin="5" Padding="10"/></StackPanel><!--Content Region--><!--<Border Grid.Row="2" Margin="10" BorderBrush="DarkGray" BorderThickness="1"Background="LightGray">--><!-- Remove this background later --><!--<ContentControlprism:RegionManager.RegionName="MainRegion"VerticalAlignment="Stretch"HorizontalAlignment="Stretch"/></Border>--><ContentControl Grid.Row="2"prism:RegionManager.RegionName="MainRegion"VerticalAlignment="Stretch"HorizontalAlignment="Stretch"/></Grid>
</Window>//ViewModelBase.cs
using System;
using System.Collections.Generic;
using System.Text;
using Prism.Mvvm;
using Prism.Commands;
using System.ComponentModel;
using System.Runtime.CompilerServices;namespace WpfApp37.ViewModels
{public class ViewModelBase :BindableBase{private string title = "Prism Application";public string Title{get{return title;}set{SetProperty(ref title, value);}}private bool isBusy;public bool IsBusy{get{return isBusy;}set{SetProperty(ref isBusy, value);}}}
}//book.cs
using System;
using System.Collections.Generic;
using System.Text;namespace WpfApp37.Models
{public class Book{public int Id { get;set; }public string Name { get;set; }public string ISBN { get;set; }public string Author { get; set; }}
}

 

 

 

 

image

 

 

 

 

 

 

image

 

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

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

相关文章

备份一个简易队列写法

备份一个简易队列写法 定义 typedef struct {uint8_t data[14]; } can_frame_t;typedef struct {can_frame_t frames[CAN_FRAME_QUEUE_SIZE];volatile uint8_t head;volatile uint8_t tail;volatile uint8_t count; } …

松岗做网站价格新传奇网页游戏

在将单元测试postmortem编码到另一个项目创建的代码时,我遇到了如何使用initBinder模拟与控制器绑定的验证器的这个问题&#xff1f;通常我会考虑确保我的输入是有效的,并且在验证器中进行一些额外的调用,但在这种情况下,验证器类与通过几个数据源进行检查相结合,并且测试变得相…

网站seo分析案例河南网站建设推荐

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 Python基础知识总览1. Python简介2. 安装与环境配置3. 基本语法3.1 变量与数据类型3.2 控制结构3.3 函数与模块3.4 文件操作 4. 面向对象编程&#xff08;OOP&#…

做数学题的网站有吗自助建站网站哪个好

ART公司成立于1999年&#xff0c;拥有38万员工遍布全球&#xff0c;ART一直致力于红外线光学跟踪系统的研发与生产&#xff0c;并将先进的科技应用于产品研发&#xff0c;产品制造&#xff0c;市场营销&#xff0c;产品销售以及优秀的客户支持。主要向客户提供高端的虚拟现实跟…

建站平台控制自适应网页模板企业

概述 外连接的进阶用法在行列转换中比较有优势&#xff0c;往往存在需要把数据库中的格式转换成报表格式&#xff0c;但是SQL仅仅只是查询数据的语言&#xff0c;格式转换并不是原本的用途。 全外连接 标准 SQL 里定义了外连接的三种类型&#xff0c;如下所示。 左外连接&…

公司网站开发有哪些今天西安新消息

随着科技的不断发展&#xff0c;虚拟现实&#xff08;VR&#xff09;技术已经逐渐渗透到各个领域&#xff0c;为人们的生活带来了极大的便利。在煤矿行业&#xff0c;VR技术的应用也日益受到重视&#xff0c;尤其是在煤矿安全检查方面。为了提高矿工的安全意识和技能&#xff0…

福州网站改版哪家好wordpress做英文站

&#x1f9d1; 作者简介&#xff1a;阿里巴巴嵌入式技术专家&#xff0c;深耕嵌入式人工智能领域&#xff0c;具备多年的嵌入式硬件产品研发管理经验。 &#x1f4d2; 博客介绍&#xff1a;分享嵌入式开发领域的相关知识、经验、思考和感悟,欢迎关注。提供嵌入式方向的学习指导…

全球做空现货黄金的网站网页制作专业个人职业生涯规划书

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注公众号【雄雄的小课堂】。最近&#xff0c;我的个人站上线啦&#xff0c;大家可以直接在浏览器的地址栏中输入&#xff1a;穆雄雄.com&#xff0c;轻轻敲击回车&#xff0c;即可直接进入……欢迎大家多多关注&#xff0c;多多留…

js网站模板最新网页游戏传奇类

介绍依赖注入只负责由其创建的对象实例容器或者子容器释放的时候&#xff0c;会释放由其创建的对象实例。推荐使用容器来来管理我们的对象的创建和释放。操作为了演示&#xff0c;我们创建一个UserService&#xff0c;并让该Service继承IDisposablepublic class UserService : …

安徽做网站电话重庆互联网公司多吗

1.背景近期由于业务调整&#xff0c;需要将Windows Server 2008 MySQL5.5数据库迁移到Windows Server 2012 MySQL8.0集群MGR中&#xff0c;由于实际部署时&#xff0c;有一台机器硬盘损坏&#xff0c;只能构建双节点MGR&#xff0c;在迁移以及应用迁移过程中遇到许多参数与迁移…

【SPIE出版】第四届环境遥感与地理信息技术国际学术会议(ERSGIT 2025)

第四届环境遥感与地理信息技术国际学术会议(ERSGIT 2025)定于2025年10月17-19日在中国南京隆重举行。【高录用丨往届快至会后3个月EI检索丨往届均已EI检索】 第四届环境遥感与地理信息技术国际学术会议(ERSGIT 2025…

工厂打星问题

using namespace std ; const int NumberOfPlants = 4; void inputData(int a[],int lastPlantNUmber); void scale(int a[],int size); void graph(const int asteriskCount[],int lastPlantNumber); void getTotal(i…

公司网站开发详细流程网站上的二维码

目录 一、环境信息 二、简述 三、升级点 四、支持功能 五、安装包下载地址 六、配置参数介绍 七、安装步骤 1、配置环境变量 2、生效环境变量 3、检验动态链接是否正常 4、修改配置文件MigrationConfig.txt 八、运行效果 一、环境信息 名称值CPUIntel(R) Core(TM) i…

网页设计图兰州网站关键字优化

Vim是一款功能强大的文本编辑器&#xff0c;广泛用于程序员和开发人员中。虽然Vim主要用于文本编辑&#xff0c;但它也提供了一些方便的功能来编译和运行代码。本文将详细介绍如何在Vim中编译和运行代码&#xff0c;包括设置编译快捷键、使用插件以及集成构建系统。 1. 引言 …

四川红叶建设有限公司网站大连省建设厅网站

课程名称&#xff1a; E054-web安全应用-Brute force暴力破解进阶 课程分类&#xff1a; web安全应用 实验等级: 中级 任务场景: 【任务场景】 小王接到磐石公司的邀请&#xff0c;对该公司旗下的网站进行安全检测&#xff0c;经过一番检查发现该网站可能存在弱口令漏洞…

找素材的网站鸭梨网站建设

现象&#xff1a; 通过抓包看到在部分客户端上跨域的非简单请求只发送一个预检的OPTIONS请求&#xff0c;之后的真实请求并没有发送。 出现问题的环境&#xff1a; 部分IOS低版本系统。 windows系统微信内必现&#xff08;2020-04-29&#xff09;。 分析 通过上面条件OPT…

东莞主页网站制作黄页号码怎么取消标记

举例&#xff1a; 结果&#xff1a; 文字描述&#xff1a; 先将浮点数转化为二进制的表示形式&#xff0c; 接着将其二进制的形式按照科学计数法来表示&#xff0c; 符号位的确定&#xff1a;正数0&#xff0c; 负数1 指数的确定&#xff1a;将其二进制表示成为科学计数法…

沈阳平台网站建设p2p网站建设 上海

1、问题 目前只有一个google手机之前安装了app,里面有room写的数据库&#xff0c;后面把app卸载了&#xff0c;再次安装新的app(修改了数据库里面的字段)&#xff0c;启动奔溃。 2、分析 提示数据库错误&#xff0c;很明显就像以前的app里面的数据库没有删除一样&#xff0c;…

网上开店的基本流程有哪些河池网站seo

目录 前言 驱动入门知识 1.APP 打开的文件在内核中如何表示 2.打开字符设备节点时&#xff0c;内核中也有对应的 struct file 编写 Hello 驱动程序步骤 1.流程介绍 2.驱动代码&#xff1a; 3.应用层代码&#xff1a; 4.本驱动程序的 Makefile 内容&#xff1a; 5.上机…

主机开通成功网站建设中怎么把网站上传到域名

前言 数据结构_空间复杂度_时间复杂度讲解_常见复杂度对比 本文介绍数据结构中的时间复杂度和空间复杂度 ***文章末尾&#xff0c;博主进行了概要总结&#xff0c;可以直接看总结部分*** 博主博客链接&#xff1a;https://blog.csdn.net/m0_74014525 点点关注&#xff0c;后期…