参考B站刘铁猛老师的订餐软件https://www.bilibili.com/video/av29782724?from=search&seid=6787438911056306128

环境:VS2019+Prism框架,参考本人博客安装即可 https://www.cnblogs.com/xixixing/p/11312474.html

程序下载地址 https://files.cnblogs.com/files/xixixing/Demo0813.zip

1、界面分析

2、创建项目Demo0813

 

具体代码:UI界面MainWindow.xaml

<Window x:Class="Demo0813.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Restaurant.Name, StringFormat=\{0\}-在线订餐}" Height="600" Width="1000"
WindowStartupLocation="CenterScreen">
<Border BorderBrush="Orange" BorderThickness="3" CornerRadius="6" Background="Yellow">
<Grid x:Name="Root" Margin="4">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border BorderBrush="Orange" BorderThickness="1" CornerRadius="6" Padding="4">
<StackPanel>
<StackPanel Orientation="Horizontal">
<StackPanel.Effect>
<DropShadowEffect Color="LightGray" />
</StackPanel.Effect>
<TextBlock Text="欢迎光临-" FontSize="60" FontFamily="LiShu" />
<TextBlock Text="{Binding Restaurant.Name}" FontSize="60" FontFamily="LiShu" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="小店地址:" FontSize="24" FontFamily="LiShu" />
<TextBlock Text="{Binding Restaurant.Address}" FontSize="24" FontFamily="LiShu" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="订餐电话:" FontSize="24" FontFamily="LiShu" />
<TextBlock Text="{Binding Restaurant.PhoneNumber}" FontSize="24" FontFamily="LiShu" />
</StackPanel>
</StackPanel>
</Border>
<DataGrid AutoGenerateColumns="False" GridLinesVisibility="None" CanUserDeleteRows="False"
CanUserAddRows="False" Margin="0,4" Grid.Row="1" FontSize="16" ItemsSource="{Binding DishMenu}">
<DataGrid.Columns>
<DataGridTextColumn Header="菜品" Binding="{Binding Dish.Name}" Width="120" />
<DataGridTextColumn Header="种类" Binding="{Binding Dish.Category}" Width="120" />
<DataGridTextColumn Header="点评" Binding="{Binding Dish.Comment}" Width="120" />
<DataGridTextColumn Header="推荐分数" Binding="{Binding Dish.Score}" Width="120" />
<DataGridTemplateColumn Header="选中" SortMemberPath="IsSelected" Width="120">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center" HorizontalAlignment="Center"
Command="{Binding Path=DataContext.SelectMenuItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="2">
<TextBlock Text="共计" VerticalAlignment="Center" />
<TextBox IsReadOnly="True" TextAlignment="Center" Width="120" Text="{Binding Count}" Margin="4,0" />
<Button Content="Order" Height="24" Width="120" Command="{Binding PlaceOrderCommand}" />
</StackPanel>
</Grid>
</Border>
</Window>

MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Demo0813.Models;
using Demo0813.Services;
using Prism.Commands;
using Prism.Mvvm; namespace Demo0813.ViewModels
{
public class MainWindowViewModel : BindableBase
{
#region 数据属性
private Restaurant restaurant; //私有变量、对应的数据属性1
public Restaurant Restaurant
{
get { return restaurant; }
set
{
restaurant = value;
this.RaisePropertyChanged("Restaurent");
}
}
private List<DishMenuItemViewModel> dishMenu; //私有变量、对应的数据属性2
public List<DishMenuItemViewModel> DishMenu //DishMenuItemViewModel类组成的集合
{
get { return dishMenu; }
set
{
dishMenu = value;
this.RaisePropertyChanged("DishMenu");
}
}
private int count; //私有变量、对应的数据属性3
public int Count
{
get { return count; }
set
{
count = value;
this.RaisePropertyChanged("Count");
}
}
#endregion
#region 命令属性
public DelegateCommand SelectMenuItemCommand { get; set; } //命令属性1
public DelegateCommand PlaceOrderCommand { get; set; } //命令属性2
#endregion public MainWindowViewModel()
{
this.LoadRestaurant();
this.LoadDishMenu();
this.SelectMenuItemCommand = new DelegateCommand(new Action(this.SelectMenuItemExecute)); //命令属性与方法联系起来
this.PlaceOrderCommand = new DelegateCommand(new Action(this.PlaceOrderCommandExecute));
}
private void LoadRestaurant() //数据属性1的方法
{
this.Restaurant = new Restaurant();
this.Restaurant.Name = "Crazy大象";
this.Restaurant.Address = "北京市海淀区万泉河路紫金庄园1号楼 1层 113室(亲们:这地儿特难找!)";
this.Restaurant.PhoneNumber = "15210365423 or 82650336";
}
private void LoadDishMenu() //数据属性2的方法,加载所有菜品到DishMenu中
{
IDataService ds = new XmlDataService();
var dishes = ds.GetAllDishes(); //得到所有菜品
this.DishMenu = new List<DishMenuItemViewModel>();
foreach (var dish in dishes)
{
DishMenuItemViewModel item = new DishMenuItemViewModel();
item.Dish = dish;
this.DishMenu.Add(item);
}
}
private void SelectMenuItemExecute() //命令属性1的方法,被选中菜品的数量
{
this.Count = this.DishMenu.Count(i => i.IsSelected == true); //集合中元素的数量,Count是集合自带的方法
}
private void PlaceOrderCommandExecute() //命令属性2的方法,保存被选择的菜品
{
var selectedDishes = this.DishMenu.Where(i => i.IsSelected == true).Select(i => i.Dish.Name).ToList();
IOrderService orderService = new SaveService();
orderService.PlaceOrder(selectedDishes);
MessageBox.Show("订餐成功!");
}
}
}

DishMenuItemViewModel.cs

using Demo0813.Models;
using Prism.Mvvm; namespace Demo0813.ViewModels
{
public class DishMenuItemViewModel : BindableBase
{
public Dish Dish { get; set; } //数据属性 private bool isSelected; //私有变量、对应的数据属性,此属性不展示,只作为选中与否的标记
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
this.RaisePropertyChanged("IsSelected");
}
} public DishMenuItemViewModel()
{ }
}
}

IDataService.cs

using System.Collections.Generic;
using Demo0813.Models; namespace Demo0813.Services
{
interface IDataService //只定义不实现
{
List<Dish> GetAllDishes(); //定义方法
}
}

IOrderService.cs

using System.Collections.Generic;

namespace Demo0813.Services
{
interface IOrderService //只定义不实现
{
void PlaceOrder(List<string> dishes); //定义方法
}
}

XmlDataService.cs

using System;
using System.Collections.Generic;
using Demo0813.Models;
using System.Xml.Linq; namespace Demo0813.Services
{
class XmlDataService : IDataService //实现接口,读取Data.xml中所有菜品
{
public List<Dish> GetAllDishes() //返回所有菜品
{
List<Dish> dishList = new List<Dish>();
string xmlFileName = System.IO.Path.Combine(Environment.CurrentDirectory, @"Data\Data.xml"); //合成路径
XDocument xDoc = XDocument.Load(xmlFileName); //载入内容
var dishes = xDoc.Descendants("Dish"); //Dish节点的所有子节点,descendant子代
foreach (var d in dishes)
{
Dish dish = new Dish();
dish.Name = d.Element("Name").Value;
dish.Category = d.Element("Category").Value;
dish.Comment = d.Element("Comment").Value;
dish.Score = double.Parse(d.Element("Score").Value);
dishList.Add(dish); //加载到表中
} return dishList;
}
}
}

SaveService.cs

using System.Collections.Generic;

namespace Demo0813.Services
{
class SaveService : IOrderService //实现接口
{
public void PlaceOrder(List<string> dishes)
{
System.IO.File.WriteAllLines(@"D:\order.txt", dishes.ToArray()); //D盘保存订餐结果
}
}
}

Dish.cs

using Prism.Mvvm;

