1. 九宮格抽獎
1.1 View
<UserControl>
<UserControl.Background>
<ImageBrush
ImageSource="/Image/BackGround05.jpg"
Opacity="0.8"
Stretch="Fill" />
</UserControl.Background>
<StackPanel
Margin="20"
HorizontalAlignment="Center"
Orientation="Vertical">
<WrapPanel
Width="310"
Height="310"
Margin="25"
HorizontalAlignment="Center">
<Border
BorderBrush="Gainsboro"
BorderThickness="5"
CornerRadius="10">
<Border.Background>
<RadialGradientBrush>
<RadialGradientBrush.GradientOrigin>
<Point X="0.75" Y="0.25" />
</RadialGradientBrush.GradientOrigin>
<RadialGradientBrush.GradientStops>
<GradientStop Offset="0.0" Color="Yellow" />
<GradientStop Offset="0.5" Color="Orange" />
<GradientStop Offset="1.5" Color="Red" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Border.Background>
<ItemsControl
x:Name="Nine"
Margin="5"
ItemsSource="{Binding PrizeList, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- 九宫格内容 -->
<Button
Width="80"
Height="80"
Margin="8"
BorderBrush="{Binding Color}"
BorderThickness="3"
FontSize="11"
FontWeight="Bold">
<Button.Background>
<ImageBrush ImageSource="{Binding Path}" Stretch="Fill" />
</Button.Background>
<DockPanel>
<TextBlock Margin="0,60,0,0" Text="{Binding Title}" />
</DockPanel>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
</WrapPanel>
<StackPanel
Margin="20"
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button
Width="85"
Height="40"
Margin="10"
Command="{Binding ExecuteCommand}"
CommandParameter="Lottery"
Content="抽獎"
FontSize="17"
FontWeight="Bold"
IsEnabled="{Binding ButtonIsEnable}" />
<Button
Width="85"
Height="40"
Margin="10"
Command="{Binding ExecuteCommand}"
CommandParameter="Refresh"
Content="刷新"
FontSize="17"
FontWeight="Bold"
Visibility="Collapsed" />
</StackPanel>
</StackPanel>
</UserControl>
1.2 ViewModel
主要就是使用兩個Timer,第一個實現轉圈圈,第二個移動到中獎的位置
using DryIoc;
using NPOI.HSSF.Record;
using NPOI.SS.Formula.Eval;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
namespace LotteryDrawSystem.ViewModels
{
class SquaredUpViewModel : BindableBase
{
private Random _random;
private int AlltimerCount = 0;
private int LuckIndex = 0;
private int LoopCount = 0;
private static Timer AllTimer;
private static Timer LuckTimer;
private bool buttonIsEnable = true;
public bool ButtonIsEnable
{
get { return buttonIsEnable; }
set { buttonIsEnable = value; RaisePropertyChanged(); }
}
private List<PrizeDto> prizeList;
public List<PrizeDto> PrizeList
{
get { return prizeList; }
set { prizeList = value; RaisePropertyChanged(); }
}
public DelegateCommand<string> ExecuteCommand { get; private set; }
public SquaredUpViewModel()
{
_random = new Random();
//加載數據
PrizeList = new List<PrizeDto>();
AddItem();
InitialFunction();
//按鈕
ExecuteCommand = new DelegateCommand<string>(ButtonFunction);
}
public void ButtonFunction(string obj)
{
switch (obj)
{
case "Lottery":
AllFunction();
break;
case "Refresh":
Refresh();
break;
}
}
public void AddItem()
{
for (int i = 0; i < 9; i++)
{
if (i == 4)
{
string path = "/Image/Squared/Nine11.jpg";
PrizeList.Add(new PrizeDto() { Title = "", Path = path, Color = "AntiqueWhite" });
}
else
{
int num = i + 1;
string path = "/Image/Squared/Nine0" + num + ".jpg";
string msg = "00" + i;
PrizeList.Add(new PrizeDto() { Title = msg, Path = path, Color = "AntiqueWhite" });
}
}
}
public void InitialFunction()
{
AllTimer = new System.Timers.Timer();
AllTimer.Elapsed += AllTimedEvent;
AllTimer.Interval = 80;
LuckTimer = new System.Timers.Timer(250);
LuckTimer.Elapsed += LuckTimedEvent;
LuckTimer.Interval = 250;
}
public void Refresh()
{
PrizeList[LuckIndex].Color = "AntiqueWhite";
}
public void AllFunction()
{
ButtonIsEnable = false;
AllTimer.Start();
while (true)
{
LuckIndex = _random.Next(0, PrizeList.Count);
// 如果随机数不等于4,退出循环
if (LuckIndex != 4)
{
System.Diagnostics.Trace.WriteLine("----Final Index : " + LuckIndex.ToString());
break;
}
}
}
private void AllTimedEvent(Object source, ElapsedEventArgs e)
{
RollingChangeColor();
RollingChangeIndex();
LoopCount++;
//最後幾圈慢慢停
if (LoopCount == 100)
{
AllTimer.Interval = 200;
}
if (LoopCount > 110)
{
AllTimer.Stop();
Task.Delay(10);
System.Diagnostics.Trace.WriteLine("----:"+AlltimerCount);
if (AlltimerCount < 3 && AlltimerCount != 0)
{
PrizeList[AlltimerCount - 1].Color = "AntiqueWhite";
}
else if (AlltimerCount == 0)
{
PrizeList[3].Color = "AntiqueWhite";
}
else if (AlltimerCount == 3)
{
PrizeList[6].Color = "AntiqueWhite";
}
else if (AlltimerCount == 5)
{
PrizeList[2].Color = "AntiqueWhite";
}
else if (AlltimerCount == 6)
{
PrizeList[7].Color = "AntiqueWhite";
}
else if (AlltimerCount == 7)
{
PrizeList[8].Color = "AntiqueWhite";
}
else if (AlltimerCount == 8)
{
PrizeList[5].Color = "AntiqueWhite";
}
AllTimer.Interval = 80;
LoopCount = 0;
LuckTimer.Start();
}
}
public void LuckTimedEvent(Object source, ElapsedEventArgs e)
{
var luckEntity = PrizeList[LuckIndex];
if (PrizeList[AlltimerCount].Equals(luckEntity))
{
PrizeList[AlltimerCount].Color = "Red";
LuckTimer.Stop();
AlltimerCount = LuckIndex;
ButtonIsEnable = true;
}
else
{
RollingChangeIndex();
System.Diagnostics.Trace.WriteLine("----ALL Index : " + AlltimerCount);
RollingChangeColor();
}
}
public void RollingChangeColor()
{
PrizeList[AlltimerCount].Color = "Red";
if (AlltimerCount < 3 && AlltimerCount != 0)
{
PrizeList[AlltimerCount - 1].Color = "AntiqueWhite";
}
else if (AlltimerCount == 0)
{
PrizeList[3].Color = "AntiqueWhite";
}
else if (AlltimerCount == 3)
{
PrizeList[6].Color = "AntiqueWhite";
}
else if (AlltimerCount == 5)
{
PrizeList[2].Color = "AntiqueWhite";
}
else if (AlltimerCount == 6)
{
PrizeList[7].Color = "AntiqueWhite";
}
else if (AlltimerCount == 7)
{
PrizeList[8].Color = "AntiqueWhite";
}
else if (AlltimerCount == 8)
{
PrizeList[5].Color = "AntiqueWhite";
}
}
public void RollingChangeIndex()
{
if (AlltimerCount < 2)
{
AlltimerCount++;
}
else if (AlltimerCount == 2)
{
AlltimerCount = 5;
}
else if (AlltimerCount == 5)
{
AlltimerCount = 8;
}
else if (AlltimerCount == 8)
{
AlltimerCount = 7;
}
else if (AlltimerCount == 7)
{
AlltimerCount = 6;
}
else if (AlltimerCount == 6)
{
AlltimerCount = 3;
}
else if (AlltimerCount == 3)
{
AlltimerCount = 0;
}
}
}
}
1.3 Picture
背景圖片網址:
2. 集合中抽取前十
2.1 Excel Upload Data
2.1.1 Excel Upload Data
<UserControl>
<UserControl.Background>
<ImageBrush
ImageSource="/Image/BackGround06.jpg"
Opacity="0.8"
Stretch="Fill" />
</UserControl.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border
Width="660"
Margin="15,30,15,15"
BorderBrush="BurlyWood"
BorderThickness="2"
CornerRadius="10">
<DockPanel Width="650" LastChildFill="False">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock
Margin="15,25,5,5"
FontSize="20"
FontWeight="Bold"
Text="Select the file path : " />
<Button
Height="40"
Margin="5,20,5,5"
HorizontalAlignment="Center"
Command="{Binding OpenExcelPathCommand}"
Content="選擇檔案"
FontSize="20" />
</StackPanel>
<StackPanel
Margin="15"
HorizontalAlignment="Left"
VerticalAlignment="Center"
DockPanel.Dock="Top">
<TextBlock
FontSize="18"
FontWeight="Bold"
Foreground="Aquamarine"
Text="{Binding FilePath}" />
</StackPanel>
<StackPanel
Margin="0,0,10,15"
HorizontalAlignment="Right"
DockPanel.Dock="Bottom"
Orientation="Horizontal">
<Button
Width="120"
Height="40"
Margin="5"
Command="{Binding UploadExcelCommand}"
Content="Upload" />
<Button
Width="120"
Height="40"
Margin="5"
Command="{Binding CancelCommand}"
Content="Cancle" />
</StackPanel>
</DockPanel>
</Border>
<StackPanel
Grid.Row="1"
Margin="10,15,10,10"
Orientation="Vertical">
<TextBlock
Margin="10"
FontSize="20"
FontWeight="Bold"
Text="Result:" />
<ListBox
x:Name="MenuBar"
Margin="6"
AutomationProperties.Name="DemoPagesListBox"
ItemsSource="{Binding UserList}"
Style="{StaticResource MaterialDesignChoiceChipPrimaryListBox}">
<ListBox.Resources>
<Style BasedOn="{StaticResource MaterialDesignScrollBarMinimal}" TargetType="ScrollBar" />
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate DataType="domain:DemoItem">
<StackPanel Orientation="Horizontal">
<TextBlock
Margin="2"
FontSize="15"
FontWeight="Bold"
Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</UserControl>
2.1.2 ViewModel
using Microsoft.VisualBasic;
using Microsoft.Win32;
using NPOI.SS.UserModel;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LotteryDrawSystem.ViewModels
{
public class UploadExcelViewModel : BindableBase
{
public readonly IEventAggregator aggregator;
private string filePath;
public string FilePath
{
get { return filePath; }
set { filePath = value; RaisePropertyChanged(); }
}
private ObservableCollection<string> userList;
public ObservableCollection<string> UserList
{
get { return userList; }
set { userList = value; RaisePropertyChanged(); }
}
public DelegateCommand OpenExcelPathCommand { get; private set; }
public DelegateCommand UploadExcelCommand { get; private set; }
public DelegateCommand CancelCommand { get; private set; }
public UploadExcelViewModel(IEventAggregator aggregator)
{
this.aggregator = aggregator;
OpenExcelPathCommand = new DelegateCommand(OpenExcelPath);
UploadExcelCommand = new DelegateCommand(UploadExcel);
CancelCommand = new DelegateCommand(CancelExecute);
UserList = new ObservableCollection<string>();
}
public void CancelExecute()
{
FilePath = null;
UserList = null;
}
public void OpenExcelPath()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "All Files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
// 用户选择了一个文件
FilePath = openFileDialog.FileName;
}
}
public void UploadExcel()
{
if (string.IsNullOrEmpty(FilePath))
{
FilePath = "請選擇文件";
}
else
{
UserList = new ObservableCollection<string>();
var fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
var workbook = WorkbookFactory.Create(fs);
var sheet = workbook.GetSheetAt(0);
if (sheet != null)
{
//最后一列的标号
int rowCount = sheet.LastRowNum;
for (int i = 0; i <= rowCount; i++)
{
IRow row = sheet.GetRow(i);
ICell cell = row.GetCell(0);
if (cell != null)
{
string cellValue = cell.StringCellValue;
if (!string.IsNullOrEmpty(cellValue))
{
UserList.Add(cellValue);
}
}
}
}
if (UserList.Count > 0) {
aggregator.SendMessage(UserList);
}
}
}
}
}
2.1.3 DialogExtension
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LotteryDrawSystem.Extension
{
public static class DialogExtension
{
public static void RegisterMessage(this IEventAggregator aggregator,
Action<MessageModel> action)
{
aggregator.GetEvent<MessageEvent>().Subscribe(action);
}
/// <summary>
/// 发送提示消息
/// </summary>
/// <param name="aggregator"></param>
/// <param name="message"></param>
public static void SendMessage(this IEventAggregator aggregator, object message)
{
aggregator.GetEvent<MessageEvent>().Publish(new MessageModel()
{
Message = message
});
}
}
}
2.2 抽獎
2.2.1 View
<UserControl>
<UserControl.Background>
<ImageBrush ImageSource="/Image/BackGround08.jpg"
Opacity="1.19"
Stretch="Fill" />
</UserControl.Background>
<UserControl.Resources />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="500" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Hidden">
<StackPanel HorizontalAlignment="Center">
<ItemsControl Margin="5"
ItemsSource="{Binding UserList, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<materialDesign:Card Width="120"
Height="100"
Margin="6"
Padding="-0.5"
Foreground="{DynamicResource PrimaryHueDarkForegroundBrush}">
<Border Background="{Binding Color}"
BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Width="95"
Height="65"
Margin="0,5,0,0"
BorderThickness="2">
<Button.Background>
<ImageBrush ImageSource="{Binding Path}" />
</Button.Background>
</Button>
<TextBlock Grid.Row="2"
Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontWeight="Bold"
Foreground="Black"
Style="{StaticResource MaterialDesignBody2TextBlock}"
Text="{Binding Name}" />
</Grid>
</Border>
</materialDesign:Card>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
<StackPanel Grid.Row="1"
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button Width="85"
Height="50"
Margin="10"
Command="{Binding ButtonCommand}"
CommandParameter="Lottery"
Content="抽獎"
FontSize="18"
FontWeight="Bold"
IsEnabled="{Binding ButtonIsEnable}" />
<Button Width="85"
Height="50"
Margin="10"
Command="{Binding ButtonCommand}"
CommandParameter="Save"
Content="保存"
FontSize="18"
FontWeight="Bold" />
</StackPanel>
<WrapPanel Grid.Row="2"
HorizontalAlignment="Center"
Orientation="Horizontal">
<ItemsControl Margin="2"
ItemsSource="{Binding ResultList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<materialDesign:Card Width="130"
Height="25"
Margin="3"
Foreground="{DynamicResource PrimaryHueDarkForegroundBrush}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60" />
<ColumnDefinition Width="70" />
</Grid.ColumnDefinitions>
<TextBlock Margin="8,2,2,2"
VerticalAlignment="Center"
FontSize="15"
FontWeight="Bold"
Foreground="Black"
Text="{Binding Path}" />
<TextBlock Grid.Column="2"
Margin="2,2,2,1"
VerticalAlignment="Center"
FontSize="15"
FontWeight="Bold"
Foreground="Black"
Text="{Binding Name}" />
</Grid>
</materialDesign:Card>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</WrapPanel>
</Grid>
</UserControl>
2.2.2 ViewModel
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Timers;
namespace LotteryDrawSystem.ViewModels
{
public class LotteryDrawViewModel : BindableBase
{
private readonly IEventAggregator aggregator;
private int AlltimerCount = 0;
private int LoopCount = 0;
private int LuckIndex = 0;
private Random random = new Random();
private static Timer AllTimer;
private static Timer LuckTimer;
/// <summary>
/// 抽獎人員
/// </summary>
private ObservableCollection<ItemShow> userList;
public ObservableCollection<ItemShow> UserList
{
get { return userList; }
set { userList = value; RaisePropertyChanged(); }
}
/// <summary>
/// 中獎人員
/// </summary>
private ObservableCollection<ItemShow> resultList;
public ObservableCollection<ItemShow> ResultList
{
get { return resultList; }
set { resultList = value; RaisePropertyChanged(); }
}
private bool buttonIsEnable = true;
public bool ButtonIsEnable
{
get { return buttonIsEnable; }
set { buttonIsEnable = value; RaisePropertyChanged(); }
}
public DelegateCommand<string> ButtonCommand { get; private set; }
public LotteryDrawViewModel(IEventAggregator aggregator)
{
this.aggregator = aggregator;
ButtonCommand = new DelegateCommand<string>(ButtonFunction);
UserList = new ObservableCollection<ItemShow>();
ResultList = new ObservableCollection<ItemShow>();
InitialLoadData();
InitialLoadTimer();
}
public void ButtonFunction(string obj)
{
switch (obj)
{
case "Lottery":
Start();
break;
case "Save":
Save();
break;
}
}
public void Start()
{
ButtonIsEnable = false;
// 开始定时器
AllTimer.Start();
//先放入結果
LuckIndex = random.Next(0, UserList.Count);
System.Diagnostics.Trace.WriteLine("----Final Index : " + LuckIndex.ToString());
}
public void InitialLoadData()
{
aggregator.RegisterMessage((arg) =>
{
UserList = new ObservableCollection<ItemShow>();
ResultList = new ObservableCollection<ItemShow>();
var list = (ObservableCollection<string>)arg.Message;
foreach (string item in list)
{
int number = list.IndexOf(item)+1;
string path = "/Image/Lottery/Profile" + number + ".jpg";
int index = ResultList.Count;
UserList.Add(new ItemShow() { Index = index, Path = path, Name = item ,Color = "Azure" });
}
});
}
public void InitialLoadTimer()
{
AllTimer = new System.Timers.Timer();
AllTimer.Elapsed += AllTimedEvent;
AllTimer.Interval = 100;
LuckTimer = new System.Timers.Timer(250);
LuckTimer.Elapsed += LuckTimedEvent;
LuckTimer.Interval = 250;
}
public void AllTimedEvent(Object source, ElapsedEventArgs e)
{
UserList[AlltimerCount].Color = "Aqua";
if (AlltimerCount != 0)
{
UserList[AlltimerCount - 1].Color = "Azure";
}
if (AlltimerCount == 0)
{
UserList[UserList.Count-1].Color = "Azure";
}
AlltimerCount++;
if (AlltimerCount == UserList.Count)
{
AlltimerCount = 0;
}
LoopCount++;
if (LoopCount == 120)
{
AllTimer.Interval = 220;
}
if (LoopCount > 140)
{
AllTimer.Stop();
UserList[AlltimerCount-1].Color = "Azure";
System.Diagnostics.Trace.WriteLine("----ALL Index : " + AlltimerCount);
AllTimer.Interval = 100;
LoopCount = 0;
LuckTimer.Start();
}
}
public void LuckTimedEvent(Object source, ElapsedEventArgs e)
{
if (AlltimerCount == LuckIndex)
{
UserList[AlltimerCount].Color = "Aqua";
LuckTimer.Stop();
AlltimerCount = 0;
ButtonIsEnable = true;
}
else
{
AlltimerCount ++;
if (AlltimerCount == UserList.Count) {
AlltimerCount = 0;
}
UserList[AlltimerCount].Color = "Aqua";
if (AlltimerCount != 0)
{
UserList[AlltimerCount - 1].Color = "Azure";
}
if (AlltimerCount == 0)
{
UserList[UserList.Count - 1].Color = "Azure";
}
}
}
public void Save() {
int index = ResultList.Count + 1;
string zIndex = "第" + index + "名 : ";
var luckEntity = UserList[LuckIndex];
ResultList.Add(new ItemShow() { Path = zIndex, Name = luckEntity.Name });
//然後移除
UserList.RemoveAt(LuckIndex);
}
}
}