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

简介:
XAML标签元素在silverlight运行时被转换成相应的对象,通过XamlReader类的Load方法,动态创建UI元素:
  1. 指定一条XAML内容字符串,为按照XML规则运行,XamlReader.Load()现在需要你在你的XAML文件中指定一个xmlns;
  2. 通过XamlReader.Load方法把元素在内存中编译(这样就可以得到UI元素对象的引用,也有可能是null,或者报错);
  3. 最后把它添加到容器的子控件中。
下面我们来制作一个简单的时钟,Page.xaml如下:
<UserControl x:Class="OpenXmlVideo2.Page"
    xmlns=" [url]http://schemas.microsoft.com/client/2007[/url]"
    xmlns:x=" [url]http://schemas.microsoft.com/winfx/2006/xaml[/url]"
    Width="187" Height="97">
    <Canvas x:Name="EClock" Height="97" Width="187" >
    </Canvas>
</UserControl>
Page.xaml.cs如下:
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(001);   //间隔1秒
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }
 

        
void timer_Tick(object sender, EventArgs e)
        
{
            textBlock1.Text 
= DateTime.Now.ToLongTimeString();
        }

    }

}

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





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/73546,如需转载请自行联系原作者
目录
相关文章
|
8月前
|
Web App开发 前端开发 JavaScript
SAP UI5 SimpleForm 里在水平方向显示多组 Form 元素的实现方法试读版
SAP UI5 SimpleForm 里在水平方向显示多组 Form 元素的实现方法试读版
36 0
|
8月前
|
XML 数据格式
SAP UI5 应用 manifest.json 文件里 Routes 数组元素的相对顺序,不可忽视的试读版
SAP UI5 应用 manifest.json 文件里 Routes 数组元素的相对顺序,不可忽视的试读版
49 0
|
10月前
|
前端开发 JavaScript 中间件
关于浮动元素,你还在自己计算位置吗?来看看 Floating UI 吧!
关于浮动元素,你还在自己计算位置吗?来看看 Floating UI 吧!
147 0
|
Web App开发 开发者 iOS开发
SAP UI5 SimpleForm 里在水平方向显示多组 Form 元素的实现方法试读版
SAP UI5 SimpleForm 里在水平方向显示多组 Form 元素的实现方法试读版
|
开发工具 Android开发 iOS开发
如何使用 Draggable 和 DragTarget 在 Flutter 中创建拖放 UI 元素?
如何使用 Draggable 和 DragTarget 在 Flutter 中创建拖放 UI 元素?
365 0
|
测试技术
在jasmine beforeEach里修改UI元素的一个side effect
在jasmine beforeEach里修改UI元素的一个side effect
89 0
在jasmine beforeEach里修改UI元素的一个side effect
|
测试技术
Angular单元测试里使用fixture.debugElement测试UI界面元素
Angular单元测试里使用fixture.debugElement测试UI界面元素
126 0
Angular单元测试里使用fixture.debugElement测试UI界面元素
如何通过调试的方式搞清楚Angular createEmbeddedView具体创建的UI元素是什么
如何通过调试的方式搞清楚Angular createEmbeddedView具体创建的UI元素是什么
105 0
如何通过调试的方式搞清楚Angular createEmbeddedView具体创建的UI元素是什么
SAP Cloud for Customer对UI界面元素文本的调整原理
CRM WebUI的configuration mode下可以修改button的label,但是menu item的文字能不能修改我就忘了。 C4C客户有个需求: 把new button改成Add button,把menu item Add改成From Contact.
SAP Cloud for Customer对UI界面元素文本的调整原理
SAP Fiori smart template技术里CDS view的注解和UI元素对应关系
SAP Fiori smart template技术里CDS view的注解和UI元素对应关系
203 0
SAP Fiori smart template技术里CDS view的注解和UI元素对应关系

热门文章

最新文章