namespace Demo0813.Models
{
public class Dish : BindableBase
{
public string Name { get; set; }
public string Category { get; set; }
public string Comment { get; set; }
public double Score { get; set; }
public Dish()
{ }
}
}

Restaurant.cs

using Prism.Mvvm;

namespace Demo0813.Models
{
public class Restaurant : BindableBase //类,以及其中的属性
{
public string Name { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public Restaurant()
{ }
}
}

Data.xml

<?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>5</Score>
</Dish>
<Dish>
<Name>水果披萨</Name>
<Category>披萨</Category>
<Comment></Comment>
<Score>4</Score>
</Dish>
<Dish>
<Name>牛肉披萨</Name>
<Category>披萨</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>培根披萨</Name>
<Category>披萨</Category>
<Comment></Comment>
<Score>4.5</Score>
</Dish>
<Dish>
<Name>什锦披萨</Name>
<Category>披萨</Category>
<Comment></Comment>
<Score>4.5</Score>
</Dish>
<Dish>
<Name>金枪鱼披萨</Name>
<Category>披萨</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>海鲜披萨</Name>
<Category>披萨</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>川香披萨</Name>
<Category>披萨</Category>
<Comment></Comment>
<Score>4.5</Score>
</Dish>
<Dish>
<Name>黑椒鸡腿扒</Name>
<Category>特色主食</Category>
<Comment>本店特色</Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>肉酱意面</Name>
<Category>特色主食</Category>
<Comment>本店特色</Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>寂寞小章鱼</Name>
<Category>风味小吃</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>照烧鸡软骨</Name>
<Category>风味小吃</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>芝士青贝</Name>
<Category>风味小吃</Category>
<Comment></Comment>
<Score>4.5</Score>
</Dish>
<Dish>
<Name>奥尔良烤翅</Name>
<Category>风味小吃</Category>
<Comment>秒杀KFC</Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>双酱煎泥肠</Name>
<Category>风味小吃</Category>
<Comment></Comment>
<Score>4</Score>
</Dish>
<Dish>
<Name>香酥鱿鱼圈</Name>
<Category>风味小吃</Category>
<Comment>本店特色</Comment>
<Score>4.5</Score>
</Dish>
<Dish>
<Name>黄金蝴蝶虾</Name>
<Category>风味小吃</Category>
<Comment>本店特色</Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>金枪鱼沙拉</Name>
<Category>沙拉</Category>
<Comment>本店特色</Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>日式素沙拉</Name>
<Category>沙拉</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>冰糖洛神</Name>
<Category>饮料</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>玫瑰特饮</Name>
<Category>饮料</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>清新芦荟</Name>
<Category>饮料</Category>
<Comment></Comment>
<Score>5</Score>
</Dish>
<Dish>
<Name>薄荷汽水</Name>
<Category>饮料</Category>
<Comment>本店特色</Comment>
<Score>5</Score>
</Dish>
</Dishes>

Prism框架实战——订餐软件的更多相关文章

  1. Prism框架研究(一)

    从今天起开始写一个Prism框架的学习博客,今天是第一篇,所以从最基本的一些概念开始学习这个基于MVVM的框架的学习,首先看一下Prism代表什么,这里引用一下比较官方的英文解释来看一下:Prism ...

  2. 项目中使用Prism框架

    Prism框架在项目中使用   回顾 上一篇,我们介绍了关于控件模板的用法,本节我们将继续说明WPF更加实用的内容,在大型的项目中如何使用Prism框架,并给予Prism框架来构建基础的应用框架,并且 ...

  3. Prism框架的Regions使用

    Prism框架的Regions,可以把用户控件.窗体等附加到主窗体指定的控件中. [实战1] 1.新建Prism Blank App(WPF) 项目:Demo0810 Views文件夹处,鼠标右键—— ...

  4. Apworks框架实战

    Apworks框架实战(一):Apworks到底是什么? Apworks框架实战(二):开始使用 Apworks框架实战(三):单元测试与持续集成 Apworks框架实战(四):使用Visual St ...

  5. 应用程序框架实战二十二 : DDD分层架构之仓储(层超类型基础篇)

