WPF+Mvvm项目入门完整教程-仓储管理系统(二)

news2024/9/21 12:46:36

目录

  • 一、搭建一个主界面框架
  • 二、实现步骤
    • 1.主界面区域划分
    • 2.主界面区域实现

一、搭建一个主界面框架

主要实现主界面的框架样式和基础功能。这里特别说明一下,由于MvvmLight 已经过时不在维护,本项目决定将MvvmLight框架变更为 CommunityToolkit.Mvvm 框架,使用方法和 MvvmLight 一样,具体配置大家可以参考像 MvvmLight 一样使用 CommunityToolkit.Mvvm 工具包 这篇文章。本节实现效果如下所示:
在这里插入图片描述

二、实现步骤

1.主界面区域划分

这里我们分析一下,主界面一共分成上中下三个区域,最上面的为系统功能按钮,中间区域显示内容信息,底部显示系统版本版权信息等。分割效果图如下:
在这里插入图片描述

2.主界面区域实现

MainWindow.xaml 文件完整代码如下:

<Window x:Class="CommonProject_DeskTop.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:CommonProject_DeskTop"
        xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:viewmodel="clr-namespace:CommonProject_DeskTop.ViewModel" 
        mc:Ignorable="d" Title="通用基础案例"
        DataContext="{Binding Source={StaticResource Locator},Path=Main}" MinWidth="1200" MinHeight="700"
        ResizeMode="CanResize"  Background="#1F1F1F" AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Assets/Styles/SystemButton.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid x:Name="MainContent" ClipToBounds="True" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <!--顶部系统菜单按钮-->
        <Grid Background="#1F2336" MouseDown="DragMoveWindow">
            <!--绘制多边形形状作为标题背景底色-->
            <Polygon Points="0 0,280,0,220 40 20 40 0 40" StrokeThickness="0" Stroke="White" HorizontalAlignment="Left">
                <Polygon.Fill>
                    <SolidColorBrush Color="Gray"/>
                </Polygon.Fill>
            </Polygon>
            <TextBlock Text="仓储管理系统" TextAlignment="Left"  VerticalAlignment="Center" FontSize="20" Foreground="Gold" Margin="35 0 0 0"/>
            <!--系统按钮-->
            <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Margin="0 0 10 0" VerticalAlignment="Center" >
                <Button Content="&#xe602;" ToolTip="登录" Style="{StaticResource SystemButtonStyle}" Command="{Binding ButtonClickCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=ToolTip}"/>
                <Button Content="&#xe650;" ToolTip="最小化" Style="{StaticResource SystemButtonStyle}" Command="{Binding ButtonClickCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=ToolTip}"/>
                <Button Content="&#xe65b;" ToolTip="最大化" Style="{StaticResource SystemButtonStyle}" Command="{Binding ButtonClickCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=ToolTip}"/>
                <Button Content="&#xe70f;" ToolTip="关闭" Style="{StaticResource SystemButtonStyle}" Command="{Binding ButtonClickCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=ToolTip}"/>
            </StackPanel>
        </Grid>
        <Grid Grid.Row="1">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="130"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Rectangle Fill="#111111"/>
                <ListBox Margin="0 5 0 0" Background="Transparent" Foreground="White" BorderThickness="0" FontSize="16" Name="listMenu">
                    <behavior:Interaction.Triggers>
                        <behavior:EventTrigger EventName="SelectionChanged">
                            <behavior:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.MenuClickCmd}" CommandParameter="{Binding ElementName=listMenu,Path=SelectedItem}"/>
                        </behavior:EventTrigger>
                    </behavior:Interaction.Triggers>
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListBoxItem">
                                        <StackPanel Height="35" Orientation="Horizontal" Background="Transparent" Name="container" Cursor="Hand">
                                            <Rectangle Fill="DarkRed" VerticalAlignment="Stretch" Width="5" Visibility="Hidden" Name="leftSlideRectangle"/>
                                            <ContentPresenter  VerticalAlignment="Center" Margin="15 0 0 0"/>
                                        </StackPanel>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsSelected" Value="True">
                                                <Setter Property="Visibility" Value="Visible" TargetName="leftSlideRectangle"/>
                                                <Setter Property="Background" Value="#3a3a3a" TargetName="container"/>
                                            </Trigger>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="Visibility" Value="Visible" TargetName="leftSlideRectangle"/>
                                                <Setter Property="Opacity" Value="0.5" TargetName="leftSlideRectangle"/>
                                                <Setter Property="Background" Value="#1a1a1a" TargetName="container"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <ListBoxItem  Content="首页"/>
                    <ListBoxItem  Content="物资管理"/>
                    <ListBoxItem  Content="仓库设置"/>
                    <ListBoxItem  Content="库位管理"/>
                    <ListBoxItem  Content="客户管理"/>
                    <ListBoxItem  Content="供应商管理"/>
                    <ListBoxItem  Content="物资入库"/>
                    <ListBoxItem  Content="物资出库"/>
                    <ListBoxItem  Content="入库管理"/>
                    <ListBoxItem  Content="出库管理"/>
                </ListBox>
            </Grid>
        </Grid>
        <!--底部版本信息-->
        <Grid Grid.Row="2" Background="#424242">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <StatusBarItem Grid.Column="0" FontSize="10" Foreground="#F5F5F5" HorizontalAlignment="Center" Content="版权@2024"/>
            <StatusBarItem Grid.Column="1" FontSize="10" Foreground="#F5F5F5" HorizontalAlignment="Center" Content="XXXXXXXXX有限公司"/>
            <StatusBarItem Grid.Column="3"  HorizontalAlignment="Center" >
                <TextBlock Text="{Binding DateTimeStr}" FontSize="10" Foreground="#EBEBEB"/>
            </StatusBarItem>
        </Grid>

    </Grid>
