XAML 和 C# WebView 在 Windows Phone 8.1 App 中不工作

XAML and C# WebView not working in Windows Phone 8.1 App

我正在尝试通过显示本地 html 文件来测试 WebView。问题是,webview 没有显示任何内容。

我的 XAML 部分如下所示:

<Grid>
    <StackPanel Orientation="Vertical">
        <TextBlock x:Name="onlineTestHeadingText"
            Text="Welcome to online test!"
            FontSize="24"
            Foreground="Red"/>
        <WebView x:Name="onlineTestWebView" 
               />
    </StackPanel>

</Grid>

C# 部分是:(已编辑:已更正相对路径)

public sealed partial class OnlineTest : Page
{

    Uri url;
    public OnlineTest()
    {
        this.InitializeComponent();
    }

    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        url = onlineTestWebView.BuildLocalStreamUri("myUrl", "/problem_page/test.html");
        StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();

        onlineTestWebView.NavigateToLocalStreamUri(url, myResolver);
    }
} 

Helper class 实现是:

public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
    public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
    {
        if (uri == null)
        {
            throw new Exception();
        }
        string path = uri.AbsolutePath;

        // Because of the signature of the this method, it can't use await, so we 
        // call into a seperate helper method that can use the C# await pattern.
        return GetContent(path).AsAsyncOperation();
    }

    private async Task<IInputStream> GetContent(string path)
    {
        // We use a package folder as the source, but the same principle should apply
        // when supplying content from other locations
        try
        {
            Uri localUri = new Uri("ms-appx:///html" + path);
            StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
            IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
            return stream.GetInputStreamAt(0);
        }
        catch (Exception) { throw new Exception("Invalid path"); }
    }
}

2 个我能找到的问题:

  1. 您的网页视图没有高度和宽度。请重新赋值测试

  2. 您可能会得到错误的文件夹。基于以下代码,uri 将为 ms-appx:///html/html/problem_page/test.html。那是你期望的 uri 吗?

url = onlineTestWebView.BuildLocalStreamUri("myUrl", "/html/problem_page/test.html");

...

Uri localUri = new Uri("ms-appx:///html" + path);