UWP:如果调整窗口大小,则`webview`内容将被切断

时间:2018-11-22 07:43:39

标签: windows visual-studio c#-4.0 uwp

我想用窗口调整webview的大小。 但是,当我减小窗口的大小时,Webview被切断了。 (当我增大窗口的大小时,它看起来很正常。)

    <Grid>
        <WebView x:Name="m_WebView" HorizontalAlignment="Left" Height="350" Margin="0,0,0,0" VerticalAlignment="Top" Width="568" ScriptNotify="m_WebView_ScriptNotify" NavigationCompleted="m_WebView_OnNavigationCompleted" x:FieldModifier="public"/>
        <ProgressRing x:Name="workingProgressRing" HorizontalAlignment="Center" VerticalAlignment="Center" Width="50" Height="50"/>

    </Grid>

        private void Page_onSizeChanged2(object sender, SizeChangedEventArgs e)
        {

            double widthRatio = e.NewSize.Width / (double)568;
            double heightRatio = e.NewSize.Height / (double)320;
            double newRatio = widthRatio < heightRatio ? widthRatio : heightRatio;
            double newWidth = 568 * newRatio;
            double newHeight = 320 * newRatio;

            m_WebView.RenderTransform = new CompositeTransform { ScaleX = newRatio, ScaleY = newRatio };

            ((CompositeTransform)m_WebView.RenderTransform).TranslateX = -(newWidth - 568 * widthRatio) / 2;
            ((CompositeTransform)m_WebView.RenderTransform).TranslateY = -(newHeight - 320 * heightRatio) / 2;


        }

Normal window picture.

Cut off webview picture.

1 个答案:

答案 0 :(得分:1)

UWP中控件大小的正确方法是根据布局自动缩放,而不是直接硬编码WidthHeight值。

根据您的情况,您可以删除WidthHeight属性,将HorizontalAlignmentVerticalAlignment设置为Stretch,然后使WebView缩放改为使用Grid