</Window>

2、MainWindow.xaml.cs 后端代码如下:

using CommonProject_DeskTop.ViewModel;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.Navigation;
using System.Windows.Shapes;

namespace CommonProject_DeskTop
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //消息注册
            RegisterMessage();
        }

        private void RegisterMessage()
        {
            注册一个系统菜单功能消息
            WeakReferenceMessenger.Default.Register<string>(this, HandleMessage);
        }

        private void HandleMessage(object recipient, string message)
        {
            switch (message)
            {
                case "MainWindowClose":
                    AppClose();
                    break;
                case "MainWindowMax":
                    MainWindowMax();
                    break;
                case "MainWindowMin":
                    MainWindowMin();
                    break;
            }
        }


        /// <summary>
        /// 窗体最小化
        /// </summary>
        private void MainWindowMin()
        {
            this.WindowState = WindowState.Minimized;
        }
        /// <summary>
        /// 窗体最大化
        /// </summary>
        private void MainWindowMax()
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
            }
            else
            {
                this.WindowState = WindowState.Maximized;
            }
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        private void AppClose()
        {
            //DataCenter.Instance.Dispose();
            string appName = AppDomain.CurrentDomain.SetupInformation.ApplicationName.Replace(".exe", "");
            foreach (var item in Process.GetProcessesByName(appName))
            {
                item.Kill();
            }
        }


        /// <summary>
        /// 窗体拖拽方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DragMoveWindow(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            if (e.LeftButton == MouseButtonState.Pressed && this.WindowState != WindowState.Maximized)
            {
                this.DragMove();
            }
        }
    }
}

3、MainViewModel.cs 文件代码如下:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
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.Threading;

namespace CommonProject_DeskTop.ViewModel
{
    public class MainViewModel : ObservableObject
    {
        #region 属性
        [ObservableProperty]
        private string dateTimeStr;
        public string DateTimeStr
        {
            get { return dateTimeStr; }
            set { SetProperty(ref dateTimeStr, value); }
        }
        #endregion

        #region 命令
        public RelayCommand<string> ButtonClickCmd { get; set; }
        public RelayCommand<object>MenuClickCmd { get; set; }

        #endregion

        #region 方法



        #region 系统按钮方法
        private void FunMenu(string p)
        {
            switch (p)
            {
                case "最小化":
                    WindowMin();
                    break;
                case "最大化":
                    WindowMax();
                    break;
                case "关闭":
                    AppClose();
                    break;
            }
        }
        private void AppClose()
        {
            WeakReferenceMessenger.Default.Send("MainWindowClose");
        }

        private void WindowMax()
        {
            WeakReferenceMessenger.Default.Send("MainWindowMax");
        }

        private void WindowMin()
        {
            WeakReferenceMessenger.Default.Send("MainWindowMin");
        }
        #endregion

        #region 菜单按钮方法
        private void MenuChange(object obj)
        {
            var menu =obj as ListBoxItem;
            switch (menu.Content.ToString())
            {
                case "首页":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "物资管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "仓库设置":
                    MessageBox.Show(menu.Content.ToString(),"");
                    break;
                case "库位管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "客户管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "供应商管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "物资入库":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "物资出库":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "入库管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "出库管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
            }
        }
        #endregion

        private void CreateTimer()
        {
            #region 系统时间定时器获取时间数据
            DispatcherTimer cpuTimer = new DispatcherTimer
            {
                Interval = new TimeSpan(0, 0, 0, 1, 0)
            };
            cpuTimer.Tick += DispatcherTimer_Tick;
            cpuTimer.Start();
            #endregion
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            DateTimeStr = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
        }


