我正在为Win8(Metro)和WP8构建一个应用程序。在这个应用程序中,我有一个WebView,我需要加载一些网页。 我在WP8中通过帖子请求获取HTML,如下所示:
void LoadView(string username, string password)
{
string postDataStr = String.Format("username={0}&password={1}",
username, password);
byte[] postDataByte = Encoding.UTF8.GetBytes(postDataStr);
string additionalHeaders = "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine;
var urlLogin = new Uri(new Uri(APIConf.API_SERVICE_ENDPOINT), APIConf.LOGIN_URL);
myWebView.Navigate(urlLogin, postDataByte, additionalHeaders);
}
这很好用,但我遇到了Win8 Metro应用程序的问题。我试过这样的:
async Task<string> GetWebContent(string username, string password)
{
string serviceMethod = APIConf.LOGIN_URL;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(APIConf.API_SERVICE_ENDPOINT);
HttpContent content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password)
});
HttpResponseMessage response = await client.PostAsync(serviceMethod, content);
Stream responseStream = await response.Content.ReadAsStreamAsync();
String sResponse = null;
using (StreamReader responseReader = new StreamReader(responseStream))
{
sResponse = responseReader.ReadToEnd();
}
return sResponse;
}
async void LoadViewInWin8(string username, string password)
{
var htmlContent = await API.APIController.GetWebContent(username, password);
if (!String.IsNullOrEmpty(htmlContent))
{
myWebView.NavigateToString(htmlContent);
}
}
当我执行此代码时,myWebView会加载并显示我获得的HTML,但它不会从web加载任何样式。此外,当我点击显示的html中的一些链接时,我得到页面“导航到网页被取消”。 我知道Win8 WebView在Navigate()方法有一些问题,但有没有办法可以在我的WebView中正确加载这个webPage?我真的很感激我能得到的任何帮助。