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);
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    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
{
    /// 
    /// Interaction logic for VoteForm.xaml
    /// 
    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 = "投票完成";
                }
            }
        }
    }
}

三、效果预览


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

你可能感兴趣的:(NET)