随笔 - 616, 文章 - 0, 评论 - 10492
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
[索引页]
[源码下载]


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


作者:webabcd


介绍
Silverlight 2.0 提示和技巧系列
  • MessageBox - MessageBox 的演示 
  • Popup - Popup 弹窗口的演示 
  • 循环的几种实现方法 - 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()
        
{
            InitializeComponent();
        }


        
private void btnMessageBox_Click(object sender, RoutedEventArgs e)
        
{
            MessageBoxResult result 
= MessageBox.Show("信息""标题", MessageBoxButton.OKCancel);

            lblResult.Text 
+= result.ToString();
        }

    }

}



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
/*
 * 如果需要 Silverlight 宿主可以使用 HtmlPage.PopupWindow() 弹出新窗口,则需要如下参数
 * <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 - 需要弹出的新窗口的参数(如果浏览器是以标签的形式打开新窗口,则此参数无效)
            HtmlPopupWindowOptions opt = new HtmlPopupWindowOptions();
            opt.Left 
= 0;
            opt.Top 
= 0;
            opt.Width 
= 320;
            opt.Height 
= 240;

            
// HtmlPage.IsPopupWindowAllowed - 指定 Silverlight 宿主是否可以使用 HtmlPage.PopupWindow() 来弹出新的浏览器窗口
            
// HtmlPage.PopupWindow() - 弹出新窗口
            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(() =>
            
{
                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
/*
 * 根据 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
{
    
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
[源码下载]