Я пытаюсь создать простой класс для моих вызовов REST, чтобы в моем приложении не было одного и того же кода в нескольких местах.
Проблема в том, что я не знаю, как уведомить объект, вызвавший метод UploadStringAsync (), изнутри обработчика события UploadStringCompleted ().
Вот мой класс, я добавил комментарии, чтобы отметить место, где я хочу уведомить объект, из которого был вызван метод Post:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MyProject.Utils
{
public class Rest
{
private WebClient client = new WebClient();
private void Init()
{
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
}
public void Post(string uri, string postRequest, object objectToNotify)
{
Init();
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
client.UploadStringAsync(new Uri(uri,
UriKind.RelativeOrAbsolute), "POST", postRequest, objectToNotify);
}
private void client_UploadStringCompleted(object sender,
UploadStringCompletedEventArgs e)
{
// here I would like to notify an object from
// which the Post() method has been called
// I don't know how to do this as this method has no idea
// which object called the method
// this doesn't work:
// (e.UserState.GetType())e.UserState.RestCallback(e);
}
}
}
EDIT:
Мой модифицированный код после предложений Джейкоба:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
namespace MyProject.Utils
{
public class Rest
{
private WebClient client = new WebClient();
private void Init()
{
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
}
public void Post(
string uri, string postRequest,
Action<UploadStringCompletedEventArgs> callback)
{
HtmlPage.Window.Alert("bbb");
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
client.UploadStringAsync(new Uri(uri,
UriKind.RelativeOrAbsolute), "POST", postRequest, callback);
}
private void client_UploadStringCompleted(object sender,
UploadStringCompletedEventArgs result)
{
HtmlPage.Window.Alert("aaa");
var callback = (Action<UploadStringCompletedEventArgs>)result.UserState;
callback(result);
}
}
}
Вот как я вызываю метод Post:
string postRequest = "id=5":
Rest restClient = new Rest();
restClient.Post("http://mywebsite.com/", postRequest, RestCallback);
.......................
private void RestCallback(UploadStringCompletedEventArgs result)
{
if (result.Error != null)
{
ContactFormSuccess.Text = "There was an error sending email message.";
}
else
{
ContactFormSuccess.Text = "Email message successfully sent.";
}
}