2016-05-06 16 views
2

Ich versuche, meine Anwendung Ressourcen in einem Windows 8.1 WinRT WebView mit einem IUriToStreamResolver zu injizieren, wie hier beschrieben: https://blogs.msdn.microsoft.com/wsdevsol/2014/06/20/a-primer-on-webview-navigatetolocalstreamuri/Deadlock in IUriToStreamResolver wenn synchrone AJAX machen fordert

Aber ich bin jedes Mal eine synchrone XHR ab Bei meinem JavaScript gerät die App offenbar in eine Sackgasse. Derselbe Code funktioniert in einer Windows 10 UWP-App einwandfrei.

Leider muss ich synchrone Anfragen verwenden. Ist es möglich, den Stillstand zu vermeiden?

index.html:

<!DOCTYPE html> 
<html> 
<head><title>Test</title></head> 
<body> 
    <span id="progress">Loading...</span> 

    <script type="text/javascript"> 
     // Set timeout to show the page before causing the deadlock 
     setTimeout(function() { 
      var xhr = new XMLHttpRequest(); 
      xhr.open('GET', '/index.html', /* async */ false); 
      xhr.onload = function() { 
       /* this will never be called... */ 
       document.getElementById('progress').textContent = 'Success'; 
      }; 
      xhr.onerror = function() { 
       /* this will never be called... */ 
       document.getElementById('progress').textContent = 'Error'; 
      }; 
      xhr.send(); 
     }, 1000); 
    </script> 
</body> 
</html> 

MainPage.xaml.cs:

private void Page_Loaded(object sender, RoutedEventArgs e) { 
    // webview is <WebView x:Name="webview" /> 
    var uri = webview.BuildLocalStreamUri("Test", "index.html"); 
    webview.NavigateToLocalStreamUri(uri, new StreamUriWinRTResolver()); 
} 

// I took this from the MSDN example 
public sealed class StreamUriWinRTResolver : IUriToStreamResolver { 
    public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri) { 
     if (uri == null) 
      throw new Exception(); 
     string path = uri.AbsolutePath; 
     return GetContent(path).AsAsyncOperation(); 
    } 

    private async Task<IInputStream> GetContent(string path) { 
     try { 
      // Load directly from appx in this example 
      Uri localUri = new Uri("ms-appx://" + path); 
      // This line is causing the app to hang: 
      StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); 
      IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read); 
      return stream; 
     } catch (Exception) { throw new Exception("Invalid path"); } 
    } 
} 

Antwort

0

In meinem Fall war die Lösung die IUriToStreamResolver in C++ zu schreiben.

ich verwendet, um dieses Beispiel Klasse von Microsoft bereitgestellt, die Probleme mit synchroner XHRs nicht verursacht: https://code.msdn.microsoft.com/HTML-WebView-control-sample-56e773fa/sourcecode?fileId=99260&pathId=523934392

ich erklären kann nicht wirklich, warum dies einen Unterschied macht, aber die Tatsache, dass UWP verhält sich anders lässt mich denken Das könnte ein Fehler im Framework sein.