silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 讨论的是运用In

转载来自银光中国;

 

使用观察者模式在 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 成员类:

前台@L_673_17@:

1
2
3
4
5
6
<UserControl x:Class="PageSwitchSimple.PageSwitcher" 
    xmlns="http://scheR_582_11845@as.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://scheR_582_11845@as.microsoft.com/winfx/2006/xaml" 
    Width="800" Height="600">

</UserControl>

后台@L_673_17@:

 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://scheR_582_11845@as.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://scheR_582_11845@as.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" @H_533_212@margin="5"/> 
    <Button x:Name="ChangePage" Content="Change" FontSize="18" Width="100" Height="50" /> 
  </Grid> 
</UserControl>

后台@L_673_17@:

 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://scheR_582_11845@as.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://scheR_582_11845@as.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>

后台@L_673_17@:

 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

大佬总结

以上是大佬教程为你收集整理的Silverlight窗体间传值全部内容,希望文章能够帮你解决Silverlight窗体间传值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。