当前位置: 首页 > article >正文

【C#】WPF MVVM 简单示例代码

1. 目录结构

在这里插入图片描述

2. 代码

2.1 DelegateCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MVVSSample.Commands
{
    class DelegateCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            if (this.CanExcuteFunc == null)
            {
                return true;
            }
            return this.CanExcuteFunc(parameter);
        }

        public void Execute(object parameter)
        {
            if(this.ExcuteAction == null)
            {
                return;
            }

            this.ExcuteAction(parameter);
        }

        public Action<object> ExcuteAction{ get; set;}
        public Func<object, bool> CanExcuteFunc { get; set; }
    }
}

2.2 MainWindowViewModel.cs

using Microsoft.Win32;
using MVVSSample.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVVSSample.ViewModels
{
    class MainWindowViewModel : NotificationObject
    {
        private double input1;

        public double Input1
        {
            get { return input1; }
            set
            {
                input1 = value;
                this.RaisePropertyChanged("Input1");
            }
        }

        private double input2;

        public double Input2
        {
            get { return input2; }
            set
            {
                input2 = value;
                this.RaisePropertyChanged("Input2");
            }
        }

        private double result;

        public double Result
        {
            get { return result; }
            set
            {
                result = value;
                this.RaisePropertyChanged("Result");
            }
        }

        public DelegateCommand AddCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }

        private void Add(object parameter)
        {
            this.Result = this.Input1 + this.Input2;
        }

        private void Save(object parameter)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.ShowDialog();
        }

        //构造函数
        public MainWindowViewModel()
        {
            this.AddCommand = new DelegateCommand();
            this.AddCommand.ExcuteAction = new Action<object>(this.Add);

            this.SaveCommand = new DelegateCommand();
            this.SaveCommand.ExcuteAction = new Action<object>(this.Save);
        }

    }
}

2.3 NotificationObject.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVVSSample.ViewModels
{
    class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if(this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

2.4 App.xaml

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

2.5 MainWindow.xaml

<Window x:Class="MVVSSample.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:MVVSSample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
    <!--界面1-->
    <!--<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

            <Menu>
            <MenuItem Header="_File">
                <MenuItem Header="_Save" Command="{Binding SaveCommand}" />
            </MenuItem>
        </Menu>

        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Slider x:Name="slider1" Grid.Row="0" Background="LightBlue" Minimum="-100" Maximum="100" Margin="4" 
                    Value="{Binding Input1}" />
            <Slider x:Name="slider2" Grid.Row="1" Background="LightBlue" Minimum="-100" Maximum="100" Margin="4" 
                    Value="{Binding Input2}" />
            <Slider x:Name="slider3" Grid.Row="2" Background="LightBlue" Minimum="-100" Maximum="100" Margin="4" 
                    Value="{Binding Result}" />
            <Button x:Name="addButton" Grid.Row="3" Content="Add" Width="120" Height="80" Command="{Binding AddCommand}" />

        </Grid>
        
    </Grid>-->

    <!--界面2-->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Content="Save" Command="{Binding SaveCommand}" />
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBox x:Name="tb1" Grid.Row="0" Background="LightBlue" FontSize="24" Margin="4" Text="{Binding Input1}"/>
            <TextBox x:Name="tb2" Grid.Row="1" Background="LightBlue" FontSize="24" Margin="4" Text="{Binding Input2}"/>
            <TextBox x:Name="tb3" Grid.Row="2" Background="LightBlue" FontSize="24" Margin="4" Text="{Binding Result}" />
            <Button x:Name="addButton" Grid.Row="3" Content="Add" Width="120" Height="80" Command="{Binding AddCommand}"/>
        </Grid>
    </Grid>

</Window>

2.6 MainWindow.xaml.cs

using MVVSSample.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.Navigation;
using System.Windows.Shapes;

namespace MVVSSample
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowViewModel();
        }
    }
}

3. 界面效果

3.1 效果1

在这里插入图片描述

3.2 效果2

在这里插入图片描述

4. 参考

  • 刘铁猛WPF教程

http://www.kler.cn/news/353221.html

相关文章:

  • 文通车牌识别相机在工地称重应用中的卓越表现
  • JVM、字节码文件介绍
  • (三十二)实现一个基本的文件上传功能的Flask应用
  • RNN,LSTM,GRU的区别和联系? RNN的梯度消失问题?如何解决?
  • 人工智能:开启未来生活与工作的新征程
  • 搭建微信AI机器人
  • 深入了解Spring重试组件spring-retry
  • 【python】极简教程4-接口设计
  • 开源影像tif切图工具gdal2tiles部署以及切图
  • 给定数组找出出现次数超过数组长度一半的数
  • ETL转换:金蝶云和旺店通数据集成全流程
  • 详解23种设计模式
  • MySQL新手向:对比常用存储引擎
  • 人工智能正在扼杀云计算的可持续性
  • gitlab的基本用法之创建用户和组
  • Python基础07_推导式函数
  • 电子电气架构---汽车OEM敏捷式集成方案简介
  • 第5天:视图和控件-补充材料——`MainActivity.kt`解读
  • 【力扣150Golang】除自身以外数组的乘积
  • SketchUp Pro 2024 for Mac 3D建模 草图设计大师软件安装【保姆级教程,简单小白轻松上手】
  • 箭头函数语法及书写规则。
  • 大模型量化算法之LLM.int8()
  • C语言——链表
  • (31)oracle数据泵导出
  • 使用Python语言结合OpenCV库来处理视频流和条形码/二维码的识别
  • docker逃逸方法汇总与简要分析