如何给Silverlight页面添加滑动条

本文介绍如何为Silverlight应用添加滚动条,确保页面内容完整显示。通过修改App.xaml文件,在程序启动时包裹ScrollViewer控件,实现页面滚动功能。此外,还提供了页面间导航的方法。

Silverlight的页面默认是没有上下或左右滑动条的,所以你可能会碰到这种情况,做好了一个Silverlight页面却无法浏览到底部的内容(完整的内容)。所以,我们需要在程序启动前,就把所有的页面都装在一个ScrollViewer里就可以了!

 

第一个步骤 ,在App.xaml里实现如下代码:

 

可能为了以后的方面,我们还需要在外层套一层Grid,所以代码变成:

 

private static Grid gridRoot;
private static ScrollViewer root;

 

 public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            gridRoot = new Grid();
            root = new ScrollViewer();          
            root.Content = new MainPage();
            gridRoot.Children.Add(root);
            this.RootVisual = gridRoot;
        }
        public static void Navigate(UserControl newPage)
        {              
            root = new ScrollViewer();
            root.Content = newPage;
            gridRoot.Children.Clear();
            gridRoot.Children.Add(root);           
        }

 

程序启动后,会先加载MainPage.xaml

这时候,你就发现MainPage里已经有滑动条了

 

如果要在MainPage里导航到其他页面,那么使用下面的代码就可以了:

                    App app = (App)Application.Current;
                    Page2 ep = new Page2 ();
                    App.Navigate(ep);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值