OnPropertyChanged事件处理程序

时间:2013-04-17 15:58:49

标签: c# events httpwebrequest httpwebresponse

我正在为Windows Phone编写应用程序。我有一个变量结果,它存储服务器在发出Windows Phone应用程序请求后发送的响应。

我有一个名为 request 的类,它会生成请求并接收响应。我想检查结果变量何时更改为我可以对我的应用程序进行进一步处理。

这是请求类:

namespace PhoneApp1
{
    public class Request: INotifyPropertyChanged
    {
        public string data;
        public string result = "test";

        public Request()
        {

        }

        public string Result
        {
            get
            {
                return result;
            }
            set
            {
                if (this.result == value)
                {
                    return;
                }
                this.result = value;
                OnPropertyChanged("Result");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void doRequest(string parameters, string URL)
        {
            data = parameters;

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
        }

        public void GetRequestStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            Stream postStream = myRequest.EndGetRequestStream(callbackResult);
            byte[] byteArray = Encoding.UTF8.GetBytes(data);

            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();

            myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
        }

        public void GetResponsetStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);

            StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream());
            result = httpWebStreamReader.ReadToEnd();
        }
    }
}

如您所见,我在结果变量上实现了OnPropertyChanged事件处理程序。

以下是我创建类的实例以执行请求的代码:

                    **//Log-in Button Event Handler**
                    Request req = new Request();
                    req.PropertyChanged += new PropertyChangedEventHandler(req_PropertyChanged);
                    req.doRequest("function=LogIn&username=" + username + "&password=" + password, "http://localhost:4000/Handler.ashx");
                }
            }
        }**//End of Log-in Button Event Handler**

        public void req_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            string result = ((Request)sender).Result;

            if (result.Equals("True") || result.Equals("true"))
            {
                PhoneApplicationService.Current.State["Username"] = username;
                NavigationService.Navigate(new Uri("/Authenticated.xaml", UriKind.Relative));
            }

            else
            {
                MessageBox.Show("The log-in details are invalid!");
            }
        }

出于某种原因,单击登录按钮时没有任何反应。没有消息框,没有重定向,甚至没有例外。我无法弄清楚我做错了什么。请帮我。过去两个小时我试图解决这个问题。

为什么req_PropertyChanged代码没有执行,即使我知道结果变量正在改变?

1 个答案:

答案 0 :(得分:0)

PropertyChanged不是魔术 只有在您明确提出事件时才会触发。

你确实在你的Result制定者中举起了这个活动,但你并没有这样说 当您编写result = httpWebStreamReader.ReadToEnd();时,您可以直接设置该字段,而无需运行属性设置器 因此,事件永远不会发生。