日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用观察者模式在 Silverlight 中切换用户控件

發(fā)布時間:2025/3/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用观察者模式在 Silverlight 中切换用户控件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

有一篇技巧,見

http://tech.sina.com.cn/s/2008-07-03/1528718607.shtml

http://kb.cnblogs.com/page/42897/?page=1

?

討論的是運用InitParams在Silverlight 2應用程序中切換用戶控件,這是個很笨但是直觀的解決方式。

但如果在控件中傳值,那將怎么辦?以上方法將毫無用途!

今天在一個老外的博客看到有個很巧妙的方法,不敢獨享,現(xiàn)分享出來:

首先寫個接口:


namespace PageSwitchSimple
{
? public interface ISwitchable
? {
??? void UtilizeState( object state );
? }
}

?

然后寫個切換類:

?

using System.Windows.Controls;

namespace PageSwitchSimple
{
? public static class Switcher
? {
??? public static PageSwitcher pageSwitcher;

??? public static void Switch( UserControl newPage )
??? {
????? pageSwitcher.Navigate( newPage );
??? }

??? public static void Switch( UserControl newPage, object state )
??? {
????? pageSwitcher.Navigate( newPage, state );
??? }
? }
}

?

PageSwitcher 成員類:

?

前臺代碼:

<UserControl x:Class="PageSwitchSimple.PageSwitcher"
??? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??? Width="800" Height="600">

</UserControl>

后臺代碼:

using System;
using System.Windows.Controls;

namespace PageSwitchSimple
{
? public partial class PageSwitcher : UserControl
? {
??? public PageSwitcher()
??? {
????? InitializeComponent();
??? }

??? public void Navigate( UserControl nextPage )
??? {
????? this.Content = nextPage;
??? }

??? public void Navigate( UserControl nextPage, object state )
??? {
????? this.Content = nextPage;
????? ISwitchable s = nextPage as ISwitchable;

??????//這里真是太巧妙了,用于傳object 參數(shù)!
????? if ( s != null )
????? {
??????? s.UtilizeState( state );
????? }
????? else
????? {
??????? throw new ArgumentException( "nextPage is not ISwitchable! "
????????? + nextPage.Name.ToString() );
????? }
??? }
? }
}


?

然后寫兩個實現(xiàn)接口ISwitchable的用戶控件:

?

控件一:

<UserControl x:Class="PageSwitchSimple.Page"
??? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??? Width="400" Height="300">
? <Grid x:Name="LayoutRoot" Background="White">
??? <TextBlock Text="Your Name: " FontSize="18" />
??? <TextBox x:Name="Name" FontSize="18" Width="150" Height="35" VerticalAlignment="Top" Margin="5"/>
??? <Button x:Name="ChangePage" Content="Change" FontSize="18" Width="100" Height="50" />
? </Grid>
</UserControl>


?

后臺代碼:

?

using System.Windows;
using System.Windows.Controls;

namespace PageSwitchSimple
{
? public partial class Page : UserControl, ISwitchable
? {
??? public Page()
??? {
????? InitializeComponent();
????? ChangePage.Click += new RoutedEventHandler( ChangePage_Click );
??? }

??? void ChangePage_Click( object sender, RoutedEventArgs e )
??? {
????? Switcher.Switch( new Page2(), Name.Text );
??? }
? }
}

?

控件二:

<UserControl x:Class="PageSwitchSimple.Page2"
??? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??? Width="400" Height="300">
? <Grid x:Name="LayoutRoot" Background="Bisque">
??? <TextBlock x:Name="Message" Text="Page2" FontSize="18" />
??? <Button x:Name="ChangePage" Content="Change" FontSize="18" Width="100" Height="50" />
? </Grid>
</UserControl>


?

后臺代碼:

?

using System.Windows;
using System.Windows.Controls;

namespace PageSwitchSimple
{
? public partial class Page2 : UserControl, ISwitchable
? {
??? public Page2()
??? {
????? InitializeComponent();
????? ChangePage.Click += new RoutedEventHandler( ChangePage_Click );
??? }

??? public void UtilizeState( object state )
??? {
????? Message.Text = state.ToString();
??? }

??? void ChangePage_Click( object sender, RoutedEventArgs e )
??? {
????? Switcher.Switch( new Page() );
??? }
? }
}

?

?

最后修改App.cs

??? private void Application_Startup( object sender, StartupEventArgs e )
??? {
????? PageSwitcher pageSwitcher = new PageSwitcher();
????? this.RootVisual = pageSwitcher;
????? Switcher.pageSwitcher = pageSwitcher;
????? Switcher.Switch( new Page() );
??? }

?

巧妙運用了觀察者模式,佩服作者的思路。

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/starcrm/archive/2009/07/10/1520714.html

總結

以上是生活随笔為你收集整理的使用观察者模式在 Silverlight 中切换用户控件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。