Prism框架实战——订餐软件
阅读原文时间:2023年07月13日阅读:2

参考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

¨C15C ¨C26C ¨C21C ¨C22C ¨C23C

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 dishMenu; //私有变量、对应的数据属性2
public List 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 GetAllDishes(); //定义方法
}
}

IOrderService.cs

using System.Collections.Generic;

namespace Demo0813.Services
{
interface IOrderService //只定义不实现
{
void PlaceOrder(List 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 GetAllDishes() //返回所有菜品
{
List dishList = new List();
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 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


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

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章