穩紮穩打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://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
[源碼下載]
發佈了6 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章