前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Silverlight 2 DispatcherTimer和通过XAML创建UI元素

Silverlight 2 DispatcherTimer和通过XAML创建UI元素

作者头像
张善友
发布2018-01-19 15:38:41
8260
发布2018-01-19 15:38:41
举报
文章被收录于专栏:张善友的专栏

XAML标签元素在silverlight运行时被转换成相应的对象,通过XamlReader类的Load方法,动态创建UI元素:

  1. 指定一条XAML内容字符串,为按照XML规则运行,XamlReader.Load()现在需要你在你的XAML文件中指定一个xmlns;
  2. 通过XamlReader.Load方法把元素在内存中编译(这样就可以得到UI元素对象的引用,也有可能是null,或者报错);
  3. 最后把它添加到容器的子控件中。

下面我们来制作一个简单的时钟,Page.xaml如下:

代码语言:js
复制
<UserControl x:Class="OpenXmlVideo2.Page"
     xmlns="http://schemas.microsoft.com/client/2007"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Width="187" Height="97">
     <Canvas x:Name="EClock" Height="97" Width="187" >
     </Canvas>
 </UserControl> 

Page.xaml.cs如下:

代码语言:javascript
复制
using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Markup; 



namespace OpenXmlVideo2

{

    public partial class Page : UserControl

    {

        private TextBlock textBlock1;

        private System.Windows.Threading.DispatcherTimer timer; 



        public Page()

        {

            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Page_Loaded); 



        } 



        void Page_Loaded(object sender, RoutedEventArgs e)

        {

            string xaml = string.Empty;

            xaml = "<TextBlock xmlns=\"http://schemas.microsoft.com/client/2007\" Margin=\"14,11,19,12\" Name=\"textBlock1\" FontFamily=\"Time New Roman\" FontSize=\"40\">00:00:00</TextBlock>";

            textBlock1 = XamlReader.Load(xaml) as TextBlock;



           //Loaded就是TextBlock的加载事件,那么里面的textBlock1_Loaded自然就是事件处理程序的名称。

            textBlock1.Loaded += new RoutedEventHandler(textBlock1_Loaded);



           //改变附加属性(attached properties),必须使用SetValue方法

            textBlock1.SetValue(Canvas.LeftProperty, 2);

            textBlock1.SetValue(Canvas.TopProperty, 2);



            //加把textBlock1对象做为子对象添加到画布(和asp.net页的控件树的道理相拟)



            this.EClock.Children.Add(textBlock1);



        } 



        void textBlock1_Loaded(object sender, RoutedEventArgs e)

        {



            //使用了DispatcherTimer,我把间隔设置为1秒。该计时器的间隔事件也是Tick事件

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

            timer.Interval = new TimeSpan(0, 0, 1);   //间隔1秒

            timer.Tick += new EventHandler(timer_Tick);

            timer.Start();

        } 



        void timer_Tick(object sender, EventArgs e)

        {

            textBlock1.Text = DateTime.Now.ToLongTimeString();

        }

    }

}

运行的结果如下:

image
image

一个简单的电子钟做好了。主要是学习两项内容:通过XamlReader类的Load方法,动态创建UI元素和DispatcherTimer。

参考资料:

Silverlight 2的变化(Breaking Changes in Silverlight 2):

http://www.cnblogs.com/worksguo/archive/2008/03/07/1094347.html

Silverlight2.0中的计时器类DispatcherTimer——不再使用Storyboard计时器

http://www.cnblogs.com/gowhere/archive/2008/03/11/silverlight2_dispatchertimer.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2008-03-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档