2017-04-18 30 views
0

파일 쓰기 보안 문제로 인해 응용 프로그램을 ms-appx-web :에서 ms-appdata :로 변경하려고했습니다. 그러나 ms-appx-web에서는 잘 작동하는 window.external.notify()에 의존하기 때문에 즉시 실패합니다 : ms-appdata :와 함께 아무 작업도하지 않는 것처럼 보입니다. 시험 삼아 우리는 웹보기 개체에 다음 HTML을로드 : 어떤 종류의 팝업 메시지를 생성하지 않습니다,Windows 10에서 ms-appdata로 Javascript notify()하는 방법 UWP

demo <body> 
demofunc() 

하지만 : 예상대로이 결과를 생성

<html> 
<head> 
    <script> 
     function demofunc(str) { 
      document.getElementById("demo").innerHTML += str; 
      window.external.notify(str); 
     } 
    </script> 
</head> 
<body onLoad="demofunc('demofunc()');"> 
    demo &lt;body&gt; 
    <div id="demo"></div> 
</body> 
</html> 

합니다. 왜? 데모 div에 두 번째 출력 행을 추가하기 위해 demofunc() 메서드가 호출되었지만 window.external.notify()가 팝업 메시지를 생성하지 않는 것은 분명합니다. ms-appdata와 함께 notify()에 관한 특별 규칙이 있습니까?

업데이트 - 질문은 Can't run javascript alerts in universal app's webview at payment gateway과 비슷하며 ms-appx-web에서는 작동하지만 ms-appdata :에서는 작동하지 않습니다. 그 질문은 ScriptNotify()를 캐치합니다. 그런 다음 Windows.UI.Popups.MessageDialog를 사용하여 대화 상자를 팝업합니다. ms-appx-web을 사용하면 ScriptNotify()가 호출되지만 ms-appdata를 사용하면 ScriptNotify()가 호출되지 않습니다. 그것이 우리 문제입니다. 팝업이 발생하지 않습니다.

+0

아마도 도움이 될까요? https://social.msdn.microsoft.com/Forums/en-US/bdde7d0f-b1dd-4708-95a9-afe1a50dbb75/script-notify-for-msappdata?forum=w81prevwCsharp – NewToJS

+0

[자바 스크립트 경고를 실행할 수 없습니다 범용 애플 리케이션의 지불 게이트웨이에서 webview] (http://stackoverflow.com/questions/31804380/cant-run-javascript-alerts-in-universal-apps-webview-at-payment-gateway) – AVK

+0

감사합니다 링크, 그것은 매우 흥미 롭습니다. notify() 이벤트를 잡아 내고 또 다른 메소드를 실행하는 것으로 보이는데 이는 실제로 유용합니다. 그러나 우리는 또한 사용자를위한 대화 메시지를 실제로 팝업해야합니다. 누락 된 팝업 대화 상자는 사실이 게시물의 가장 중요한 이유입니다. – user3334340

답변

1

그러나 그것은 바로 우리가 MS-appx-웹과 잘 작동 window.external.notify()에 의존하기 때문에 실패하지만 MS-APPDATA와 무 조작으로 행동하는 것 같다 :.

시나리오의 경우 ms-appdata:///이 아닌 ms-local-stream:/// 구성표를 사용하십시오. 그리고 난 ms-local-stream:/// 구성표를 테스트했습니다, 그것은 꽤 잘하고 있습니다.

NavigateToLocalStreamUri 메서드를 사용하려면 URI 패턴을 콘텐츠 스트림으로 변환하는 IUriToStreamResolver 구현을 전달해야합니다. 다음 코드를 참조하십시오.

StreamUriWinRTResolver

public sealed class StreamUriWinRTResolver : IUriToStreamResolver 
    { 
     /// <summary> 
     /// The entry point for resolving a Uri to a stream. 
     /// </summary> 
     /// <param name="uri"></param> 
     /// <returns></returns> 
     public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri) 
     { 
      if (uri == null) 
      { 
       throw new Exception(); 
      } 
      string path = uri.AbsolutePath; 
      // Because of the signature of this method, it can't use await, so we 
      // call into a separate helper method that can use the C# await pattern. 
      return getContent(path).AsAsyncOperation(); 
     } 

     /// <summary> 
     /// Helper that maps the path to package content and resolves the Uri 
     /// Uses the C# await pattern to coordinate async operations 
     /// </summary> 
     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-appdata:///local" + 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"); } 
     } 
    } 

MainPage는

public MainPage() 
{ 
    this.InitializeComponent(); 
    MyWebView.ScriptNotify += MyWebView_ScriptNotify; 
    Uri url = MyWebView.BuildLocalStreamUri("MyTag", "/Test/HomePage.html"); 
    StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver(); 
    MyWebView.NavigateToLocalStreamUri(url, myResolver); 
} 

나는 GitHub의에 code sample을 업로드했습니다. 확인해주십시오.

+0

고맙습니다. 실제로 팝업 대화 상자가 표시되지 않는 문제가 해결되었습니다. 분명히 UWP는 Javascript가 ms-appdata를 사용하여 팝업을 시작하는 것을 허용하지 않지만 ms-local-stream을 사용하여 허용합니다. – user3334340