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;
//导航的意思
}
}
}