Silverlight窗體間傳值

轉載來自銀光中國;

 

使用觀察者模式在 Silverlight 中切換用戶控件

時間:2010-10-18 08:06來源:博客園 作者:jasmin 點擊: 118次
有一篇技巧,見 http://tech.sina.com.cn/s/2008-07-03/1528718607.shtml 或 http://kb.cnblogs.com/page/42897/?page=1 討論的是運用InitParams在 Silverlight 2應用程序中切換用戶控件 ,這是個很笨但是直觀的解決方式。 但如果在控件中傳值,那將怎麼辦?以上方法將毫無用途! 今天在一個老外的博客看到有個很巧妙的方法
  

有一篇技巧,見

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

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

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

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

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

首先寫個接口:

1
2
3
4
5
6
7
namespace PageSwitchSimple 
{ 
  public interface ISwitchable 
  { 
    void UtilizeState( object state ); 
  } 
}

然後寫個切換類:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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 成員類:

前臺代碼:

1
2
3
4
5
6
<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>

後臺代碼:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 參數! 
      if ( s != null ) 
      { 
        s.UtilizeState( state ); 
      } 
      else 
      { 
        throw new ArgumentException( "nextPage is not ISwitchable! " 
          + nextPage.Name.ToString() ); 
      } 
    } 
  } 
}

然後寫兩個實現接口ISwitchable的用戶控件:

控件一:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<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>

後臺代碼:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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 ); 
    } 
  } 
}

控件二:

1
2
3
4
5
6
7
8
9
<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>

後臺代碼:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

1
2
3
4
5
6
7
private void Application_Startup( object sender, StartupEventArgs e ) 
    { 
      PageSwitcher pageSwitcher = new PageSwitcher(); 
      this.RootVisual = pageSwitcher; 
      Switcher.pageSwitcher = pageSwitcher; 
      Switcher.Switch( new Page() ); 
    }

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

本文來自jasmin的博客,原文地址:http://www.cnblogs.com/jasmine_xm/archive/2010/10/18/1854401.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章