forked from yanw/App_win_iot_V2.0
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
344 lines
16 KiB
344 lines
16 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
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 IOTContainer.View
|
|
{
|
|
/// <summary>
|
|
/// ClockControl.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class ClockControl : UserControl
|
|
{
|
|
bool isStop = false;
|
|
Task task;
|
|
CancellationTokenSource source1 = new CancellationTokenSource();
|
|
string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
|
|
public ClockControl()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
public void BindClock(int type, int width, int height, string backgroundColor, string fontColor)
|
|
{
|
|
TextBlock textTime = new TextBlock();
|
|
TextBlock textWeek = new TextBlock();
|
|
TextBlock textDate = new TextBlock();
|
|
this.Width = width;
|
|
this.Height = height;
|
|
gridMain.Width = width;
|
|
gridMain.Height = Height;
|
|
BrushConverter converter = new BrushConverter();
|
|
if (!string.IsNullOrEmpty(backgroundColor))
|
|
{
|
|
Brush newFill = (Brush)converter.ConvertFromString(backgroundColor);
|
|
gridMain.Background = newFill;
|
|
}
|
|
if (!string.IsNullOrEmpty(fontColor))
|
|
{
|
|
Brush newFill = (Brush)converter.ConvertFromString(fontColor);
|
|
textTime.Foreground = newFill;
|
|
textWeek.Foreground = newFill;
|
|
textDate.Foreground = newFill;
|
|
}
|
|
if (type == 4)
|
|
{
|
|
Viewbox viewbox = new Viewbox();
|
|
viewbox.Width = width;
|
|
viewbox.Height = height;
|
|
gridMain.Children.Add(viewbox);
|
|
Grid grid = new Grid();
|
|
grid.Width = 320;
|
|
grid.Height = 80;
|
|
ColumnDefinition column1 = new ColumnDefinition();
|
|
column1.Width = GridLength.Auto;
|
|
grid.ColumnDefinitions.Add(column1);
|
|
grid.ColumnDefinitions.Add(new ColumnDefinition());
|
|
grid.RowDefinitions.Add(new RowDefinition());
|
|
grid.RowDefinitions.Add(new RowDefinition());
|
|
grid.Children.Add(textTime);
|
|
textTime.SetValue(Grid.ColumnProperty, 0);
|
|
textTime.SetValue(Grid.RowSpanProperty, 2);
|
|
textTime.VerticalAlignment = VerticalAlignment.Center;
|
|
textTime.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textTime.FontSize = 36;
|
|
textTime.FontWeight = FontWeights.Bold;
|
|
textTime.Margin = new Thickness(18, 0, 8, -10);
|
|
textTime.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textDate);
|
|
textDate.SetValue(Grid.ColumnProperty, 1);
|
|
textDate.SetValue(Grid.RowProperty, 0);
|
|
textDate.VerticalAlignment = VerticalAlignment.Bottom;
|
|
textDate.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textDate.FontSize = 14;
|
|
textDate.FontWeight = FontWeights.Light;
|
|
textDate.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textWeek);
|
|
textWeek.SetValue(Grid.ColumnProperty, 1);
|
|
textWeek.SetValue(Grid.RowProperty, 1);
|
|
textWeek.VerticalAlignment = VerticalAlignment.Top;
|
|
textWeek.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textWeek.FontSize = 14;
|
|
textWeek.FontFamily = new FontFamily(Application.Current.Resources["CNRegular"].ToString());
|
|
viewbox.Stretch = Stretch.Uniform;
|
|
viewbox.StretchDirection = StretchDirection.Both;
|
|
viewbox.Child = grid;
|
|
}
|
|
else if (type == 0)
|
|
{
|
|
Viewbox viewbox = new Viewbox();
|
|
viewbox.Width = width;
|
|
viewbox.Height = height;
|
|
gridMain.Children.Add(viewbox);
|
|
Grid grid = new Grid();
|
|
grid.Width = 320;
|
|
grid.Height = 80;
|
|
ColumnDefinition column1 = new ColumnDefinition();
|
|
column1.Width = GridLength.Auto;
|
|
ColumnDefinition column2 = new ColumnDefinition();
|
|
ColumnDefinition column3 = new ColumnDefinition();
|
|
column3.Width = GridLength.Auto;
|
|
grid.ColumnDefinitions.Add(column1);
|
|
grid.ColumnDefinitions.Add(column2);
|
|
grid.ColumnDefinitions.Add(column3);
|
|
grid.Children.Add(textTime);
|
|
textTime.SetValue(Grid.ColumnProperty, 0);
|
|
textTime.VerticalAlignment = VerticalAlignment.Center;
|
|
textTime.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textTime.FontSize = 24;
|
|
textTime.FontWeight = FontWeights.Bold;
|
|
textTime.Margin = new Thickness(18, 0, 0, 0);
|
|
textTime.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textWeek);
|
|
textWeek.SetValue(Grid.ColumnProperty, 1);
|
|
textWeek.VerticalAlignment = VerticalAlignment.Center;
|
|
textWeek.HorizontalAlignment = HorizontalAlignment.Center;
|
|
textWeek.FontSize = 20;
|
|
textWeek.Margin = new Thickness(0, 0, 0, 4);
|
|
textWeek.FontFamily = new FontFamily(Application.Current.Resources["CNRegular"].ToString());
|
|
|
|
grid.Children.Add(textDate);
|
|
textDate.SetValue(Grid.ColumnProperty, 2);
|
|
textDate.VerticalAlignment = VerticalAlignment.Center;
|
|
textDate.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textDate.FontSize = 24;
|
|
textDate.FontWeight = FontWeights.Light;
|
|
textDate.Margin = new Thickness(0, 0, 25, 0);
|
|
textDate.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
viewbox.Stretch = Stretch.Uniform;
|
|
viewbox.StretchDirection = StretchDirection.Both;
|
|
viewbox.Child = grid;
|
|
}
|
|
else if (type == 1)
|
|
{
|
|
Viewbox viewbox = new Viewbox();
|
|
viewbox.Width = width;
|
|
viewbox.Height = height;
|
|
gridMain.Children.Add(viewbox);
|
|
Grid grid = new Grid();
|
|
grid.Width = 320;
|
|
grid.Height = 80;
|
|
ColumnDefinition column1 = new ColumnDefinition();
|
|
column1.Width = GridLength.Auto;
|
|
ColumnDefinition column2 = new ColumnDefinition();
|
|
ColumnDefinition column3 = new ColumnDefinition();
|
|
column3.Width = GridLength.Auto;
|
|
grid.ColumnDefinitions.Add(column1);
|
|
grid.ColumnDefinitions.Add(column2);
|
|
grid.ColumnDefinitions.Add(column3);
|
|
|
|
|
|
grid.Children.Add(textDate);
|
|
textDate.SetValue(Grid.ColumnProperty, 0);
|
|
textDate.VerticalAlignment = VerticalAlignment.Center;
|
|
textDate.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textDate.FontSize = 24;
|
|
textDate.FontWeight = FontWeights.Light;
|
|
textDate.Margin = new Thickness(18, 0, 0, 0);
|
|
textDate.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textTime);
|
|
textTime.SetValue(Grid.ColumnProperty, 1);
|
|
textTime.VerticalAlignment = VerticalAlignment.Center;
|
|
textTime.HorizontalAlignment = HorizontalAlignment.Center;
|
|
textTime.FontSize = 24;
|
|
textTime.FontWeight = FontWeights.Bold;
|
|
textTime.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textWeek);
|
|
textWeek.SetValue(Grid.ColumnProperty, 2);
|
|
textWeek.VerticalAlignment = VerticalAlignment.Center;
|
|
textWeek.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textWeek.FontSize = 20;
|
|
textWeek.Margin = new Thickness(0, 0, 25, 4);
|
|
textWeek.FontFamily = new FontFamily(Application.Current.Resources["CNRegular"].ToString());
|
|
viewbox.Stretch = Stretch.Uniform;
|
|
viewbox.StretchDirection = StretchDirection.Both;
|
|
viewbox.Child = grid;
|
|
}
|
|
else if (type == 2)
|
|
{
|
|
Viewbox viewbox = new Viewbox();
|
|
viewbox.Width = width;
|
|
viewbox.Height = height;
|
|
gridMain.Children.Add(viewbox);
|
|
Grid grid = new Grid();
|
|
grid.Width = 320;
|
|
grid.Height = 80;
|
|
ColumnDefinition column = new ColumnDefinition();
|
|
column.Width = GridLength.Auto;
|
|
grid.ColumnDefinitions.Add(column);
|
|
grid.ColumnDefinitions.Add(new ColumnDefinition());
|
|
grid.RowDefinitions.Add(new RowDefinition());
|
|
grid.RowDefinitions.Add(new RowDefinition());
|
|
|
|
|
|
grid.Children.Add(textTime);
|
|
textTime.SetValue(Grid.ColumnProperty, 0);
|
|
textTime.SetValue(Grid.RowProperty, 0);
|
|
textTime.VerticalAlignment = VerticalAlignment.Bottom;
|
|
textTime.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textTime.FontSize = 24;
|
|
textTime.FontWeight = FontWeights.Bold;
|
|
textTime.Margin = new Thickness(18, 0, 0, 0);
|
|
textTime.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textDate);
|
|
textDate.SetValue(Grid.ColumnProperty, 0);
|
|
textDate.SetValue(Grid.RowProperty, 1);
|
|
textDate.VerticalAlignment = VerticalAlignment.Top;
|
|
textDate.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textDate.FontSize = 18;
|
|
textDate.FontWeight = FontWeights.Light;
|
|
textDate.Margin = new Thickness(18, 0, 0, 0);
|
|
textDate.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textWeek);
|
|
textWeek.SetValue(Grid.ColumnProperty, 1);
|
|
textWeek.SetValue(Grid.RowProperty, 1);
|
|
textWeek.VerticalAlignment = VerticalAlignment.Top;
|
|
textWeek.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textWeek.FontSize = 14;
|
|
textWeek.Margin = new Thickness(12, -1, 0, 0);
|
|
textWeek.FontFamily = new FontFamily(Application.Current.Resources["CNRegular"].ToString());
|
|
viewbox.Stretch = Stretch.Uniform;
|
|
viewbox.StretchDirection = StretchDirection.Both;
|
|
viewbox.Child = grid;
|
|
}
|
|
else
|
|
{
|
|
Viewbox viewbox = new Viewbox();
|
|
viewbox.Width = width;
|
|
viewbox.Height = height;
|
|
gridMain.Children.Add(viewbox);
|
|
Grid grid = new Grid();
|
|
grid.Width = 320;
|
|
grid.Height = 80;
|
|
ColumnDefinition column1 = new ColumnDefinition();
|
|
column1.Width = GridLength.Auto;
|
|
ColumnDefinition column2 = new ColumnDefinition();
|
|
ColumnDefinition column3 = new ColumnDefinition();
|
|
column3.Width = GridLength.Auto;
|
|
grid.ColumnDefinitions.Add(column1);
|
|
grid.ColumnDefinitions.Add(column2);
|
|
grid.ColumnDefinitions.Add(column3);
|
|
|
|
grid.Children.Add(textTime);
|
|
|
|
textTime.SetValue(Grid.ColumnProperty, 0);
|
|
textTime.VerticalAlignment = VerticalAlignment.Center;
|
|
textTime.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textTime.FontSize = 24;
|
|
textTime.FontWeight = FontWeights.Bold;
|
|
textTime.Margin = new Thickness(18, 0, 0, 0);
|
|
textTime.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textDate);
|
|
textDate.SetValue(Grid.ColumnProperty, 1);
|
|
textDate.VerticalAlignment = VerticalAlignment.Center;
|
|
textDate.HorizontalAlignment = HorizontalAlignment.Center;
|
|
textDate.FontSize = 14;
|
|
textDate.FontWeight = FontWeights.Light;
|
|
textDate.Margin = new Thickness(0, 0, 0, -6.5);
|
|
textDate.FontFamily = new FontFamily(Application.Current.Resources["BrownStd"].ToString());
|
|
|
|
grid.Children.Add(textWeek);
|
|
textWeek.SetValue(Grid.ColumnProperty, 2);
|
|
textWeek.VerticalAlignment = VerticalAlignment.Center;
|
|
textWeek.HorizontalAlignment = HorizontalAlignment.Left;
|
|
textWeek.FontSize = 11;
|
|
textWeek.Margin = new Thickness(0, 0, 110, -5);
|
|
textWeek.FontFamily = new FontFamily(Application.Current.Resources["CNRegular"].ToString());
|
|
viewbox.Stretch = Stretch.Uniform;
|
|
viewbox.StretchDirection = StretchDirection.Both;
|
|
viewbox.Child = grid;
|
|
}
|
|
|
|
|
|
gridMain.Dispatcher.Invoke(new Action(() =>
|
|
{
|
|
DateTime dtNow = DateTime.Now;
|
|
textDate.Text = dtNow.ToString("yyyy-MM-dd");
|
|
textWeek.Text = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];
|
|
textTime.Text = dtNow.ToShortTimeString().ToString();
|
|
|
|
}));
|
|
CancellationToken token = source1.Token;
|
|
TaskFactory factory = new TaskFactory(token);
|
|
task = factory.StartNew(() =>
|
|
{
|
|
var curDate = DateTime.Now;
|
|
while (!isStop)
|
|
{
|
|
if (token.IsCancellationRequested)
|
|
{
|
|
// 释放资源操作等等...
|
|
break;
|
|
}
|
|
if (curDate.AddMilliseconds(2000) <= DateTime.Now)
|
|
{
|
|
gridMain.Dispatcher.Invoke(new Action(() =>
|
|
{
|
|
DateTime dtNow = DateTime.Now;
|
|
textDate.Text = dtNow.ToString("yyyy-MM-dd");
|
|
textWeek.Text = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];
|
|
textTime.Text = dtNow.ToShortTimeString().ToString();
|
|
|
|
}));
|
|
curDate = DateTime.Now;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(100);
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
isStop = true;
|
|
if (task != null && (task.IsCanceled || task.IsCompleted || task.IsFaulted))
|
|
{
|
|
task.Dispose();
|
|
}
|
|
source1.Cancel();
|
|
}
|
|
}
|
|
}
|
|
|