在silverlight中的页面跳转和传值

页面跳转:

由于app.xaml是整个应用启动的入口点,他是没有Usercontrol的,但是在Application_startup中,他会去创建一个 MainPage.xaml的实例,然后把它组装到app上。

因此要实现页面跳转只需要在app.xaml中作工作,也就是定一个RootView,然后所有的页面跳转都其实是在替换这个RootView的子控件而已。

在app.xaml.cs中先定义个RootView:

private Grid rootGrid = new Grid();

然后重写方法:

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = rootGrid;
            rootGrid.Children.Clear();
            rootGrid.Children.Add(new Page1());
        }
到这里就可以实现启动时候,自动加载页面Page1了。那么,如果有多个页面,怎么实现自动切换呢?

可以在app.xaml.cs中定一个静态方法:

        public static void Navigation(UserControl newPage)
        {
            //获取当前的Appliaction实例
            App currentApp = (App)Application.Current;

            //修改当前显示页面内容.
            currentApp.rootGrid.Children.Clear();
            currentApp.rootGrid.Children.Add(newPage);
        }

这样,在所有页面之间跳转的时候,只需要使用 App.Navigation(new otherPage())就会跳转到其他页面。


页面之间传值:

其实还是利用App.xaml的应用级的特性,使用using System.IO.IsolatedStorage。

首先在App.xaml.cs中定一个全局的静态变量:

 public static IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

然后在一个页面中的事件中做传值处理:

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //在向页面中传值时,可以先清空所有的内容,保证每次一都是当前有效的值即可。
            App.appSettings.Clear();

            if (! App.appSettings.Contains("Page2"))
            {
                App.appSettings.Add("Page2", "m From Page1");
            }

            App.Navigation(new Page2());
        }

在另一个页面中做取值处理:

textBlock1.Text = App.appSettings["Page2"].ToString();

that's it, 这就是比较合理的页面跳转和传值的方式。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值