        #endregion

        public MainViewModel()
        {
            ButtonClickCmd = new RelayCommand<string>(FunMenu);
            MenuClickCmd = new RelayCommand<object>(MenuChange);
            //创建定时器
            CreateTimer();
        }
    }
}

4、SystemButton.xaml 样式文件代码如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button" x:Key="SystemButtonStyle">
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Height" Value="36"/>
        <Setter Property="Width" Value="50"/>
        <Setter Property="Margin" Value="3 0 3 0"/>
        <Setter Property="Foreground" Value="Gainsboro"/>
        <Setter Property="Tag" Value="#3F3F41"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Name="back">
                        <Border Name="hover" Background="{TemplateBinding Background}" Visibility="Collapsed"/>
                        <TextBlock Text="{TemplateBinding Content}" FontFamily="{DynamicResource iconfont}" 
                                VerticalAlignment="Center" HorizontalAlignment="Center" Name="txt"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Visibility" Value="Visible" TargetName="hover"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{Binding Tag,RelativeSource={RelativeSource Mode=Self}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

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

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

相关文章

标题:探索pdf2image:将PDF文档转化为图像的Python魔法

标题&#xff1a;探索pdf2image&#xff1a;将PDF文档转化为图 像的Python魔法 背景 在数字时代&#xff0c;我们经常需要处理各种格式的文档&#xff0c;尤其是PDF文件。PDF以其跨平台的可读性和稳定性而广受欢迎。然而&#xff0c;有时我们需要将PDF文件转换成图像格式&am…

Golang | Leetcode Golang题解之第282题给表达式添加运算符

题目&#xff1a; 题解&#xff1a; func addOperators(num string, target int) (ans []string) {n : len(num)var backtrack func(expr []byte, i, res, mul int)backtrack func(expr []byte, i, res, mul int) {if i n {if res target {ans append(ans, string(expr))}…

Linux--Socket编程预备

目录 1. 理解源 IP 地址和目的 IP 地址 2.端口号 2.1端口号(port)是传输层协议的内容 2.2端口号范围划分 2.3理解 "端口号" 和 "进程 ID" 2.4理解 socket 3.传输层的典型代表 3.1认识 TCP 协议 3.2认识 UDP 协议 4. 网络字节序 5. socket 编程接…

【数据结构】--- 栈和队列

前言 前面学习了数据结构的顺序表、单链表、双向循环链表这些结构&#xff1b;现在就来学习栈和队列&#xff0c;这里可以简单的说栈和队列是具有特殊化的线性表 一、栈 1.1、栈的概念和结构 栈是一种遵循先入后出逻辑的线性数据结构。 栈是一种特殊的线性表&#xff0c;它只允…

矿场运输车4G视频监控管理解决方案

一、背景介绍 随着科技的不断进步和智能化时代的来临&#xff0c;矿业运输行业也在寻求更高效率与安全的管理手段。矿场运输车4G视频监控管理解决方案是一种基于4G网络技术的视频监控系统&#xff0c;专门用于监测和管理矿场内运输车辆的工作状态和安全情况。该方案为矿场运输…

【linux】在多核CPU下,好像看到不同进程在不同CPU调度

在2353这行打印的情况来看&#xff0c;操作系统好像给不同的进程分配不同的CPU&#xff0c;从上图来看&#xff0c;同一个进程好像基本使用的相同的CPU&#xff1a; 其实摸索syscall文件系统操作&#xff0c;本意是想找到内核文件系统中文件的创建&#xff0c;写入&#xff0c;…

C 观察者模式 Demo

目录 一、基础描述 二、Demo 最近需要接触到 MySQL 半同步插件&#xff0c;发现其中用到了观察者模式&#xff0c;之前没在 C 中用过&#xff0c;遂好奇心驱使下找了找资料&#xff0c;并写了个 Demo。 一、基础描述 观察者设计模式&#xff08;Observer Pattern&#xff0…

ts踩坑!使用可选链 ?.处理可能遇到的 undefined 或 null 值的情况,但是仍然收到一个关于可能为 undefined 的警告!

在 TypeScript 中&#xff0c;当你使用可选链&#xff08;Optional Chaining&#xff09;?. 时&#xff0c;你其实已经处理了可能遇到的 undefined 或 null 值的情况。但是&#xff0c;如果你仍然收到一个关于可能为 undefined 的警告&#xff0c;这可能是因为 TypeScript 的类…

Mybatis——快速入门

介绍 MyBatis是一款优秀的持久层&#xff08;Dao层&#xff09;框架&#xff0c;用于简化JDBC的开发。MyBatis 底层是基于 JDBC 实现的&#xff0c;它封装了 JDBC 的大部分功能&#xff0c;使得数据库操作更加便捷和高效。同时&#xff0c;MyBatis 也保留了 JDBC 的灵活性&…

