-
Silverlight 2 DispatcherTimer和通过XAML创建UI元素
XAML标签元素在silverlight运行时被转换成相应的对象,通过XamlReader类的Load方法,动态创建UI元素:
- 指定一条XAML内容字符串,为按照XML规则运行,XamlReader.Load()现在需要你在你的XAML文件中指定一个xmlns;
- 通过XamlReader.Load方法把元素在内存中编译(这样就可以得到UI元素对象的引用,也有可能是null,或者报错);
- 最后把它添加到容器的子控件中。
下面我们来制作一个简单的时钟,Page.xaml如下:
<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如下:
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();
}
}
}
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();
}
}
}
运行的结果如下:
一个简单的电子钟做好了。主要是学习两项内容:通过XamlReader类的Load方法,动态创建UI元素和DispatcherTimer。
出处:https://www.cnblogs.com/shanyou/archive/2008/03/11/SilverlightClock.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数