• WPF使用笔记-计时器,多线程更新界面,焦点移动等


    实习期间开始使用WPF,记录一点东西,没什么技术含量。

    // 计时器

    System.Windows.Threading.DispatcherTimer ShowTimer;

    ShowTimer = new System.Windows.Threading.DispatcherTimer();

    ShowTimer.Tick += new EventHandler(ShowCurTimer);

    ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);

    ShowTimer.Start();

    private void ShowCurTimer(object sender, EventArgs e)

    {

      // 时分秒 this.TimeText.Text = DateTime.Now.ToString("HH:mm:ss");

      // 日期 农历 5月1日 星期日

      string MM = DateTime.Now.ToString("MM").TrimStart('0');

       string dd = DateTime.Now.ToString("dd").TrimStart('0');

       this.DateText.Text = "阳历 ";

      this.DateText.Text += MM;

      this.DateText.Text += "月";

       this.DateText.Text += dd;

       this.DateText.Text += "日 ";

       this.DateText.Text += DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));

    }

    // 线程

    System.Threading.Thread threadRead;

    // 互斥锁(看情况使用)

    object lockMe = new object();

    threadRead = new System.Threading.Thread(threadReadFun);

    threadRead.Start();

    private void threadReadFun()

    {

      while (true)

      {

        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.5));

         // 上锁 lock (lockMe) { }

         // 多线程中更新界面

         this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new delegate1(show));

      }

    }

    // 上述线程中无法对界面控件进行更新,可定义委托用于在多线程中更新界面

    public delegate void delegate1();

    private void show()

    {

      this.TextBlockName.Text = "new";

    }

    // 后台操作控件,可通过查找Name

    ((Image)this.FindName("Image1")).Visibility = System.Windows.Visibility.Hidden;

    // 多窗口关闭

    // xaml

    Closing="onClosing"

    // cs

    private void onClosing(object sender, System.ComponentModel.CancelEventArgs e)

    {

      Environment.Exit(0);

    }

    // TextBox

    GotFocus, LostFocus 获得和失去焦点事件,Tab获得焦点后使用 TextBox.SelectAll()选中所有内容 这对键盘重新输入内容很方便


    使用按键控制焦点移动:

    private void keyDown(object sender, KeyEventArgs e)
    {
      UIElement element = Keyboard.FocusedElement as UIElement;
      if (e.Key == Key.A)
      {
        if (element != null)
        {
          element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
          e.Handled = true;
        }
      }
      else if (e.Key == Key.D)
      {
        if (element != null)
        {
          element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
          e.Handled = true;
        }
      }
      else if (e.Key == Key.W)
      {
        if (element != null)
        {
          element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
          e.Handled = true;
        }
      }
      else if (e.Key == Key.S)
      {
        if (element != null)
        {
          element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
          e.Handled = true;
        }
      }
      else
      {
        return;
      }
    }

  • 相关阅读:
    蓝牙模块连接后出现ANR,日志记录
    移动基站问题
    从地址栏获取字符串
    jquery升级换代
    手机屏幕的触点
    屏幕翻转后要干什么
    条件判断后吸住底部的总结
    mouseenter 和 mouseleave
    自动垂直居中的js
    数学方法代替浮动解决自动换行排列
  • 原文地址:https://www.cnblogs.com/ht-beyond/p/5468575.html
Copyright © 2020-2023  润新知