[索引页]
[源码下载]
作者:webabcd
介绍
Silverlight 2.0 提示和技巧系列
在线DEMO
http://www.voidcn.com/article/p-ounmxjds-tq.html
示例
1、演示 MessageBox
MessageBoxDemo.xaml
[源码下载]
作者:webabcd
介绍
Silverlight 2.0 提示和技巧系列
- MessageBox - MessageBox 的演示
- Popup - Popup 弹窗口的演示
- 循环的几种实现方法 - dispatcherTimer 方式,Storyboard 方式,Timer 方式, CompositionTarget.Rendering 方式
- 动态变换主题 - 演示如何动态地变换主题
- 本地化(多语言) - 演示如何实现对多语言的支持
- 响应鼠标双击事件 - 响应并处理鼠标的双击事件
在线DEMO
http://www.voidcn.com/article/p-ounmxjds-tq.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>
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
2、演示 Popup 弹出窗口
PopupDemo.xaml
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>
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
3、做循环程序的几种实现方法
LoopsDemo.xaml
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>
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
4、动态变换主题(以 Toolkit 中的主题为例,引用 System.Windows.Controls.Theming.Toolkit.dll 和需要用到的相关主题文件)
ThemeDemo.xaml
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>
-->
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
5、演示如何实现本地化(多语言的支持)
LocalizationDemo.xaml
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>
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
在 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();
}
{
// 通过如下方法来实现本地化(指定资源)
CultureInfo culture = new CultureInfo("zh-CN");
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
this.RootVisual = new Page();
}
在 object 标记中指定 Culture
6、响应并处理鼠标的双击事件
DoubleClick.xaml
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>
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
OK
[源码下载]
[源码下载]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。