I am writing an application for Windows Phone. I have a variable result which stores the response sent by a server after a request from the Windows Phone app.
I have a class called request which generates the request and receives the response. I want to check when the result variable changes to that I can do some further processing on my application.
Here is the request class:
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();
}
}
}
As you can see, I implemented an OnPropertyChanged event handler on the result variable.
Here is the code from where I am creating an instance of the class to do the request:
**//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!");
}
}
For some reason or another, nothing happens when I click the log-in button. No message box, no redirection, not even an exception. I can't figure out what I am doing wrong. Please help me. I have wasted these past two hours trying to solve this problem.
Why is the req_PropertyChanged code not executing, even though I know that the result variable is changing?