2016-08-19 7 views
2

htmlがms-appdataから読み込まれた場合、WindowsはWebView_ScriptNotifyイベントで呼び出しを受け付けません。UWPのappdataのhtmlからscriptNotifyを受け取る方法c#

ms-appx-webプロトコルを使用してアプリケーションバンドルからこのようなファイルを読み込むことはできますが、表示するデータはアプリケーションのインストール後にダウンロードされるため、これはオプションではありません。 webView.navigateToStringを使用することはできません。これは、参照されたライブラリがhtmlファイルに含まれないためです。現在、私は、HTMLファイル内の私のClass.xaml.cs

WebView webView = new WebView(); 
webView.ScriptNotify += WebView_ScriptNotify; 
Uri navigationUri = new Uri(@"ms-appdata:///local/index.html"); 
webView.Navigate(navigationUri); 

private void WebView_ScriptNotify(object sender, NotifyEventArgs e) 
    { 
     System.Diagnostics.Debug.WriteLine("ScriptNotifyValue: " + e.Value); 
     //I want to do the magic here, but this will never be called 
    } 

にこのような何かをしようとしている

はさらに、それはありません

<div id="content"> 
    <div class="btn" onClick="window.external.notify('hello world');"</div> 
</div> 

ですさ私はイベントが発生しなければならないとそれの値を知らないので、InvokeScript()を使用するオプション。

ms-appdataのファイルを使用することは必須です。

解決方法はありますか? 代替ワークワーニングでさえ私を驚かせるだろう。

答えて

2

文献Script notify changes in XAML:以下の条件が適用通知を送信することができるようにするコンテンツについて

  • ページのソースはNavigateToString(介してローカルシステムからのものでなければならない)NavigateToStream()またはms-appx-web:///

それとも

  • ページのソースは、HTTPS経由で配信される://とサイトのドメイン名は、パッケージマニフェストのアプリコンテンツURIのセクションに記載されています。

だから、この問題を解決するために、我々はむしろms-appdata://よりも、プロトコルms-local-stream://WebView.NavigateToLocalStreamUri methodを使用することができます。たとえば:

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     // The 'Host' part of the URI for the ms-local-stream protocol needs to be a combination of the package name 
     // and an application-defined key, which identifies the specific resolver, in this case 'MyTag'. 

     Uri url = webView.BuildLocalStreamUri("MyTag", "index.html"); 
     StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver(); 

     // Pass the resolver object to the navigate call. 
     webView.NavigateToLocalStreamUri(url, myResolver); 
    } 

    private void webView_ScriptNotify(object sender, NotifyEventArgs e) 
    { 
     System.Diagnostics.Debug.WriteLine("ScriptNotifyValue: " + e.Value); 
    } 
} 

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 app's local folder as the source 
     try 
     { 
      Uri localUri = new Uri("ms-appdata:///local" + path); 
      StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); 
      IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read); 
      return stream; 
     } 
     catch (Exception) { throw new Exception("Invalid path"); } 
    } 
} 

は、詳細情報については、 備考WebView.NavigateToLocalStreamUri methodで例とも カスタムURIがWhat’s new in WebView in Windows 8.1の解決を参照してください。また、GitHubには WebView control (XAML) sampleもあります。

+0

これは完璧に機能します。 + –

関連する問題