• 我去年码了个表(WPF MvvM)


    又快个把月没写博客了(最近忙着学JAVA去了,都是被逼的/(ㄒoㄒ)/~~),然后用WPF码了个表,其实想加上那种提醒功能什么的,额,就这样了吧,主要是感受一下数据驱动的思想。

    效果如下:

    前端XAML有两层:底层表盘,刻度是使用的ItemsControl,后来在园子里找到了一个美工做的界面,原来WPF本身就提供了一种PathListBox的东西可以很简单的就实现这种效果

            <ItemsControl Height="200" Width="200" Name ="Nine" Background="Transparent">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Height="200" Width="200"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                            <Line X1 ="100" X2="100" Y1="10" Y2="25" Stroke="Yellow" StrokeThickness="5">
                                <Line.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform Angle="{Binding Kdz,Mode=OneWay}" CenterX="100" CenterY="100"/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Line.RenderTransform>
                            </Line>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

    主窗口主喜闻乐见的数据绑定:绑定界面的刻度和启动定时器来刷新时间

    namespace WpfApplication1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            System.Windows.Threading.DispatcherTimer dtimer;
            Clock clock;
            public MainWindow()
            {
                InitializeComponent();
    
                var kds = new List<KD>();
                for (int i = 0; i < 12;i++ )
                {
                    kds.Add(new KD(i*30));
                }
                Nine.ItemsSource = kds;
    
                dtimer = new System.Windows.Threading.DispatcherTimer();
                dtimer.Interval = new TimeSpan(0,0,0,0,500);
                dtimer.Tick += dtimer_Tick; 
                clock = new Clock();
                DataContext = clock;
                dtimer.Start();
            }
    
            void dtimer_Tick(object sender, EventArgs e)
            {
                clock.Time = DateTime.Now;
            }
        }
    
        public class KD
        {
            int _kd;
    
            public KD(int kd)
            {
                _kd = kd;
            }
    
            public int Kdz
            {
                get
                {
                    return _kd;
                }
            }
        }
    }

    核心的MV是Clock类,对外暴露了一个Time属性,设置它的时候会触发其他几个属性的通知时间来驱动界面刷新(这可能跟正常的写法略有不同,反正就是那个用法了,不用在意这些细节),单向绑定,所以不需要有set属性

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WpfApplication1
    {
        public class Clock : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            DateTime time;
    
            public Clock()
            {
                time = DateTime.Now;
            }
            public DateTime Time
            {
                set
                {
                    time = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("StrTime"));
                    PropertyChanged(this, new PropertyChangedEventArgs("Hour"));
                    PropertyChanged(this, new PropertyChangedEventArgs("Minute"));
                    PropertyChanged(this, new PropertyChangedEventArgs("Second"));
                }
            }
    
            public string StrTime
            {
                get
                {
                    return time.ToString("HH:mm:ss");
                }
            }
    
            public int Hour
            {
                get
                {
                    return time.Hour * 30 + time.Minute /2;
                }
            }
    
            public int Minute
            {
                get
                {
                    return time.Minute * 6;
                }
            }
    
            public int Second
            {
                get
                {
                    return time.Second * 6;
                }
            }
        }
    }

    对于WPF最为欣赏的就是数据绑定,WPF很多设计还是很好很强大的,比起winform简直就是一种巨大的进步,至于撸WPF纯属一点小兴趣,平常也不怎么用WPF,所以这些也就随意的写写了,更多是一种学习,大牛勿喷。。。

     源码:下载

  • 相关阅读:
    Python获取秒级时间戳与毫秒级时间戳
    时间戳与时间类型转化(秒级时间戳)
    linux压缩和解压缩命令
    对于Python中@property的理解和使用
    探索性测试方法
    Linux 中 grep 命令的 12 个实践例子
    在 Linux 启动或重启时执行命令与脚本
    亲测的orabbix监控Oracle过程
    find 使用搜集
    Centos7.3-mysql5.7复制安装过程
  • 原文地址:https://www.cnblogs.com/HelliX/p/5969123.html
Copyright © 2020-2023  润新知