简体   繁体   中英

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

I'm trying to test a WebView by showing a local html file. The problem is, webview doesn't show anything.

My XAML portion is like bellow:

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

</Grid>

C# portion is:(edited: relative path corrected)

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 implementation is:

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 problems I can find:

  1. your webview doesn't have height and width. Please assign the value and test again.

  2. You may get the wrong folder. Based on the folloiwng code, the uri will be ms-appx:///html/html/problem_page/test.html . Is that the uri you expected?

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

...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM