C#委托实践之WPF窗体投票

本示例主要是C#委托知识的应用,通过OOP原则的分析,我们将代码集中在三个类当中,分别是MainWindow显示屏幕(窗体类)、VoteWindow投票器(窗体类)和Guest嘉宾类(序号、姓名、得票数)。

一、界面
1、显示屏幕(MainWindow)

显示屏幕

<Window x:Class="DelegateApp.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:DelegateApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5*"/>
            <ColumnDefinition Width="19*"/>
        </Grid.ColumnDefinitions>
        <Image HorizontalAlignment="Left" Height="192" Margin="92,56,0,0" VerticalAlignment="Top" Width="144" Source="001.jpg" Grid.ColumnSpan="2"/>
        <Image HorizontalAlignment="Left" Height="192" Margin="160.915,56,0,0" VerticalAlignment="Top" Width="144" Source="002.jpg" Grid.Column="1"/>
        <Image HorizontalAlignment="Left" Height="192" Margin="393.915,56,0,0" VerticalAlignment="Top" Width="144" Source="003.jpg" Grid.Column="1"/>
        <Label x:Name="labelCounter1" Content="王丽坤" HorizontalAlignment="Left" Margin="144,268,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
        <Label x:Name="labelCounter2" Content="刘亦菲" HorizontalAlignment="Left" Margin="214.915,268,0,0" VerticalAlignment="Top" Grid.Column="1"/>
        <Label x:Name="labelCounter3" Content="李沁沁" HorizontalAlignment="Left" Margin="453.915,268,0,0" VerticalAlignment="Top" Grid.Column="1"/>
        <Button x:Name="btnStartVote" Content="开启壹组投票" Click="btnStartVoteClick" HorizontalAlignment="Left" Margin="116,340,0,0" VerticalAlignment="Top" Width="75" Height="25" Grid.ColumnSpan="2"/>
        <Button x:Name="btnEndVote" Content="投票结束" Click="btnEndVoteClick" HorizontalAlignment="Left" Margin="426.915,340,0,0" VerticalAlignment="Top" Width="75" Height="25" Grid.Column="1"/>
        <TextBox x:Name="voteCounterForm" HorizontalAlignment="Left" Height="25" Margin="53.915,340,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="120" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Column="1"/>
    </Grid>
</Window>
2、投票器(VoteWindow)

投票器

<Window x:Class="DelegateApp.VoteWindow"
        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:DelegateApp"
        mc:Ignorable="d"
        Title="VoteWindow" Height="225" Width="400">
    <Grid>
        <Button x:Name="btnVote1" Content="王丽坤" HorizontalAlignment="Left" Margin="40,44,0,0" VerticalAlignment="Top" Width="75" Height="100" Click="btnVoteClick" Tag="1"/>
        <Button x:Name="btnVote2" Content="刘亦菲" HorizontalAlignment="Left" Margin="159,44,0,0" VerticalAlignment="Top" Width="75" Height="100" Click="btnVoteClick" Tag="2"/>
        <Button x:Name="btnVote3" Content="李沁沁" HorizontalAlignment="Left" Margin="274,44,0,0" VerticalAlignment="Top" Width="75" Height="100" Click="btnVoteClick" Tag="3"/>
    </Grid>
</Window>
二、后台功能
1、嘉宾类(Guest)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateApp
{
    class Guest
    {
        //序号
        public int Num { get; set; }
        //姓名
        public string Name { get; set; }
        //票数
        public int VoteCounter { get; set; } = 0;
    }
}
2、显示屏幕类(MainWindow)

在MainWindow类当中重点是对VoteForm对象和Guest对象的处理,并在点击按钮后通过 反射 循环创建指定数量的窗体实例。

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;
using System.Reflection;

