一.xaml于xaml之间的跳转
如a.xaml和b.xaml之间的跳转
在a页面的后台代码中写语句
this.Content = new b();就可以实现跳转
二.普通页面之间的跳转(html页或aspx页)
HtmlWindow html = HtmlPage.Window;
//html.Navigate(new Uri("http://www.Silverlightchina.net"));//普通网址
// html.Navigate(new Uri(item.Link_Page.Trim(), UriKind.Relative),"_blank");
html.Navigate(new Uri("../Index.aspx", UriKind.Relative));//相对路径
三.定义全局变量
1.我们先在App中定义一个Grid控件;代码如下所示;
Grid rootGrid = new Grid();
2.当整个应用程序启动运行时,让它第一个页面Login页面;在Application Startup事件中来处理解决,先看一下代码:
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = rootGrid;①
this.rootGrid.Children.Add(new Login());②
}
这句代码是什么意思呢?分析如下:
1.当整个应用程序启动时,程序的主界面加载就是Grid;
2.Grid控件加载完成了,那么它的创建也完成,接下我们就可以为它添加一个子页面;
3.登陆时的主页面问题就完全解决了,但是登陆成功后我们又怎么来转向成功后的页面呢;我们还是在App.cs里面写一个方法来解决,
先看代码;
/// <summary>
/// 页面跳转
/// summary>
/// <param name="usercontrol">param>
public void RedirectTo(UserControl usercontrol)
{
App app = (App)Application.Current;①
app.rootGrid.Children.Clear();②
app.rootGrid.Children.Add(usercontrol);③
}
这三句代码是什么意思呢?分析结果如下
①.它的意思就是得到当应用程序的对象
②.刚才我们不是对Grid里面添加Login页面进去了,现在为了添加成功页面进去,我们必须把Grid控件中的Login清除掉;
③.这上操作是建立在②的基础之上的,没有它的Clear就不能添加;所以这一步就是在添加新页面;
我们怎么来调用③这个方法呢?不难看出③这个方面的参数类型是UserControl,是一个控件;在调用它时我们传一个就是一个页面;在以后的Xaml页面的跳转Button响应事件中,直接通过以下方式处理(可只写第三行)
App app = (App)Application.Current;
app.curUserName = txtUserName.Text.Trim();
app.RedirectTo(new MainPage());
四.窗体之间跳转(子窗体跳转的时候很好用)
ChildWindow1 childwindow = new ChildWindow1();
childwindow.Show();
(2)
silverlight 中如何可以在两xaml中实现跳转
.先在silverlight项目中新建一个接口文件IContent.cs,内容如下(namespace请各位根据自己的实际情况修改):
--------------------------------------------------
using System.Windows;
namespace BookStore
{
public interface IContent
{
UIElement Content { get; set; }
}
}
步骤2.建二个Xaml文件Test.xaml和Test2.Xaml
Test.Xaml完整内容如下:
---------------------------------------------------------------------
<UserControl x:Class="BookStore.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="600" Height="400">
<Grid x:Name="LayoutRoot" Background="White" >
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="50" Background="AliceBlue" Width="200" Height="100">
<TextBlock TextAlignment="Center">
这是Test.Xaml文件
</TextBlock>
<Button Height="25" Width="150" Content="转到Test2.xaml" Click="Button_Click"></Button>
</StackPanel>
</Grid>
</UserControl>
Test.Xaml.Cs完整内容如下:
--------------------------------------------------------------------
using System.Windows;
using System.Windows.Controls;
namespace BookStore
{
//手动增加, IContent ,让Test实现IContent接口
public partial class Test : UserControl, IContent
{
public Test()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//实现切换(点击test.xaml上的按钮将切换到Test2"场景")
(Application.Current.RootVisual as IContent).Content = new Test2();
}
/// <summary>
/// 增加一个Content属性
/// </summary>
public new UIElement Content
{
get
{
return base.Content;
}
set
{
base.Content = value;
}
}
}
}
Test2.Xaml完整内容如下:
---------------------------------------------------------------------
<UserControl x:Class="BookStore.Test2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="600" Height="400">
<Grid x:Name="LayoutRoot" Background="White" >
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="50" Background="Beige" Width="200" Height="100">
<TextBlock TextAlignment="Center">
这是Test2.Xaml文件
</TextBlock>
<Button Height="25" Width="150" Content="转到Test.xaml" Click="Button_Click"></Button>
</StackPanel>
</Grid>
</UserControl>
Test2.Xaml.cs完整内容如下:(其实跟Test.Xaml.cs几乎一样)
-------------------------------------------------------------------
using System.Windows;
using System.Windows.Controls;
namespace BookStore
{
//手动增加, IContent ,让Test2实现IContent接口
public partial class Test2 : UserControl, IContent
{
public Test2()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//就这一行有点一不样(点击test2.xaml上的按钮将还回到Test"场景")
(Application.Current.RootVisual as IContent).Content = new Test();
}
/// <summary>
/// 增加一个Content属性
/// </summary>
public new UIElement Content
{
get
{
return base.Content;
}
set
{
base.Content = value;
}
}
}
}
(3)
SilverLight项目中xaml页面之间的跳转
最近因为项目需求在学习SilverLight,遇到了一个问题,就是我的项目中有多个xaml文件,我想从这个跳到另外一个xaml文件,不知道该怎么弄。(菜鸟学习中,老鸟勿嘲笑啊)
找了很多资料,基本都是转载,复制别人的,好不容易有一个原创的啊,试了还不行。最后自己找到了解决方法,其实很简单!!!
这是我的项目: 我是想从 MainPage.xaml 页面跳入 LayoutTest文件夹里面的不同的xaml
我在 MainPage.xaml 页面放了一个按钮,在按钮事件中写入代码:
this.Content = new LayoutTest.Canvas(); // 跳转入 LayoutTest 中的Canvas.xaml
这样就可以了