稳扎稳打Silverlight(32) - 2.0Tip/Trick之MessageBox, Popup, 循环的几种实现方法, 动态变换主题...

[索引页]
[源码下载]


稳扎稳打Silverlight(32) - 2.0Tip/Trick之MessageBox, Popup, 循环的几种实现方法, 动态变换主题, 本地化(多语言), 响应鼠标双击事件


作者: webabcd


介绍
Silverlight 2.0 提示和技巧系列
  • MessageBox - MessageBox 的演示 
  • Popup - Popup 弹窗口的演示 
  • 循环的几种实现方法 - DispatcherTimer 方式, Storyboard 方式, Timer 方式,  CompositionTarget.Rendering 方式
  • 动态变换主题 - 演示如何动态地变换主题 
  • 本地化(多语言) - 演示如何实现对多语言的支持
  • 响应鼠标双击事件 - 响应并处理鼠标的双击事件


在线DEMO
http://webabcd.blog.51cto.com/1787395/342779


示例
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
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class MessageBoxDemo : UserControl 
InBlock.gif        { 
InBlock.gif                 public MessageBoxDemo() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnMessageBox_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        MessageBoxResult result = MessageBox.Show( "信息""标题", MessageBoxButton.OKCancel); 
InBlock.gif 
InBlock.gif                        lblResult.Text += result.ToString(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
2、演示 Popup 弹出窗口
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="弹出新窗口" Margin="5" Width="80" Height="40" Click="btnPopup_Click" HorizontalAlignment="Left" VerticalAlignment="Top" /> 
        </Grid> 
</UserControl>
 
PopupDemo.xaml.cs
InBlock.gif /* 
InBlock.gif * 如果需要 Silverlight 宿主可以使用 HtmlPage.PopupWindow() 弹出新窗口,则需要如下参数 
InBlock.gif * <param name="allowHtmlPopupWindow" value="true" /> 
InBlock.gif * 此参数:同域时默认为 ture ; 跨域时默认为 false 
InBlock.gif */
 
InBlock.gif 
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Windows.Browser; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class PopupDemo : UserControl 
InBlock.gif        { 
InBlock.gif                 public PopupDemo() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnPopup_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // HtmlPopupWindowOptions - 需要弹出的新窗口的参数(如果浏览器是以标签的形式打开新窗口,则此参数无效) 
InBlock.gif                        HtmlPopupWindowOptions opt =  new HtmlPopupWindowOptions(); 
InBlock.gif                        opt.Left = 0; 
InBlock.gif                        opt.Top = 0; 
InBlock.gif                        opt.Width = 320; 
InBlock.gif                        opt.Height = 240; 
InBlock.gif 
InBlock.gif                         // HtmlPage.IsPopupWindowAllowed - 指定 Silverlight 宿主是否可以使用 HtmlPage.PopupWindow() 来弹出新的浏览器窗口 
InBlock.gif                         // HtmlPage.PopupWindow() - 弹出新窗口 
InBlock.gif                         if ( true == HtmlPage.IsPopupWindowAllowed)    
InBlock.gif                                HtmlPage.PopupWindow( new Uri( "http://webabcd.cnblogs.com/", UriKind.Absolute), "newWindow", opt); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
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
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Windows.Threading; 
InBlock.gif using System.Threading; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class LoopsDemo : UserControl 
InBlock.gif        { 
InBlock.gif                 public LoopsDemo() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(LoopsDemo_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void LoopsDemo_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        DispatcherTimerDemo(); 
InBlock.gif                        StoryboardDemo(); 
InBlock.gif                        TimerDemo(); 
InBlock.gif                        CompositionTargetDemo(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// DispatcherTimer - 在 UI 线程上循环(会受到 UI 线程的影响) 
InBlock.gif                 /// </summary> 
InBlock.gif                 private  void DispatcherTimerDemo() 
InBlock.gif                { 
InBlock.gif                        DispatcherTimer dTimer =  new DispatcherTimer(); 
InBlock.gif                        dTimer.Interval = TimeSpan.Zero; 
InBlock.gif                        dTimer.Tick +=  new EventHandler(dTimer_Tick); 
InBlock.gif                        dTimer.Start(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void dTimer_Tick( object sender, EventArgs e) 
InBlock.gif                { 
InBlock.gif                        resultDispatcherTimer.Text = DateTime.Now.ToString( "hh:mm:ss fff"); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif 
InBlock.gif                Storyboard _board; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Storyboard - 在非 UI 线程上循环 
InBlock.gif                 /// </summary> 
InBlock.gif                 private  void StoryboardDemo() 
InBlock.gif                { 
InBlock.gif                        _board =  new Storyboard(); 
InBlock.gif                        _board.Duration = TimeSpan.Zero; 
InBlock.gif                        _board.Completed +=  new EventHandler(_board_Completed); 
InBlock.gif                        _board.Begin(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void _board_Completed( object sender, EventArgs e) 
InBlock.gif                { 
InBlock.gif                        resultStoryBoard.Text = DateTime.Now.ToString( "hh:mm:ss fff"); 
InBlock.gif                        _board.Begin(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif 
InBlock.gif                Timer _timer; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Timer - 在非 UI 线程上循环 
InBlock.gif                 /// </summary> 
InBlock.gif                 private  void TimerDemo() 
InBlock.gif                { 
InBlock.gif                        _timer =  new Timer(_timer_CallBack,  null, TimeSpan.Zero, TimeSpan.Zero); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void _timer_CallBack( object state) 
InBlock.gif                { 
InBlock.gif                         this.Dispatcher.BeginInvoke(() => 
InBlock.gif                        { 
InBlock.gif                                resultTimer.Text = DateTime.Now.ToString( "hh:mm:ss fff"); 
InBlock.gif                        }); 
InBlock.gif                        _timer.Change(TimeSpan.Zero, TimeSpan.Zero); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// CompositionTarget.Rendering - 每呈现 1 帧都会触发此事件(相当于 Flash 的 Event.ENTER_FRAME) 
InBlock.gif                 /// </summary> 
InBlock.gif                 private  void CompositionTargetDemo() 
InBlock.gif                { 
InBlock.gif                        CompositionTarget.Rendering +=  new EventHandler(CompositionTarget_Rendering); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void CompositionTarget_Rendering( object sender, EventArgs e) 
InBlock.gif                { 
InBlock.gif                        resultCompositionTarget.Text = DateTime.Now.ToString( "hh:mm:ss fff"); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
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
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Windows.Controls.Theming; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class ThemeDemo : UserControl 
InBlock.gif        { 
InBlock.gif                 public ThemeDemo() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void Button_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // 设置主题的路径并将其赋值给需要使用该主题的控件 
InBlock.gif                        Uri uri =  new Uri( "/Silverlight20;component/Theme/System.Windows.Controls.Theming.ExpressionDark.xaml", UriKind.Relative); 
InBlock.gif                        ImplicitStyleManager.SetResourceDictionaryUri(LayoutRoot, uri); 
InBlock.gif 
InBlock.gif                         // 设置主题的应用模式后,将主题应用到指定的控件,此控件内的所用控件都将使用该主题 
InBlock.gif                        ImplicitStyleManager.SetApplyMode(LayoutRoot, ImplicitStylesApplyMode.Auto); 
InBlock.gif                        ImplicitStyleManager.Apply(LayoutRoot); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
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
InBlock.gif /* 
InBlock.gif * 配置本地化资源,如本例中需要添加 Localization.resx, Localization.zh-CN.resx 和 Localization.en-US.resx文件 
InBlock.gif * 要在资源文件中设置好相应的 Name 和 Value 
InBlock.gif * 本例中,要把 Silverlight20.Resource.Localization 类及其构造函数的访问级别手动修改为 Public    
InBlock.gif * 打开项目文件,本例为 Silverlight20.csproj,对需要支持的本地化资源做配置,本例为 <SupportedCultures>zh-CN;en-US</SupportedCultures> 
InBlock.gif * 具体如何实现本地化,可以查 MSDN 中的 CultureInfo 
InBlock.gif * 如何指定需要调用的本地化资源:在 object 的 param 中指定;在 Application_Startup 中指定 
InBlock.gif */
 
InBlock.gif 
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class LocalizationDemo : UserControl 
InBlock.gif        { 
InBlock.gif                 public LocalizationDemo() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(LocalizationDemo_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void LocalizationDemo_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // 通过编程的方式调用指定的本地化资源 
InBlock.gif                        lblName.Text = Resource.Localization.Name; 
InBlock.gif                        lblAge.Text = Resource.Localization.Age; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
在 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
InBlock.gif /* 
InBlock.gif * 根据 DispatcherTimer 是否为启动状态判断在某一时间段内是否按了两次鼠标左键 
InBlock.gif *         第一按鼠标左键则启动 DispatcherTimer,双击或者到了间隔时间则停止 DispatcherTimer 
InBlock.gif *         每次按键,如果 DispatcherTimer 为启动状态,即为双击 
InBlock.gif */
 
InBlock.gif 
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Windows.Threading; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class DoubleClick : UserControl 
InBlock.gif        { 
InBlock.gif                DispatcherTimer _dTimer; 
InBlock.gif 
InBlock.gif                 public DoubleClick() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(DoubleClick_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void DoubleClick_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        _dTimer =  new DispatcherTimer(); 
InBlock.gif                        _dTimer.Interval = TimeSpan.FromMilliseconds(300); 
InBlock.gif                        _dTimer.Tick +=  new EventHandler(_dTimer_Tick); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btn_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         if (_dTimer.IsEnabled) 
InBlock.gif                        { 
InBlock.gif                                result.Text +=  "双击"
InBlock.gif                                _dTimer.Stop(); 
InBlock.gif                        } 
InBlock.gif                         else 
InBlock.gif                        { 
InBlock.gif                                _dTimer.Start(); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void _dTimer_Tick( object sender, EventArgs e) 
InBlock.gif                { 
InBlock.gif                        _dTimer.Stop(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
 

     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/343978 ,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值