博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用观察者模式在 Silverlight 中切换用户控件
阅读量:4135 次
发布时间:2019-05-25

本文共 3342 字,大约阅读时间需要 11 分钟。

有一篇技巧,见

 

讨论的是运用InitParams在Silverlight 2应用程序中切换用户控件,这是个很笨但是直观的解决方式。

但如果在控件中传值,那将怎么办?以上方法将毫无用途!

今天在一个老外的博客看到有个很巧妙的方法,不敢独享,现分享出来:

首先写个接口:

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 参数!

      if ( s != null )
      {
        s.UtilizeState( state );
      }
      else
      {
        throw new ArgumentException( "nextPage is not ISwitchable! "
          + nextPage.Name.ToString() );
      }
    }
  }
}

 

然后写两个实现接口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() );
    }

 

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

 

 

 

转载地址:http://tvpvi.baihongyu.com/

你可能感兴趣的文章
C++默认参数
查看>>
正则表达式中有用但很少用的语法
查看>>
Vim 的 XML 文档编辑插件——xml.vim
查看>>
vim帮助文档无法跳转的问题
查看>>
vim常用快捷键及设置
查看>>
数据挖掘十大经典算法(6) PageRank
查看>>
数据挖掘十大经典算法(7) AdaBoost
查看>>
数据挖掘十大经典算法(9) 朴素贝叶斯分类器 Naive Bayes
查看>>
数据挖掘十大经典算法(10) CART: 分类与回归树
查看>>
初探数据挖掘中的十大经典算法
查看>>
android- ViewPager的跳转Fragment
查看>>
android-Banner控件的简单轮播图
查看>>
android-数据存入SP SP读取数据(简单案例)
查看>>
android-创建数据库存入数据,数据库数据的增删改查
查看>>
dccker-maven插件出现“Failed to execute goal com.spotifydocker-maven-plugin0.4.13....Permission den问题
查看>>
Java内存模型及其原理
查看>>
synchronized的原理及应用
查看>>
探索大数据基础设施容器化 | StartDT Tech Lab 04
查看>>
MFC——子窗口之间传递参数和调用函数
查看>>
Ubuntu18.04安装NVIDIA显卡驱动
查看>>