[源码下载]
稳扎稳打Silverlight(32) - 2.0Tip/Trick之MessageBox,Popup,循环的几种实现方法,动态变换主题,本地化(多语言),响应鼠标双击事件
作者: webabcd
介绍
Silverlight 2.0 提示和技巧系列
MessageBox - MessageBox 的演示 Popup - Popup d窗口的演示 循环的几种实现方法 - dispatcherTimer 方式,Storyboard 方式,Timer 方式, CompositionTarget.Rendering 方式 动态变换主题 - 演示如何动态地变换主题 本地化(多语言) - 演示如何实现对多语言的支持 响应鼠标双击事件 - 响应并处理鼠标的双击事件
在线DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
示例
1、演示 MessageBox
MessageBoxDemo.xaml < UserControl x:Class ="Silverlight20.Tip.MessageBoxDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< GrID x:name ="LayoutRoot" Background ="White" >
< StackPanel >
< button x:name ="btnMessageBox" Content ="MessageBox 演示" Click ="btnMessageBox_Click" margin ="5" />
< TextBlock x:name ="lblResult" />
</ StackPanel >
</ GrID >
</ UserControl >
MessageBoxDemo.xaml.cs
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
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;
namespace Silverlight20.Tip
{
public partial class MessageBoxDemo : UserControl
{
public MessageBoxDemo()
@H_419_361@
{InitializeComponent();
}
private voID btnMessageBox_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult result = MessageBox.Show("信息", "标题", MessageBoxbutton.OKCancel);
lblResult.Text += result.ToString();
}
}
}
2、演示 Popup d出窗口
PopupDemo.xaml
< UserControl x:Class ="Silverlight20.Tip.PopupDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< GrID x:name ="LayoutRoot" Background ="White" >
< button x:name ="btnPopup" Content ="d出新窗口" margin ="5" WIDth ="80" Height ="40" Click ="btnPopup_Click" HorizontalAlignment ="left" VerticalAlignment ="top" />
</ GrID >
</ UserControl >
PopupDemo.xaml.cs
/**/ /*
* 如果需要 Silverlight 宿主可以使用 HTMLPage.PopupWindow() d出新窗口,则需要如下参数
* <param name="allowHTMLPopupWindow" value="true" />
* 此参数:同域时默认为 ture ; 跨域时默认为 false
*/
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
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.browser;
namespace Silverlight20.Tip
{
public partial class PopupDemo : UserControl
{
public PopupDemo()
{
InitializeComponent();
}
private voID btnPopup_Click(object sender, RoutedEventArgs e)
{
// HTMLPopupWindowOptions - 需要d出的新窗口的参数(如果浏览器是以标签的形式打开新窗口,则此参数无效)
HTMLPopupWindowOptions opt = new HTMLPopupWindowOptions();
opt.left = 0;
opt.top = 0;
opt.WIDth = 320;
opt.Height = 240;
// HTMLPage.IsPopupWindowAllowed - 指定 Silverlight 宿主是否可以使用 HTMLPage.PopupWindow() 来d出新的浏览器窗口
// HTMLPage.PopupWindow() - d出新窗口
if (true == HTMLPage.IsPopupWindowAllowed)
HTMLPage.PopupWindow(new Uri("http://webabcd.cnblogs.com/", UriKind.absolute), "newWindow", opt);
}
}
}
3、做循环程序的几种实现方法
LoopsDemo.xaml
< UserControl x:Class ="Silverlight20.Tip.LoopsDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< GrID x:name ="LayoutRoot" Background ="White" >
< StackPanel >
< StackPanel OrIEntation ="Horizontal" margin ="5" >
< TextBlock Text ="dispatcherTimer: " />
< TextBlock x:name ="resultdispatcherTimer" />
</ StackPanel >
< StackPanel OrIEntation ="Horizontal" margin ="5" >
< TextBlock Text ="Timer: " />
< TextBlock x:name ="resultTimer" />
</ StackPanel >
< StackPanel OrIEntation ="Horizontal" margin ="5" >
< TextBlock Text ="StoryBoard: " />
< TextBlock x:name ="resultStoryBoard" />
</ StackPanel >
< StackPanel OrIEntation ="Horizontal" margin ="5" >
< TextBlock Text ="CompositionTarget: " />
< TextBlock x:name ="resultCompositionTarget" />
</ StackPanel >
</ StackPanel >
</ GrID >
</ UserControl >
LoopsDemo.xaml.cs
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
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.Threading;
using System.Threading;
namespace Silverlight20.Tip
{
public partial class LoopsDemo : UserControl
{
public LoopsDemo()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(LoopsDemo_Loaded);
}
voID LoopsDemo_Loaded(object sender, RoutedEventArgs e)
{
dispatcherTimerDemo();
StoryboardDemo();
TimerDemo();
CompositionTargetDemo();
}
/**//// <summary>
/// dispatcherTimer - 在 UI 线程上循环(会受到 UI 线程的影响)
/// </summary>
private voID dispatcherTimerDemo()
{
dispatcherTimer dTimer = new dispatcherTimer();
dTimer.Interval = TimeSpan.Zero;
dTimer.Tick += new EventHandler(dTimer_Tick);
dTimer.Start();
}
voID dTimer_Tick(object sender, EventArgs e)
{
resultdispatcherTimer.Text = DateTime.Now.ToString("hh:mm:ss fff");
}
Storyboard _board;
/**//// <summary>
/// Storyboard - 在非 UI 线程上循环
/// </summary>
private voID StoryboardDemo()
{
_board = new Storyboard();
_board.Duration = TimeSpan.Zero;
_board.Completed += new EventHandler(_board_Completed);
_board.Begin();
}
voID _board_Completed(object sender, EventArgs e)
{
resultStoryBoard.Text = DateTime.Now.ToString("hh:mm:ss fff");
_board.Begin();
}
Timer _timer;
/**//// <summary>
/// Timer - 在非 UI 线程上循环
/// </summary>
private voID TimerDemo()
{
_timer = new Timer(_timer_CallBack, null, TimeSpan.Zero, TimeSpan.Zero);
}
private voID _timer_CallBack(object state)
{
this.dispatcher.BeginInvoke(() =>
@H_404_2237@{
resultTimer.Text = DateTime.Now.ToString("hh:mm:ss fff");
});
_timer.Change(TimeSpan.Zero, TimeSpan.Zero);
}
/**//// <summary>
/// CompositionTarget.Rendering - 每呈现 1 帧都会触发此事件(相当于 Flash 的 Event.ENTER_FRAME)
/// </summary>
private voID CompositionTargetDemo()
{
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
voID CompositionTarget_Rendering(object sender, EventArgs e)
{
resultCompositionTarget.Text = DateTime.Now.ToString("hh:mm:ss fff");
}
}
}
4、动态变换主题(以 Toolkit 中的主题为例,引用 System.windows.Controls.Theming.Toolkit.dll 和需要用到的相关主题文件)
themeDemo.xaml
< UserControl x:Class ="Silverlight20.Tip.themeDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< GrID x:name ="LayoutRoot" Background ="White" >
< button Content ="ExpressionDark 样式" WIDth ="120" Height ="40" VerticalAlignment ="top" HorizontalAlignment ="left" margin ="5" Click ="button_Click" ></ button >
</ GrID >
</ UserControl >
<!--
在 xaml 文件中声明的方式使用主题
<UserControl x:Class="Silverlight20.Tip.themeDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mytheme="clr-namespace:System.windows.Controls.Theming;assembly=System.windows.Controls.Theming.Toolkit">
<GrID x:name="LayoutRoot" Background="White"
mytheme:ImplicitStyleManager.ApplyMode="auto"
mytheme:ImplicitStyleManager.ResourceDictionaryUri="/Silverlight20;component/theme/System.windows.Controls.Theming.ExpressionDark.xaml"
>
<button Content="ExpressionDark 样式" WIDth="120" Height="40" VerticalAlignment="top" HorizontalAlignment="left" margin="5"></button>
</GrID>
</UserControl>
-->
themeDemo.xaml.cs
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
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.Controls.Theming;
namespace Silverlight20.Tip
{
public partial class themeDemo : UserControl
{
public themeDemo()
{
InitializeComponent();
}
private voID button_Click(object sender, RoutedEventArgs e)
{
// 设置主题的路径并将其赋值给需要使用该主题的控件
Uri uri = new Uri("/Silverlight20;component/theme/System.windows.Controls.Theming.ExpressionDark.xaml", UriKind.relative);
ImplicitStyleManager.SetResourceDictionaryUri(LayoutRoot, uri);
// 设置主题的应用模式后,将主题应用到指定的控件,此控件内的所用控件都将使用该主题
ImplicitStyleManager.SetApplyMode(LayoutRoot, ImplicitStylesApplyMode.auto);
ImplicitStyleManager.Apply(LayoutRoot);
}
}
}
5、演示如何实现本地化(多语言的支持)
LocalizationDemo.xaml
< UserControl x:Class ="Silverlight20.Tip.LocalizationDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res ="clr-namespace:Silverlight20.Resource" >
< StackPanel OrIEntation ="Vertical" >
< StackPanel margin ="5" >
< TextBlock Text ="姓名: " />
< TextBlock x:name ="lblname" />
< TextBlock Text ="年龄: " />
< TextBlock x:name ="lblAge" />
</ StackPanel >
<!-- 通过声明的方式调用指定的本地化资源 -->
< StackPanel.Resources >
< res:Localization x:name ="myRes" />
</ StackPanel.Resources >
< StackPanel margin ="5" >
< TextBlock Text ="姓名: " />
< TextBlock Text =" {Binding name, Source={StaticResource myRes}} " />
< TextBlock Text ="年龄: " />
< TextBlock Text =" {Binding Age, Source={StaticResource myRes}} " />
</ StackPanel >
</ StackPanel >
</ UserControl >
LocalizationDemo.xaml.cs
/**/ /*
* 配置本地化资源,如本例中需要添加 Localization.resx, Localization.zh-CN.resx 和 Localization.en-US.resx文件
* 要在资源文件中设置好相应的 name 和 Value
* 本例中,要把 Silverlight20.Resource.Localization 类及其构造函数的访问级别手动修改为 Public
* 打开项目文件,本例为 Silverlight20.csproj,对需要支持的本地化资源做配置,本例为 <SupportedCultures>zh-CN;en-US</SupportedCultures>
* 具体如何实现本地化,可以查 MSDN 中的 CultureInfo
* 如何指定需要调用的本地化资源:在 object 的 param 中指定;在 Application_Startup 中指定
*/
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
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;
namespace Silverlight20.Tip
{
public partial class LocalizationDemo : UserControl
{
public LocalizationDemo()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(LocalizationDemo_Loaded);
}
voID LocalizationDemo_Loaded(object sender, RoutedEventArgs e)
{
// 通过编程的方式调用指定的本地化资源
lblname.Text = Resource.Localization.name;
lblAge.Text = Resource.Localization.Age;
}
}
}
在 Application 中指定 Culture
private voID Application_Startup( object sender, StartupEventArgs e)
{
// 通过如下方法来实现本地化(指定资源)
CultureInfo culture = new CultureInfo("zh-CN");
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
this.RootVisual = new Page();
}
在 object 标记中指定 Culture
<!-- 演示如何在 Silverlight 中实现本地化 -->
<!-- 通过为 object 标记设置如下参数来实现本地化(指定资源) -->
< param name ="culture" value ="en-US" />
< param name ="uiculture" value ="en-Us" />
6、响应并处理鼠标的双击事件
DoubleClick.xaml
< UserControl x:Class ="Silverlight20.Tip.DoubleClick"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< GrID x:name ="LayoutRoot" Background ="White" >
< StackPanel >
< button x:name ="btn" Content ="Double Click Me" margin ="5" Click ="btn_Click" />
< TextBox x:name ="result" margin ="5" />
</ StackPanel >
</ GrID >
</ UserControl >
DoubleClick.xaml.cs
@H_466_4042@
@H_205_4045@
@H_767_4047@/**/ @H_238_4049@/** 根据 dispatcherTimer 是否为启动状态判断在某一时间段内是否按了两次鼠标左键
* 第一按鼠标左键则启动 dispatcherTimer,双击或者到了间隔时间则停止 dispatcherTimer
* 每次按键,如果 dispatcherTimer 为启动状态,即为双击
*/
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
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.Threading;
namespace Silverlight20.Tip
@H_468_4192@
@H_169_4195@
@H_545_4198@ {public partial class DoubleClick : UserControl
{
dispatcherTimer _dTimer;
public DoubleClick()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DoubleClick_Loaded);
}
voID DoubleClick_Loaded(object sender, RoutedEventArgs e)
{
_dTimer = new dispatcherTimer();
_dTimer.Interval = TimeSpan.FromMilliseconds(300);
_dTimer.Tick += new EventHandler(_dTimer_Tick);
}
private voID btn_Click(object sender, RoutedEventArgs e)
{
if (_dTimer.IsEnabled)
{
result.Text += "双击";
_dTimer.Stop();
}
else
{
_dTimer.Start();
}
}
voID _dTimer_Tick(object sender, EventArgs e)
{
_dTimer.Stop();
}
}
}
OK
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(32) - 2.0Tip/Trick之MessageBox, Popup, 循环的几种实现方法, 动态变换主题, 本地化(多语言), 响应鼠标双击事件全部内容,希望文章能够帮你解决稳扎稳打Silverlight(32) - 2.0Tip/Trick之MessageBox, Popup, 循环的几种实现方法, 动态变换主题, 本地化(多语言), 响应鼠标双击事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)