namespace DelegateApp
{
    //【1】声明委托
    public delegate void StartVoteDelegate(int num);
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //创建保存嘉宾对象集合
        private Dictionary<int, Guest> dicGuest = null;
        //创建投票器窗体集合
        private List<Window> windowList = new List<Window>();
        public MainWindow()
        {
            InitializeComponent();
            //初始化嘉宾对象集合
            this.dicGuest = new Dictionary<int, Guest>()
            {
                [1] = new Guest { Num = 1, Name = "王丽坤" },
                [2] = new Guest { Num = 2, Name = "刘亦菲" },
                [3] = new Guest { Num = 3, Name = "李沁沁" },
            };
        }
        //【2】根据委托创建方法
        private void Receiver(int num)
        {
            //根据序号找到嘉宾对象,并使其投票总数+1
            this.dicGuest[num].VoteCounter++;
            //同步显示投票结果
            this.labelCounter1.Content = this.dicGuest[1].Name + this.dicGuest[1].VoteCounter.ToString() + "票";
            this.labelCounter2.Content = this.dicGuest[2].Name + this.dicGuest[2].VoteCounter.ToString() + "票";
            this.labelCounter3.Content = this.dicGuest[3].Name + this.dicGuest[3].VoteCounter.ToString() + "票";
        }

        //创建投票器对象
        private void btnStartVoteClick(object sender, RoutedEventArgs e)
        {
            int counter = Convert.ToInt32(this.voteCounterForm.Text.Trim());
            for (int i = 0; i < counter; i++)
            {
                //通过反射创建窗体(根据壹个类的完全限定名创建对象)
                VoteWindow voteWindow = (VoteWindow)Assembly.Load("DelegateApp").CreateInstance("DelegateApp.VoteWindow");
                voteWindow.Title = "投票器:" + (i + 1).ToString();
                //【4】将投票器窗体对象中的委托变量和当前窗体的委托实现方法关联
                voteWindow.voteDelegate = this.Receiver;
                voteWindow.Show();
                //添加投票器窗体到集合当中
                this.windowList.Add(voteWindow);
            }

        }
        //结束投票
        private void btnEndVoteClick(object sender, RoutedEventArgs e)
        {
            foreach (Window item in this.windowList)
            {
                item.Close();
            }
            this.btnStartVote.IsEnabled = false;
        }
    }
}
3、投票类(VoteWindow)

VoteWindow类当中主要的功能是创建委托变量并通过委托变量传递数据,在该类当中应用到了WPF窗体控件的遍历。

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 DelegateApp
{
    /// <summary>
    /// Interaction logic for VoteForm.xaml
    /// </summary>
    public partial class VoteWindow : Window
    {
        //【3】创建委托变量
        public StartVoteDelegate voteDelegate = null;
        public VoteWindow()
        {
            InitializeComponent();
        }
        //开始投票
        private void btnVoteClick(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            //【5】通过委托变量传递数据
            voteDelegate(Convert.ToInt32(btn.Tag));
            //投票后按钮禁用
            foreach (UIElement item in (this.Content as Grid).Children)
            {
                if (item is Button)
                {
                    ((Button)item).IsEnabled = false;
                    this.Title = "投票完成";
                }
            }
        }
    }
}
三、效果预览

投票吧
在投票窗体内进行过壹次投票后,该窗体显示“投票完成”,同时不可再操作。点击“投票结束”按钮后,关闭所有投票窗体,“开始壹组投票”按钮禁用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要创建C# WPF的Windows应用程序,可以按照以下步骤进行操作。首先,在Visual Studio中选择“文件”->“新建”->“项目”命令,弹出项目对话框。在该对话框中选择“WPF应用程序”,并设置项目名称、位置等信息,然后点击“确定”按钮。接下来,在项目文件夹中会有一个默认的程序MainWindow.xaml和MainWindow.xaml.cs。在MainWindow.xaml中,可以使用XAML语言来设计的布局和界面元素。例如,可以使用StackPanel来放置按钮和文本块等控件。在MainWindow.xaml.cs文件中,可以编写C#代码来处理的事件和逻辑。例如,可以在加载时设置控件的属性,或者在按钮点击事件中执行相应的操作。最后,可以通过运行应用程序来查看的效果。 #### 引用[.reference_title] - *1* *2* [C#创建Windows应用程序](https://blog.csdn.net/weixin_50610118/article/details/113002325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C# WPF通过WindowChrome自定义](https://blog.csdn.net/sD7O95O/article/details/127218714)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值