《深入浅出WPF》学习笔记七.使用Prism实现点单系统

news/2024/9/17 19:03:18/ 标签: wpf, 学习, 笔记, c#, microsoft

《深入浅出WPF》学习笔记七.使用Prism实现Mvvm点单系统

背景

深入浅出Wpf系列视频的最后一个demo,使用Prism、Mvvm实现点单系统。demo并不复杂,但是涉及的面广,方便更好的理解wpf。代码在下面自取。后续会把git地址补充上来。

46228076cff84b338fdf745bb6d12f14.png

代码

项目层级

6605f83c2e42434b991baf71f75c0c4d.png

command

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace RestaurantOrderSystem.Command
{public class RelayCommand : ICommand{private readonly Action<object> _execute;public RelayCommand(Action<object> execute){_execute = execute;}public event EventHandler CanExecuteChanged{add { CommandManager.RequerySuggested += value; }remove { CommandManager.RequerySuggested -= value; }}public bool CanExecute(object parameter) => true;public void Execute(object parameter){_execute(parameter);}}
}

Data

<?xml version="1.0" encoding="utf-8" ?>
<Dishes><Dish><Name>奥尔良烤肉披萨</Name><Category>披萨</Category><Comment>本店推荐</Comment><Score>4.5</Score></Dish><Dish><Name>夏威夷风情披萨</Name><Category>披萨</Category><Comment>本店推荐</Comment><Score>4.6</Score></Dish><Dish><Name>榴莲卷边披萨</Name><Category>披萨</Category><Comment>本店推荐</Comment><Score>4.7</Score></Dish><Dish><Name>韩式烤肉披萨</Name><Category>披萨</Category><Comment>特色</Comment><Score>4.5</Score></Dish><Dish><Name>墨尔本牛排</Name><Category>牛排</Category><Comment>本店推荐</Comment><Score>4.6</Score></Dish><Dish><Name>德克萨斯牛排</Name><Category>牛排</Category><Comment>本店推荐</Comment><Score>4.6</Score></Dish><Dish><Name>纽约香煎牛排</Name><Category>牛排</Category><Comment>本店推荐</Comment><Score>4.6</Score></Dish><Dish><Name>可乐</Name><Category>饮料</Category><Comment>本店推荐</Comment><Score>4.6</Score></Dish><Dish><Name>雪碧</Name><Category>饮料</Category><Comment>本店推荐</Comment><Score>4.6</Score></Dish>
</Dishes>

