稳扎稳打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.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>
 
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();

                }

        }

}
 
 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的IsEnabled=“{BindingABC}”,但这不起作用–MenuItem总是启用.提前感谢您的线索!干杯编辑:有趣的是,当设置Mode=TwoWay时,绑定似乎有效.但是,在菜单项上移动鼠标后,将更新上下文菜单的外观.这是异步工作吗?右键
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,因为我确信即使是一个c#人也可以使用类似的.net实现来回答这个问题).我申请的简短说明:我的桌面应用程序将仅在win平台上运行,使用vb.net,它是一个简单的网吧管理软件,在服务器上运行服务器gui,在工作站上运行客户端gui,
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如果它在UIThread上,如何将文件请求和ProcessFile()移动到单独的线程?varxClient=newServiceReference1.Service1SoapClient();xClient.Retrieve_File_Completed+=newEventHandler<ServiceReference1.Retrieve_Fi
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不在办公室使用sharepoint.所以不知道如何开始.这些是我的新手问题>学习共享点值得努力吗?>学习sharepoint的资源在哪里?>我是否应该考虑开发哪些参考项目?感谢您的意见.解决方法:SharePoint以如此积极的方式改变了我的职业
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:Thecollectiondatacontracttype‘System.Collections.Generic.Listcannotbedeserializedbecauseitdoesnothaveapublicparameterlessconstructor.Addingapublicparameterlessconstructorwillf
我需要根据Silverlight中的某些配置值设置给定控件的Style.我想有可能从两种可用的样式(静态资源)中选择一个控件样式.我试图做一些像:<TextBoxStyle="{BindingABC}"/>哪里publicstringABC{get{return"{StaticResourceMyStyle}";}}不幸的是,这不起作用.你有
我刚买了第一台Android设备,我喜欢它…我也很喜欢你可以创建自己的应用程序并随意分发它们.我已经阅读了一些关于Monodroid的内容,而且显然微软希望将Silverlight放在这些设备上,尽管没有太多关于它们的信息……但是Moonlight呢?如果Monodroid就像Mono……为什么我们需要它呢?相反
我们的ASP.NET网站允许用户执行各种查询,并根据从数据库查询的结果显示网络图(如UML图).目前,我们正在生成一个位图并显示它.但由于我们需要支持允许用户以交互方式显示/隐藏某些块的功能,因此我们计划使用Silverlight来渲染图形.我们还计划在未来添加更多互动.我有两个问题:>ASP
我正在开发一个Silverlight4应用程序,我已经创建了一个自定义的启动画面.乍一看,自定义启动画面运行良好–非常好.几天后,我开始注意到闪屏不再显示,屏幕仍然是空白.这似乎只发生在我打开多个指向同一个应用程序的IE选项卡/窗口时.前几个将加载正常,而以下选项卡/窗口将保持“白
这是我的XAML:<ImageVerticalAlignment="Center"HorizontalAlignment="Center"Source="{BindingInput,Converter={StaticResourceByteArrayToBitmapConverter}}"><Image.Rend
问候,我有一个ItemsControl,我更改了模板,为绑定的ItemsSource中的每个对象显示一个RadioButton.但是ItemsSource可以为空,当它为空时我想显示默认值.像“绑定列表中没有可供您选择的项目”……我想到的一种方法是将ItemsControl.Visibility设置为Collapsed,并将TextBlock.Vsibi
堆栈溢出的第一个问题……我是C#的新手,但在学习它时却非常直接.几分钟前我才看到这个tutorial.通过各种c#技术阅读WCF,WPF,Silverlight,c#和asp.net,这是很多技术都可以用c#来实现.我将创建一个Web应用程序c#.我认为SilverLight似乎是我最好的选择.该应用程序将拥有一个数
我正在使用MVVM(Model-View-ViewModel)模式编写应用程序,并利用MicrosoftP&P团队的Prism和Unity位.我有一个包含项目列表的视图.这些项包含在ViewModel中的ObservableCollection中,View中的列表框是数据绑定的(ViewModel设置为View的DataContext).在ViewModel中,我有一个运行的
我有一个应用程序,它在首次运行时显示免责声明页面.选择“接受”或“拒绝”后,您再也看不到该页面了.但是,当您在第一次运行后按后退键尝试关闭应用程序时,您将返回免责声明页面,然后再次点击该页面,返回主页面,然后再次退出.这仅在应用程序第一次运行时发生,但我希望应用程序在
我正在尝试在SilverlightforWindowsPhone中使用异步HttpWebRequest.一切都很完美,直到我到达我应该打电话的地方privatestaticManualResetEventallDone=newManualResetEvent(false);...request.BeginGetResponse(newAsyncCallback(GetResponseCallback),request);a
嗨,我有两个Writablebitmap,一个来自jpg,另一个来自png,并使用此方法在循环中混合颜色:privatestaticColorMix(Colorfrom,Colorto,floatpercent){floatamountFrom=1.0f-percent;returnColor.FromArgb((byte)(from.A*amountFrom+to.A*perc
我需要开发一个程序,它包含一个图像(png),中心有一个洞.在这个图像下将有一个框架,我想点击图像的透明孔我可以点击框架内的按钮.我不知道是否有一种方法可以通过图像或其他方式传播点击.谢谢你的帮助解决方法:你在图像上将IsHitTestVisible设置为false,然后点击浏览.
我正在研究一个silverlight应用程序,我发现List没有Find扩展方法说,List<Something>list=newList<Something>(something);list.Remove(list.Find(e=>e.id==10));没有查找扩展方法我错过了什么?解决方法:它不包括在内以减小运行时的大小.建议您使用LINQ扩展,例如First
我试图弄清楚如何设置Path元素的Data属性来获得此类型的软角:alttexthttp://i42.tinypic.com/1rzu6w.jpg现在我只有这样的尖角:alttexthttp://i42.tinypic.com/2eeleah.jpg我尝试用椭圆玩,但我无法得到我想要的东西.谢谢最佳答案:路径的段具有IsSmoothJoin属性,默认为false.
问题我有一个在远程服务器上运行的restfulWeb服务.我已经制作了一个使用它的WP7应用程序,所以我知道它有效.我正在将应用程序移植到SilverlightWeb应用程序并遇到问题.我已经包含了代码的简化版本以及引发的错误.EndGetResponse方法抛出错误.随意询问更多信息.我一直在寻找解