C# 通过Windows Phone 8中的事件处理程序将数据传递到ui
我使用以下模式将工作线程上运行的HTTP请求的响应数据传递给相关UIC# 通过Windows Phone 8中的事件处理程序将数据传递到ui,c#,events,windows-phone-8,event-handling,eventargs,C#,Events,Windows Phone 8,Event Handling,Eventargs,我使用以下模式将工作线程上运行的HTTP请求的响应数据传递给相关UI using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Counter c = new Counter(new Random().Next(10)); c.ThresholdReached +=
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next(10));
c.ThresholdReached += c_ThresholdReached;
Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add(1);
}
}
static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit(0);
}
}
class Counter
{
private int threshold;
private int total;
public Counter(int passedThreshold)
{
threshold = passedThreshold;
}
public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
}
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
}
public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}
步骤2:然后在HTTPManager中,在WebClient完成其POST请求后,我执行以下操作:
private void ButtonSend_Click(object sender, RoutedEventArgs e)
{
// Create a worker thread
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_DoWorkFinished);
worker.WorkerSupportsCancellation = true;
worker.RunWorkerAsync();
}
....
void worker_DoWork(object sender, DoWorkEventArgs e)
{
ApplicationManager.Instance.getHTTPManager().makeHTTPSRequest(url);
}
webClient.WriteStreamClosed += (s, args) =>
{
if (args.Error != null)
{
status = mStatusFromServer;
}
else
{
status = HTTPManager.mDefaultStatus;
}
ContactSendCompletedEventArgs args2 = new ContactSendCompletedEventArgs();
args2.Status = status;
Debug.WriteLine("Status: " + args2.Status);
OnContactSendCompleted(args2);
//Debug.WriteLine("Status: " + status);
//Contact_Send_Complete(this, status, i);
};
HTTPManager man = ApplicationManager.Instance.getHTTPManager();
man.ContactSendCompleted += c_Contact_Send_Completed;
static void c_Contact_Send_Completed(object sender, ContactSendCompletedEventArgs args)
{
Debug.WriteLine("The status: ", args.Status);
...
}
我也在这门课上做过事件处理,比如
namespace MyPhone8App.Common
{
public delegate void ContactSendCompletedEventHandler(ContactSendCompletedEventArgs e);
class HTTPManager
{
...
protected virtual void OnContactSendCompleted(ContactSendCompletedEventArgs e)
{
EventHandler<ContactSendCompletedEventArgs> handler = ContactSendCompleted;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<ContactSendCompletedEventArgs> ContactSendCompleted;
}
public class ContactSendCompletedEventArgs : EventArgs
{
public String Status { get; set; }
}
就这些。有什么想法吗 你应该发布winphone 8应用程序出现问题的代码。很抱歉,现在不可用。但现在我可以更清楚地详述我做了什么。我有一个触发BackgroundWorker的UI页面,我在MyHTTPManager类中使用WebClient执行HTTPS请求。这个类也是我定义EventHandler的地方。在UI页面中,我订阅了以下事件:;ApplicationManager.Instance.getHTTPManager.My_事件+=事件处理程序\u回调\u在\u页面中。在WebClient OpenWriteClosed方法中,我获取响应的状态并将其传递给UI。尽管它进入回调,但没有要传递的数据。希望这有点帮助。定义空数据。参数e为空吗?不,一点也不。请看我的答案