Win8 (Metro) および WP8 用のアプリを作成しています。このアプリには、Web ページをロードする必要がある 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のリンクをクリックすると、「Webページへの移動がキャンセルされました」というページが表示されます。Win8 WebView の Navigate() メソッドに問題があることはわかっていますが、この WebPage を WebView に適切にロードする方法はありますか? これで得られる助けがあれば本当にありがたいです。