silverlight 页面切换

app.xmal.cs:

private void OnStartup(object sender, StartupEventArgs e)
  {
   // 此处加载主控件
            this.RootVisual = new PageSwitcher();
  }

 

page.xmal:

<UserControl
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="MultiPagePhase1.Page"
 Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White" >
  <StackPanel Height="Auto" HorizontalAlignment="Stretch" Margin="20,20,20,20" VerticalAlignment="Stretch" Width="Auto" Orientation="Horizontal">
   <TextBlock x:Name="Page1" FontFamily="Verdana" FontSize="36" Text="Page 1" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="150,0,0,0"/>
   <Button Height="40" Width="150" Content="Switch" x:Name="Switch" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="25,0,0,0" FontFamily="Verdana" FontSize="36"/>
  </StackPanel>
 </Grid> 
</UserControl>

page.xmal.cs:

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

namespace MultiPagePhase1
{
 public partial class Page : UserControl
 {
  public Page()
  {
   // Required to initialize variables
   InitializeComponent();
            Switch.Click += new RoutedEventHandler(Switch_Click);
  }
        // //當按下"我想去第二頁"
        void Switch_Click(object sender, RoutedEventArgs e)
        {
            //建立樹狀結構中的父物件
            PageSwitcher ps = this.Parent as PageSwitcher;
            //將UI置換成Page2
            ps.Navigate(new Page2());
           
        }
 }
}

page2.xmal:

<UserControl
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 x:Class="MultiPagePhase1.Page2"
 d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot" Background="White" >
  <StackPanel Height="Auto" HorizontalAlignment="Stretch" Margin="20,20,20,20" VerticalAlignment="Stretch" Width="Auto">
   <TextBlock x:Name="TextID" FontFamily="Comic Sans MS" FontSize="48" Foreground="#FFFF0000" Text="Page2" TextWrapping="Wrap"/>
   <Button Height="40" Width="150" Content="Switch!" x:Name="Switch" Background="#FF1790E5" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontFamily="Comic Sans MS" FontSize="18" RenderTransformOrigin="0.5,0.5" Margin="0,25,0,0">
    <Button.RenderTransform>
     <TransformGroup>
      <ScaleTransform ScaleY="1"/>
      <SkewTransform/>
      <RotateTransform/>
      <TranslateTransform/>
     </TransformGroup>
    </Button.RenderTransform>
   </Button>
  </StackPanel>
 </Grid>

 
</UserControl>

page2.xmal.cs:

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


namespace MultiPagePhase1
{
 public partial class Page2 : UserControl
 {
  public Page2()
  {
   // Required to initialize variables
   InitializeComponent();
            Switch.Click += new RoutedEventHandler(Switch_Click);
  }
        void Switch_Click(object sender, RoutedEventArgs e)
        {
            PageSwitcher ps = this.Parent as PageSwitcher;
            ps.Navigate(new Page());

        }
 }
}

PageSwitcher.xmal:

<UserControl
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 x:Class="MultiPagePhase1.PageSwitcher"
 d:DesignWidth="640" d:DesignHeight="480">
 
 
 </UserControl>

 

PageSwitche.xmal.cs:

using System.Windows.Controls;

namespace MultiPagePhase1
{
 public partial class PageSwitcher : UserControl
 {
  public PageSwitcher()
  {
   // Required to initialize variables
   InitializeComponent();
            if (this.Content == null)
            {
                this.Content = new Page();
                將一開始頁面指定為page
            }

            //以上为初始化
  }

        這裡是處理切換到下一頁的方法
        public void Navigate(UserControl nextPage)
        {
            this.Content = nextPage;
            //导航的意思
        }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值