微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解决Silverlight插件过长浏览器滚动条不出现的问题

在aspx页面中只能静态的设置Silverlight插件的大小,但是插件的高度是时常变化的,而且如果变化很大,比如从几百到几千px,那总不能一开始就设一个很大的高度吧?但问题是Silverlight并不能根据自己的高度将页面撑开,而只是clip了而已,这就带来了一个问题,如何才能在适当的时候才让浏览器的滚动条出现呢?这个问题倒是困扰了一部分的朋友的,因此来测试和总结一下。

  首先建一个新工程,我这里建的是“Silverlight Navigation Application”,也就是带导航模板的,因此在模板的home.xaml页面中放一个很长的矩形,如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<Grid x:Name="LayoutRoot">
        <!--<ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">-->
            <StackPanel x:Name="ContentStackPanel">
                <TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}" 
                                   Text="Home"/>

                <TextBlock x:Name="ContentText" Style="{StaticResource ContentTextStyle}" 
                                   Text="Home page content"/>
                <Rectangle Width="200" Height="1000" Fill="Red"/>
            </StackPanel>
        <!--</ScrollViewer>-->
</Grid>

 

 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public MainPage()
{

            InitializeComponent();

            this.LayoutUpdated += new EventHandler(MainPage_LayoutUpdated);
        }

        private double _minimalHeight = 300;

        void MainPage_LayoutUpdated(object sender,EventArgs e)

        {

            Size size = this.LayoutRoot.DesiredSize;

            if (size.Height < _minimalHeight)

            {

                size.Height = _minimalHeight;

            }

            String heightInPixel = String.Format("{0}px",size.Height);

            String containerElementId = "silverlightControlHost";

            HtmlElement element = HtmlPage.Document.GetElementById(containerElementId);

            element.SetStyleAttribute("height",heightInPixel);
}
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public MainPage()
{

            InitializeComponent();

            this.LayoutUpdated += new EventHandler(MainPage_LayoutUpdated);
        }

        private double _minimalHeight = 300;

        void MainPage_LayoutUpdated(object sender,heightInPixel);
}

 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public MainPage()
{

            InitializeComponent();

            this.LayoutUpdated += new EventHandler(MainPage_LayoutUpdated);
        }

        private double _minimalHeight = 300;

        void MainPage_LayoutUpdated(object sender,heightInPixel);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