首页 > 解决方案 > C# UWP WebView 未出现在 UWP 主页上

问题描述

I am working on webview control in C# UWP app. Everything was working. All at a sudden webview stop showing the webpage on the window. I tried creating one more app and rewrite the code once more. But it's not working now.

Below is the code.

<Page
    x:Class="hello.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:hello"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid >
        <WebView x:Name="webView" Source="http://www.www.bing.com" />
    </Grid>
</Page>

错误: 我也尝试使用其他 URL。试过 webView.Navigate(new Uri(" http://www.bing.com ")); 在 cs 文件中也是如此,但 webview 根本没有出现在窗口中。搜索了很多,但没有得到任何解决方案。所有断点都被正确命中,并且在运行时不会抛出任何错误。手动清理调试文件夹并再次重建应用程序..但没有运气。有人可以帮我解决这个问题吗?

标签: c#webviewuwpuwp-xaml

解决方案


C# UWP WebView 未出现在 UWP 主页上

我检查了您的代码,发现您传递了错误的 uri http://www.www.bing.com,如果您想让 webview 填充Grid您需要的设置VerticalAlignment="Stretch" HorizontalAlignment="Stretch",我编辑了您的代码,它可以工作,请参考以下内容。

<Grid>
    <WebView
        x:Name="webView"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        Source="http://www.bing.com"
        />
</Grid>

您可以在页面加载事件中调用 方法,并在 xaml 代码中webView.Navigate(new Uri("http://www.bing.com"))删除上述属性。Source

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    webView.Navigate(new Uri("http://www.bing.com"));
}

请注意您的 WebView 将访问互联网,因此您需要检查互联网功能。


推荐阅读