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

silverlight应用程序只显示一部分的解决方案路过的朋友留下脚印

       前段时间,我自己也被silverlight应用程序在网页只显示一部分的问题困扰着!话了几天的时间在网上搜索资料、专著中找答案也没有找到答案。不过我写的那个页面在我的机子上是只显示一部,把那个页面考到别的机子上却又能完全显示

      所以我认定是我的机子出了问题。不过通过我对silverlight的研究发现,silverlight应用程序,它的页面是受网页的影响的(它需要用浏览器来承接应用程序),所以网页的大小直接影响着silverlight应用程序的显示范围。

     因此,我们需要把应用程序的大小告诉浏览器,这样的话我们的问题就解决了。

示例:<UserControl x:Class="My.WelcomePage"
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml"
    Width="1000" Height="700">
    <Grid x:Name="LayoutRoot"  Width="1000" >
        <Grid.RowDeFinitions>
            <RowDeFinition Height="150"/>
            <RowDeFinition Height="720"/>
            <RowDeFinition Height="150"/>
        </Grid.RowDeFinitions>
        <Grid.Background>
            <ImageBrush ImageSource="images/Welcome/DiTuOne.jpg"/>
        </Grid.Background>
        <Grid Grid.Row="0">
            <Canvas Canvas.Left="75" Canvas.Top="10" Height="120" Width="443">
                <Image Source="images/Welcome/logo.jpg">     
                </Image>
            </Canvas>
        </Grid>
        <Grid Grid.Row="1">
           
        </Grid>
        <Grid Grid.Row="2">
           
        </Grid>
    </Grid>
</UserControl>

 

就这样一个页面运行时,只能看到宽度为1000的,高度最多是150的范围的局部界面。

不过我把xaml页面的大小告诉,网页后,效果就完全不一样了。即给它加上一个LayoutRoot_LayoutUpdated事件用于通知浏览器。

<UserControl x:Class="My.WelcomePage"
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml"
    Width="1000" Height="700">
    <Grid x:Name="LayoutRoot"  Width="1000" LayoutUpdated="LayoutRoot_LayoutUpdated">
        <Grid.RowDeFinitions>
            <RowDeFinition Height="150"/>
            <RowDeFinition Height="720"/>
            <RowDeFinition Height="150"/>
        </Grid.RowDeFinitions>
        <Grid.Background>
            <ImageBrush ImageSource="images/Welcome/DiTuOne.jpg"/>
        </Grid.Background>
        <Grid Grid.Row="0">
            <Canvas Canvas.Left="75" Canvas.Top="10" Height="120" Width="443">
                <Image Source="images/Welcome/logo.jpg">     
                </Image>
            </Canvas>
        </Grid>
        <Grid Grid.Row="1">
           
        </Grid>
        <Grid Grid.Row="2">
           
        </Grid>
    </Grid>
</UserControl>

 

并且在CS文件的该事件中添加上相应语句即可。

private void LayoutRoot_LayoutUpdated(object sender,EventArgs e)
        {

            Size size = this.LayoutRoot.DesiredSize;

 

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

            String containerElementId = "silverlightControlHost";

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

            element.SetStyleAttribute("height",heightInPixel);

 

        }

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

相关推荐