C# 如何在Windows 8 Metro应用程序的WebView中显示HTML?

C# 如何在Windows 8 Metro应用程序的WebView中显示HTML?,c#,windows-8,microsoft-metro,C#,Windows 8,Microsoft Metro,我正在为Win8(Metro)和WP8构建一个应用程序。在这个应用程序中,我有一个网络视图,我需要在其中加载一些网页。 我通过WP8中的post请求获取HTML,如下所示: void LoadView(string username, string password) { string postDataStr = String.Format("username={0}&password={1}", username

我正在为Win8(Metro)和WP8构建一个应用程序。在这个应用程序中,我有一个网络视图,我需要在其中加载一些网页。 我通过WP8中的post请求获取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);
        }
    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);
            }
        }
这很好,但我在Win8 Metro应用程序中遇到了问题。我试过这样做:

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);
        }
    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);
            }
        }
异步任务GetWebContent(字符串用户名、字符串密码) { 字符串serviceMethod=APIConf.LOGIN\u URL; HttpClient=新的HttpClient(); client.BaseAddress=新Uri(APIConf.API\u SERVICE\u端点); HttpContent=新FormUrlEncodedContent(新[] { 新的KeyValuePair(“用户名”,用户名), 新的KeyValuePair(“密码”,password) }); HttpResponseMessage response=Wait client.PostAsync(serviceMethod,content); Stream responseStream=wait response.Content.ReadAsStreamAsync(); 字符串sResponse=null; 使用(StreamReader responseReader=新的StreamReader(responseStream)) { sResponse=responseReader.ReadToEnd(); } 返回响应; } 异步void loadviewin8(字符串用户名、字符串密码) { var htmlContent=await API.APIController.GetWebContent(用户名、密码); 如果(!String.IsNullOrEmpty(htmlContent)) { myWebView.NavigateToString(htmlContent); } } 当我执行这段代码时,myWebView会加载并显示我得到的HTML,但它不会从web加载任何样式。另外,当我点击显示的html中的一些链接时,我会看到“网页导航被取消”的页面。
我知道Win8 WebView在Navigate()方法上有一些问题,但有没有办法在我的WebView中正确加载此网页?我真的非常感谢您对我的帮助。

样式URL是绝对的还是相对的?链接呢?这就解释了。它们必须是绝对的。@Denis,你实际上已经回答了我的问题:)。我的html是应该的,谢谢你。现在的问题是,即使我的请求确实让我登录了,我也没有登录,所以当我导航到HTML中的某个链接时,它会将我重定向到登录网页。您可能必须截取到url的导航,并重复在LoadView中执行的相同过程。现在,我知道可以在WP8WebBrowser控件中拦截导航,但我不知道Win8WebView是否允许。