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>
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();
}
}
}
作者:gdgzboy@牛C网
地址:http://www.niuc.net/post/306/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
牛C网推荐您再看看以下日志:
常用的Javascript代码(一)
AS2与AS3比较测试
Javascript按比例重定图片大小
Javascript计算器代码
XHTML代码常见问题
Flash电子杂志常用滚动区域控制代码
JavaScript无刷新取数据代码
用Flex从前往后创建RIAs 第二部分
IIS错误代码
常用的Javascript代码(一)
AS2与AS3比较测试
Javascript按比例重定图片大小
Javascript计算器代码
XHTML代码常见问题
Flash电子杂志常用滚动区域控制代码
JavaScript无刷新取数据代码
用Flex从前往后创建RIAs 第二部分
IIS错误代码
Silverlight vs Flex
.NET开发Silverlight程序入门(一):项目结构