    前一篇介绍了仓储的基本概念,并谈了我对仓储的一些认识,本文将实现仓储的基本功能. 仓储代表聚合在内存中的集合,所以仓储的接口需要模拟得像一个集合.仓储中有很多操作都是可以通用的,可以把这部分操作抽取到 ...

  6. 应用程序框架实战十五:DDD分层架构之领域实体(验证篇)

    在应用程序框架实战十四:DDD分层架构之领域实体(基础篇)一文中,我介绍了领域实体的基础,包括标识.相等性比较.输出实体状态等.本文将介绍领域实体的一个核心内容——验证,它是应用程序健壮性的基石.为了 ...

  7. WPF Step By Step 系列-Prism框架在项目中使用

    WPF Step By Step 系列-Prism框架在项目中使用 回顾 上一篇,我们介绍了关于控件模板的用法,本节我们将继续说明WPF更加实用的内容,在大型的项目中如何使用Prism框架,并给予Pr ...

  8. 《开源分享2》:《开源框架实战宝典电子书V1.0.0》完整版!

    经过一个多月的整理,<J2EE开源框架实战宝典>--Tiny文档PDF电子书開始发放,共同拥有将近600页.为喜爱Tiny.热爱Java开源框架的朋友提供更加体贴的文档服务! 下载地址:h ...

  9. WPF Prism框架下基于MVVM模式的命令、绑定、事件

    Prism框架下的自定义路由事件和命令绑定 BaseCode XAML代码: <Button x:Class="IM.UI.CommandEx.PrismCommandEx" ...

随机推荐

  1. Python验证数据的抽样分布类型

    假如要对一份统计数据进行分析,一般其来源来自于社会调研/普查,所以数据不是总体而是一定程度的抽样.对于抽样数据的分析,就可以结合上篇统计量及其抽样分布的内容,判断数据符合哪种分布.使用已知分布特性,可 ...

  2. 使用logstash从Kafka中拉取数据并传输给elasticsearch且创建相应索引的操作

    注意事项:默认Kafka传递给elastci的数据是在'data'字段,且不包含其他数据,所以需要使用额外的操作进行处理 logstash配置文件操作 input { kafka { bootstra ...

  3. 后端查询树的通用SQL,具备懒加载功能

    select t.org_id as key, --key值 t.org_name as title, --标题 t.has_sub as folder, --是否显示文件夹 t.has_sub as ...

  4. 发明专利定稿&递交申请啦,开心

    也不想写些什么,只是想简单的分享一下当前的心情! 第一版到最后一版中间因为各种事情耽误,一直弄到现在.5月中旬找的专利代理局中间连续修改很多次,从大改到小改,再到微调真的是学习到了! 下面就是搞定&l ...

  5. 你真的知道em和rem的区别吗?

    前言 em 和 rem 都是相对单位,在使用时由浏览器转换为像素值,具体取决于您的设计中的字体大小设置. 如果你使用值 1em 或 1rem,它可以被浏览器解析成 从16px 到 160px 或其他任 ...

  6. django 中间键重定向

    1,定义和注册中间件 在注册的中间件中使用: from django.http import HttpResponseRedirect '''下面的书写方法会陷入死循环,所以必须加判断条件只调用一次' ...

  7. day1-css练习[新浪首页顶部栏]

    直接贴代码吧: html代码 <div class="border-01"> <div class="border-001"> < ...

  8. centos7重启网卡报Job for network.service failed because...错误

    解决: [root@mina0 hadoop]# systemctl stop NetworkManager[root@mina0 hadoop]# systemctl disable Network ...

  9. 配置Notepad++万能调试

    需求: 正常情况下 使用notepad++编辑修改一些网页或脚本文件,修改之后想要查看效果需要Ctrl+S保存,然后从文件目录中打开查看. 现在我想做的就是简化查看效果的流程,让notepad++实现 ...

  10. Bridge的数据在内核处理流程

    转:http://blog.sina.com.cn/s/blog_67cc0c8f0101oh33.html 转载一篇Bridge的数据在内核处理流程,文章写的不错啊! (2013-07-05 16: ...