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

来源:互联网 发布:淘宝店开店流程及费用 编辑:程序博客网 时间:2024/05/22 06:29

       前段时间,我自己也被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);

 

        }

 

原创粉丝点击