unity2D游戏开发03状态控制

多态和动画 建立player-idle动画&#xff0c;取玩家最后两个图片 选中playcontroller控制器 将玩家动画拖进去 右键player-idle,选择set as layer Default state 右键点击Any State ,点击Make Transition 结果 动画参数 动画参数是动画控制器定义的变量&#xff0c;点击Param…

Matlab arrayfun 与 bsxfun——提高编程效率的利器!

许多人知道 MATLAB 向量化编程&#xff0c;少用 for 循环 可以提高代码运行效率&#xff0c;但关于代码紧凑化编程&#xff0c; arrayfun 与 bsxfun 两个重要函数却鲜有人能够用好&#xff0c;今天针对这两个函数举例说明其威力。 Matlab arrayfun 概述 arrayfun 是 Matlab …

one-api 源码调试配置

本文主要介绍通过 VSCode 调试 one-api 源码。 一、环境配置 1.1 VSCode 和 one-api 安装 首先,确保已经安装了 VSCode(下载链接)和 one-api 源码(下载链接)已下载并安装了依赖 1.2 安装 Go 插件 在 VSCode 中,安装 Go 插件。 1.3 安装 dlv 调试包 可以通过下载源码…

EEtrade:现货黄金盈利计算方法

现货黄金交易作为一种极具吸引力的投资方式&#xff0c;其盈利计算涉及多个关键因素&#xff0c;投资者需深入理解这些因素&#xff0c;才能准确评估交易结果&#xff0c;并制定科学的投资策略。 一、现货黄金基本盈利计算&#xff1a; 利润公式&#xff1a; 利润 (收盘价 -…

docker部署mysql8.x版本,编写shell脚本自动部署安装mysql

docker部署mysql8.x版本&#xff0c;编写shell脚本自动部署安装mysql **1.**先自行安装好docker环境&#xff0c;docker的镜像注册中心最好是国内的&#xff0c;例如执行一下命令直接修改docker配置&#xff0c; cat <<EOF > /etc/docker/daemon.json {"regist…

LabVIEW学习-LabVIEW处理带分隔符的字符串从而获取数据

带分隔符的字符串很好处理&#xff0c;只需要使用"分隔符字符串至一维字符串数组"函数或者"一维字符串数组至分隔符字符串"函数就可以很轻松地处理带分隔符地字符串。 这两个函数所在的位置为&#xff1a; 函数选板->字符串->附加字符串函数->分…

在STM32嵌入式中C/C++语言对栈空间的使用

像STM32这样的微控制器在进入main函数之前需要对栈进行初始化。可以说栈是C语言运行时的必要条件。我们知道栈实际上是一块内存空间&#xff0c;那么这块空间都用来存储什么呢&#xff1f;有什么办法能够优化栈空间的使用&#xff1f; 栈空间保存的内容 栈是一个先入后出的数据…

学术研讨 | 区块链网络体系结构研讨会顺利召开

添加图片注释&#xff0c;不超过 140 字&#xff08;可选&#xff09; 近日&#xff0c;国家区块链技术创新中心组织了“区块链网络体系结构研讨会”&#xff0c;会议面向跨域交互多、计算规模大、数据管理复杂、性能与扩展性要求高等特征的区块链网络的体系结构展开交流研讨&…

ShareDB:构建实时应用从未如此简单

项目介绍 ShareDB 是一个支持多用户实时协作的全栈库&#xff0c;适用于构建各种需要同步数据更新的在线应用&#xff0c;如在线文档编辑器、实时仪表板和多玩家游戏等。 它提供了一套全面的实时同步和多用户协作解决方案&#xff0c;具备异步最终一致性、实时查询订阅、数据库…

Win11 操作(四)g502鼠标连接电脑不亮灯无反应

罗技鼠标连接电脑不亮灯无反应 前言 罗技技术&#x1f4a9;中&#x1f4a9;&#xff0c;贴吧技术神中神&#xff01; 最近买了一个g502&#xff0c;结果买回来直接插上电脑连灯都不亮&#xff0c;问了一下客服。客服简单的让我换接口&#xff0c;又是下载ghub之类的&#xf…

ESP8266用AT指令实现连接MQTT

1准备工作 硬件&#xff08;ESP8266&#xff09;连接电脑 硬件已经烧入了MQTT透传固件 2实现连接 2-1&#xff08;进入AT模式&#xff09; 打开串口助手发送如下指令 AT 2-2&#xff08;复位&#xff09; ATRST 2-3&#xff08;开启DHCP&#xff0c;自动获取IP&#x…