0

このアプリがサーバーから受け取った html 文字列を Web ブラウザーに表示しようとしています。なぜか大失敗しています。HTML 文字列をメッセージ ボックスに表示することはできますが、Web ブラウザに表示することはできません。私はC#に非常に慣れていないので、不要なコードや間違いをお詫びします。また、よろしくお願いします。

namespace Test2
{
public partial class MainPage : PhoneApplicationPage
{
    private static readonly Dispatcher Dispatcher;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // Create a new HttpWebRequest object.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/examples/servlets/servlet/ReverseServlet");

        request.ContentType = "application/x-www-form-urlencoded";

        // Set the Method property to 'POST' to post data to the URI.
        request.Method = "POST";

        // start the asynchronous operation
        request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

    }

    private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        Stream postStream = request.EndGetRequestStream(asynchronousResult);
        System.Diagnostics.Debug.WriteLine("Please enter the input data to be posted:");//Test code
        string postData = "string is this = Testing out this app for windows 7 phone";// Test string added

        // Convert the string into a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Write to the request stream.
        postStream.Write(byteArray, 0, postData.Length);
        postStream.Close();

        // Start the asynchronous operation to get the response
        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    private static void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        Deployment.Current.Dispatcher.BeginInvoke(
        () =>
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();

            WebBrowser webBrowser1 = new WebBrowser();
            MessageBox.Show(responseString);//Message box displays html
            webBrowser1.Visibility = Visibility.Visible;
            webBrowser1.NavigateToString(responseString);//Does NOT display html string

            System.Diagnostics.Debug.WriteLine(responseString+" FIXED IT");

            // Close the stream object
            streamResponse.Close();
            streamRead.Close();

            // Release the HttpWebResponse
            response.Close();
            //allDone.Set();
        });
    }
}
}
4

1 に答える 1

0

UI に webBrowser を追加する必要があります。UI に ContentPanel というグリッドがある場合ContentPanel.Children.Add(webBrowser);、コードを使用して作成する代わりに、webBrowser を UI に直接追加することもできます。文字列を使用して HTML コンテンツを表示するにはwebBrowser.NAvigateToString(responseString);

于 2012-09-14T05:20:00.427 に答える