Models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace RestaurantOrderSystem.Models
{public class Dish{public string Name { get; set; }public string Category { get; set; }public string Comment { get; set; }public double Score { get; set; }public override string ToString(){return string.Format("{0},{1},{2},{3}", Name, Category, Comment, Score);}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace RestaurantOrderSystem.Models
{public class Restaurant{public string Name { get; set; }public string Address { get; set; }public string Phone { get; set; }}
}

Services

using RestaurantOrderSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace RestaurantOrderSystem.Services
{internal interface IDataService{public List<Dish> GetDishes();}
}
using RestaurantOrderSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace RestaurantOrderSystem.Services
{public interface IOrderService{public void PlaceOrder(List<Dish> dishes);}
}
using RestaurantOrderSystem.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace RestaurantOrderSystem.Services
{public class MockOrderService : IOrderService{public void PlaceOrder(List<Dish> dishes){string xmlFile = System.IO.Path.Combine(Environment.CurrentDirectory, @"Data\Order.txt");if (!File.Exists(xmlFile)) { File.Create(xmlFile); }System.IO.File.WriteAllLines(xmlFile, dishes.AsEnumerable().Select(o => o.Name.ToString()).ToArray());}}
}
using RestaurantOrderSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;namespace RestaurantOrderSystem.Services
{internal class XmlDataService : IDataService{public List<Dish> GetDishes(){List<Dish> disheList = new List<Dish>();string xmlFile = System.IO.Path.Combine(Environment.CurrentDirectory, @"Data\Data.xml");XDocument xmlDoc = XDocument.Load(xmlFile);var dishes = xmlDoc.Descendants("Dish");Dish dish = null;foreach (var item in dishes){dish = new Dish();dish.Name = item.Element("Name")?.Value;dish.Category = item.Element("Category")?.Value;dish.Comment = item.Element("Comment")?.Value;dish.Score = Convert.ToDouble(item.Element("Score")?.Value);disheList.Add(dish);}return disheList;}}
}

ViewModels

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Mvvm;
using RestaurantOrderSystem.Models;namespace RestaurantOrderSystem.ViewModels
{public class DishMenuItemViewModel :BindableBase{public Dish Dish {  get; set; }private bool isSelected;public bool IsSelected{get { return isSelected; }set { SetProperty(ref isSelected, value); }}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Prism.Mvvm;
using Prism.Regions;
using RestaurantOrderSystem.Command;
using RestaurantOrderSystem.Models;
using RestaurantOrderSystem.Services;namespace RestaurantOrderSystem.ViewModels
{public class OrderViewModel : BindableBase{private int count;public int Count{get { return count; }set{SetProperty(ref count, value);}}private List<DishMenuItemViewModel> menuItem;public List<DishMenuItemViewModel> MenuItem{get { return menuItem; }set{SetProperty(ref menuItem, value);}}private Restaurant myRestaurant;public Restaurant MyRestaurant{get { return myRestaurant; }set{SetProperty(ref myRestaurant, value);}}public OrderViewModel(){orderService = new MockOrderService();xmlDataService = new XmlDataService();LoadRestaurant();LoadMenuData();Count = 0;}private void LoadRestaurant(){Restaurant restaurant = new Restaurant();restaurant.Name = "JokerRestaurant";restaurant.Address = "上海世纪汇";restaurant.Phone = "024-25978888";this.MyRestaurant = restaurant;}private MockOrderService orderService;private XmlDataService xmlDataService;private void LoadMenuData(){List<Dish> dishes = xmlDataService.GetDishes();List<DishMenuItemViewModel> dishMenuList = new List<DishMenuItemViewModel>();foreach (Dish dish in dishes){DishMenuItemViewModel dishMenu = new DishMenuItemViewModel();dishMenu.Dish = dish;dishMenu.IsSelected = false;dishMenuList.Add(dishMenu);}this.MenuItem = dishMenuList;}private RelayCommand placeOrderCommand;public RelayCommand PlaceOrderCommand{get{if (placeOrderCommand == null)placeOrderCommand = new RelayCommand(PlaceOrderCommandExecute);return placeOrderCommand;}set { placeOrderCommand = value; }}private void PlaceOrderCommandExecute(object param){var orderMenu = this.MenuItem.Where(o => o.IsSelected == true).Select(o => o.Dish).ToList();orderService.PlaceOrder(orderMenu);MessageBox.Show("下单成功!");}private RelayCommand selectMenuItemCommand;public RelayCommand SelectMenuItemCommand{get{if (selectMenuItemCommand == null)selectMenuItemCommand = new RelayCommand(SelectMenuItemCommandExecute);return selectMenuItemCommand;}set { selectMenuItemCommand = value; }}private void SelectMenuItemCommandExecute(object param){this.Count = this.MenuItem.Where(o => o.IsSelected == true).Count();}}
}

Views

<Window x:Class="RestaurantOrderSystem.Views.OrderView"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:RestaurantOrderSystem.Views"mc:Ignorable="d" WindowStartupLocation="CenterScreen"Title="{Binding MyRestaurant.Name,StringFormat=\{0\}-在线订餐}" Height="600" Width="1000"><Border BorderBrush="Orange" BorderThickness="3" Background="Yellow" CornerRadius="6"><Grid><Grid.RowDefinitions><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="*"></RowDefinition><RowDefinition Height="Auto"></RowDefinition></Grid.RowDefinitions><Border BorderBrush="Orange" BorderThickness="1" Background="Yellow" CornerRadius="6" Padding="4" ><StackPanel Grid.Row="0"><StackPanel Orientation="Horizontal"><StackPanel.Effect><DropShadowEffect Color="LightGray"></DropShadowEffect></StackPanel.Effect><TextBlock Text="欢迎光临-" FontSize="60" FontFamily="LiShu"></TextBlock><TextBlock Text="{Binding MyRestaurant.Name}" FontSize="60" FontFamily="LiShu"></TextBlock></StackPanel><StackPanel Orientation="Horizontal"><TextBlock Text="店铺地址-" FontSize="24" FontFamily="LiShu"></TextBlock><TextBlock Text="{Binding MyRestaurant.Address}" FontSize="24" FontFamily="LiShu"></TextBlock></StackPanel><StackPanel Orientation="Horizontal"><TextBlock Text="订餐电话-" FontSize="24" FontFamily="LiShu"></TextBlock><TextBlock Text="{Binding MyRestaurant.Phone}" FontSize="24" FontFamily="LiShu"></TextBlock></StackPanel></StackPanel></Border><DataGrid AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding MenuItem}"Grid.Row="1" Margin="0,4" GridLinesVisibility="None" FontSize="16"><DataGrid.Columns><DataGridTextColumn Header="菜品" Width="120" Binding="{Binding Dish.Name}"></DataGridTextColumn><DataGridTextColumn Header="种类" Width="120" Binding="{Binding Dish.Category}"></DataGridTextColumn><DataGridTextColumn Header="点评" Width="120" Binding="{Binding Dish.Comment}"></DataGridTextColumn><DataGridTextColumn Header="推荐分数" Width="120" Binding="{Binding Dish.Score}"></DataGridTextColumn><DataGridTemplateColumn Header="选中" SortMemberPath="IsSelected" Width="120"><DataGridTemplateColumn.CellTemplate><DataTemplate><CheckBox Width="120" IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged}"VerticalAlignment="Center" HorizontalAlignment="Center"Command="{Binding Path=DataContext.SelectMenuItemCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"></CheckBox></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid><StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="1"><TextBlock Text="总计" HorizontalAlignment="Center"></TextBlock><TextBox IsEnabled="False" Text="{Binding Count}" Margin="4,0" Width="120"></TextBox><Button x:Name="btnOrder" Command="{Binding PlaceOrderCommand}" Width="120" Content="下单"></Button></StackPanel></Grid></Border>
</Window>
using RestaurantOrderSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;namespace RestaurantOrderSystem.Views
{/// <summary>/// OrderView.xaml 的交互逻辑/// </summary>public partial class OrderView : Window{public OrderView(){InitializeComponent();this.DataContext = new OrderViewModel();}}
}

App.cs

<Application x:Class="RestaurantOrderSystem.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:RestaurantOrderSystem"StartupUri="Views/OrderView.xaml"><Application.Resources></Application.Resources>
</Application>

git地址👇

https://github.com/wanghuayu-hub2021/OrderSystem.git

许个愿,这周有个offer。

 

 


http://www.ppmy.cn/news/1507309.html

相关文章

Multisim 用LM358 运放模拟线性稳压器 - 运放输出饱和 - 前馈电容

就是拿运放搭一个可调的LDO 稳压器&#xff0c;类似下面这个功能框图里的感觉。本来应该非常简单&#xff0c;没什么好说的&#xff0c;没想到遇到了两个问题。 原理 - 理想运放 我用PNP 三极管Q2 作为输出&#xff0c;运放输出电压升高时&#xff0c;流过PNP 三极管BE 的电流变…

云服务器部署Java+Vue前后端分离项目

1、申请一个云服务器 选择云服务器&#xff1a;阿里云、腾讯云、百度云、京东云、华为云等等&#xff0c;我使用的是阿里云服务器。 2、远程链接服务器 使用FinalShell工具或者其他远程工具&#xff0c;使用SSH链接&#xff0c;主机地址要填写阿里云服务的公网ip&#xff0c;如…

Redis的String类型常用命令总结

1. set 设置一个键的值。 set key value示例&#xff1a; set username "alice"2. get 获取一个键的值。 get key示例&#xff1a; get username3. getset 设置键的值&#xff0c;并返回键的旧值。 getset key value示例&#xff1a; getset username "…

for(char c:s),std::vector<int> numbers 和std::int numbers[],.size()和.sizeof()区别

在C中当需要对某个容器或数组进行遍历时我们可以使用以下语句&#xff0c;c将会被赋值为s中的元素 for(char c:s)://s可以是任何满足条件的容器或数组for(int c:s):for(double c:s):for(float c:s):在C中我们来区分std::vector numbers {1, 2, 3, 4, 5};和std::int numbers[] …

常见8种数据结构

常见的数据结构包括数组、链表、队列、栈、树、堆、哈希表和图&#xff0c;每种数据结构都有其特点&#xff0c;如下&#xff1a; 常见数据结构 1.数组2.链表3.队列4.栈5.树6.图7.哈希表8.堆 1.数组 特点&#xff1a; 固定大小的线性数据结构支持快速随机访问插入和删除效率…

徐州BGP机房与普通机房的区别有哪些?

BGP也被称为是边界网关协议&#xff0c;是运行在TCP上的一种自治系统的路由协议&#xff0c;能够用来处理因特网大小的网络协议&#xff0c;同时也是能够处理好不相关路由域之间的多路连接的协议&#xff0c;今天小编主要来聊一聊徐州BGP机房与普通机房之间的区别有哪些&#x…

5分钟上手亚马逊云科技AWS核心云开发/云架构知识 - 维护EC2服务器

简介&#xff1a; 小李哥从今天开始将开启全新亚马逊云科技AWS云计算知识学习系列&#xff0c;适用于任何无云计算或者亚马逊云科技技术背景的开发者&#xff0c;让大家0基础5分钟通过这篇文章就能完全学会亚马逊云科技一个经典的服务开发架构。 我将每天介绍一个基于亚马逊云…

【xilinx】Vitis 2021.1 安装在 Ubuntu 20.04 上挂起

描述 受影响的设备和配置&#xff1a; 所有 I/Q/M/E 温度等级的 Versal GTM。当前或未来的 38 Gb/s 及以上的 PAM4 配置。所有线路速率低于 38 Gb/s 或 NRZ 的 PAM4 配置不受影响。 当 GTM 收发器当前未使用但已配置时&#xff0c;在某些操作条件下&#xff08;参见表 1&#x…

C++之类与对象(完结撒花篇)

目录 前言 1.再探构造函数 2.类型转换 3.static成员 4. 友元 5.内部类 6.匿名对象 7.对象拷贝时的编译器优化 结束语 前言 在前面的博客中&#xff0c;我们对类的默认成员函数都有了一定了解&#xff0c;同时实现了一个日期类对所学的没内容进行扩展延伸&#xff0c;本…

【C++ 面试 - 基础题】每日 3 题(十一)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏&…

计算机网络TCP/UDP知识点

这是一些在学习过程中关于计算机网络八股文的一些知识点记录&#xff1a; TCP/UDP TCP怎么保证可靠性 1.序列号&#xff0c;确认应答&#xff0c;超时重传 数据到达接收方&#xff0c;接收方需要发出一个确认应答&#xff0c;表示已经收到该数据段&#xff0c;并且确认序号…

【HarmonyOS NEXT星河版开发学习】小型测试案例06-小红书卡片

个人主页→VON 收录专栏→鸿蒙开发小型案例总结​​​​​ 基础语法部分会发布于github 和 gitee上面&#xff08;暂未发布&#xff09; 前言 在鸿蒙&#xff08;HarmonyOS&#xff09;开发中&#xff0c;自适应伸缩是指应用程序能够根据不同设备的屏幕尺寸、分辨率和形态&…

Leetcode每日刷题之 11. 盛最多水的容器(C++)

1. 题目解析 根据题目我们知道本题我们需要由给出的数组找出所有容器中盛水最多的一个&#xff0c;即核心就是先求出所有容器后遍历找出最大的即可 2. 算法原理 本题使用到的算法是双指针&#xff0c;在使用暴力解法遍历所有容器的时候会出现超时的问题&#xff0c;而是用双指针…

【机器学习数据预处理】特征工程

【作者主页】Francek Chen 【专栏介绍】 ⌈ ⌈ ⌈Python机器学习 ⌋ ⌋ ⌋ 机器学习是一门人工智能的分支学科&#xff0c;通过算法和模型让计算机从数据中学习&#xff0c;进行模型训练和优化&#xff0c;做出预测、分类和决策支持。Python成为机器学习的首选语言&#xff0c;…

mysql本地3306通过nginx映射到外网

mysql本地3306通过nginx映射到外网 安装nginx, 版本大于1.19x yum install nginx 安装stream-module模块 查看yum源&#xff0c;查找有没有 nginx-mod-stream.x86_64 yum list | grep nginx 安装stream, 安装后就可以使用stream这个功能 yum install -y nginx-mod-stream.x8…

LVS详解

一、概念简述 1.1LVS概念简述 1.1.1 LVS LVS&#xff08;Linux Virtual Server&#xff09;即Linux虚拟服务器&#xff0c;是由章文嵩博士主导的开源负载均衡项目&#xff0c;目前LVS已经被集成到Linux内核模块中。LVS基于内核网络层工作&#xff0c;有着超强的并发处理能力&…

av.codec.codec.UnknownCodecError: libx264

遇到 av.codec.codec.UnknownCodecError: libx264 这个错误通常意味着 PyAV 库尝试使用 libx264 编码器来编码或解码视频&#xff0c;但该编码器在你的系统中不可用。 libx264 是一个广泛使用的 H.264 视频编码库。如果你正在使用 PyAV 来处理视频&#xff0c;特别是当你尝试读…

CA证书和openssl介绍

文章目录 一、加密和算法常见的安全攻击加密算法和协议对称加密非对称加密算法 二、CA和证书中间人攻击CA和证书安全协议SSL/TLS协议介绍HTTPS 三、opensslopenssl介绍使用openssl实现对称加密使用openssl命令生成加密密码生成随机密码建立私有CA证书申请颁发建立私有CA实际例子…

C++初阶_1:缺省参数

闲话少叙&#xff0c;咱们这章来说说缺省参数。 何为缺省参数&#xff1f; 先请诸位来看一段代码&#xff1a; //缺省参数 #include <iostream> using namespace std; void Fun(int a 10)//此处为函数的定义&#xff0c;这里形参a给了一个缺省值10 {cout << a …

测试日记day3

1、写出5条mysql常用命令 答&#xff1a; show databases&#xff1a;用于显示当前 MySQL 服务器中的所有数据库。create database [database_name]&#xff1a;用于创建新的数据库。use [database_name]&#xff1a;用户选择要使用的数据库。show tables&#xff1a;在已经选…