2012-02-15 53 views
5

Trong số other question Tôi phát hiện ra rằng không có danh sách trắng cho điện thoại cửa sổ.Khắc phục sự cố mất danh sách trắng trong phonegap cho cửa sổ điện thoại

Bây giờ tôi đang tìm một giải pháp mã nguồn gốc nhưng tôi chưa bao giờ viết dòng mã gốc cho điện thoại Windows. Vì vậy, nó không phải dễ dàng cho tôi. Tôi nghĩ rằng tôi có thể tải xuống một trang như sau:

void GetAirportData() 
{ 
    var url = new Uri("http://server.example.com/data.php", UriKind.Absolute); 
    var webClient = new WebClient(); 
    webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 
    webClient.OpenReadAsync(url, url); 
} 

Nhưng làm cách nào để có được dữ liệu này cho ứng dụng javascript của tôi?

Trả lời

11

Đây là giải pháp thay thế. Đoạn mã sau là một lệnh Phonegap thực hiện chức năng Cross Domain Call.

using System; 
using System.IO; 
using System.Net; 
using System.Runtime.Serialization; 
using WP7CordovaClassLib.Cordova; 
using WP7CordovaClassLib.Cordova.Commands; 
using WP7CordovaClassLib.Cordova.JSON; 

namespace Cordova.Extension.Commands //namespace is predefined, don't change it! 
{ 
    public class Cdc : BaseCommand //Cross domain call 
    { 
     [DataContract] 
     public class CdcOptions 
     { 
      [DataMember(Name = "path")] 
      public string Path { get; set; } 
     } 

     public void Call(string args) 
     { 
      CdcOptions options = JsonHelper.Deserialize<CdcOptions>(args); 

      var url = new Uri(options.Path, UriKind.Absolute); 

      var webClient = new WebClient(); 

      webClient.OpenReadCompleted += (s, e) => 
      { 
       if (e.Error != null) 
       { 
        DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error")); 
        return; 
       } 

       //Stream -> string 
       var sr = new StreamReader(e.Result); 
       var result = sr.ReadToEnd(); 

       DispatchCommandResult(
        new PluginResult(PluginResult.Status.OK, result)); 
      }; 

      webClient.OpenReadAsync(url, url); 

     } 
    } 
} 

thử nghiệm trên các mặt hàng:


 <script type="text/javascript"> 

      function cdc(path, success, fail) { 

       PhoneGap.exec(
          success, //success 
          fail, //fail 
          "Cdc", //service 
          "Call", //action 
          path //args 
          ); 
      }; 

      function onDeviceReady(e) { 

       cdc(
        { 
         path: "http://stackoverflow.com/questions/9291809/workaround-for-missing-whitelist-in-phonegap-for-windows-phone" 
        }, 
        function (arg) { 
         document.getElementById('test').innerHTML = arg; 
        }, function (arg) { 
         document.getElementById('test').innerHTML = arg; 
        }); 

      } 

      document.addEventListener("deviceready", onDeviceReady, false); 


     </script> 
    </head> 
    <body> 
     <div id="test"></div> 
    </body> 
</html> 
+0

+50! Trong Visual Studio tôi đã thêm một tệp lớp mới 'Cdc.cs' và đặt khối mã đầu tiên vào nó. Sau đó, tôi đã bao gồm phonegap.js trong index.html của mình và thêm khối mã thứ hai. Bắt đầu ứng dụng và nó hoạt động hoàn hảo! Cảm ơn rất nhiều. Tôi không thể làm được mà không có bạn! – PiTheNumber

+0

Vui lòng bỏ phiếu cho câu trả lời này. Ông hoàn toàn xứng đáng với nó! – PiTheNumber

Các vấn đề liên quan