Silverlight 2 DispatcherTimer和通过XAML创建UI元素
生活随笔
收集整理的這篇文章主要介紹了
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="[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(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。 ?著作權歸作者所有:來自51CTO博客作者張善友的原創作品,如需轉載,請注明出處,否則將追究法律責任 Silverlight Silverlight2 DispatcherTimer SilverLight
??? 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(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。 ?著作權歸作者所有:來自51CTO博客作者張善友的原創作品,如需轉載,請注明出處,否則將追究法律責任 Silverlight Silverlight2 DispatcherTimer SilverLight
0
微博 QQ 微信收藏
上一篇:Mono運行于IPhone之上 下一篇:Silverlight 2 和 ... 張善友502篇文章,102W+人氣,0粉絲
Ctrl+Enter?發布
發布
取消
轉載于:https://blog.51cto.com/shanyou/73546
總結
以上是生活随笔為你收集整理的Silverlight 2 DispatcherTimer和通过XAML创建UI元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一步一步学Silverlight 2系列
- 下一篇: 写段QTP脚本与大家一起分享