2016-08-19 5 views
2

html이 ms-appdata에서로드 된 경우 Windows에서 WebView_ScriptNotify 이벤트가 호출을 수신하지 못하게합니다.UWP의 appdata에서 html로 scriptNotify를받는 방법 C#

ms-appx-web 프로토콜을 사용하여 내 앱 번들에서 이러한 파일을로드 할 수 있다는 것을 알고 있지만 표시 할 데이터가 앱 설치 후 다운로드되므로 옵션이 없습니다. html 파일에 참조 된 라이브러리가 포함되지 않기 때문에 webView.navigateToString을 사용할 수도 없습니다.

가 현재 내 Class.xaml.cs HTML 파일

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의 파일을 사용해야합니다.

해결 방법을 알고 계십니까? 대체 workaroung도 나를 놀라게합니다.

답변

2

참조 Script notify changes in XAML : 내용이 다음과 같은 조건이 적용 알림을 보낼 수 있으려면

:

페이지의 소스가 NavigateToString()를 통해 로컬 시스템에서해야
  • , NavigateToStream() 또는 ms-appx-web : ///
(210)

또는

  • 페이지의 소스는 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

멋진 작품입니다. + –

관련 문제