Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight:如何使用Webclient异步模式将数据从请求传递到响应?_C#_Silverlight - Fatal编程技术网

C# Silverlight:如何使用Webclient异步模式将数据从请求传递到响应?

C# Silverlight:如何使用Webclient异步模式将数据从请求传递到响应?,c#,silverlight,C#,Silverlight,如何在proxy_OpenReadCompleted方法中访问VIP void method1() { String VIP = "test"; WebClient proxy = new WebClient(); proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted); String urlStr = "someurl/lookup?q=" +

如何在proxy_OpenReadCompleted方法中访问VIP

void method1() 
{ 
    String VIP = "test";
    WebClient proxy = new WebClient();
    proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);
    String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
}

void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{ 

}

这有两种方法。第一个是将字符串作为第二个参数传递到
OpenReadAsync
调用中,此参数成为事件args的
UserState
属性的值

void method1() 
{ 
    String VIP = "test";
    WebClient proxy = new WebClient();
    proxy.OpenReadCompleted += proxy_OpenReadCompleted;
    String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
    proxy.OpenReadAsync(new Uri(urlStr, UriKind.Relative), VIP);
}    

void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{ 
   String VIP = (string)e.UserState;
   // Do stuff that uses VIP.
}
另一种方法是使用闭包直接访问变量:-

void method1() 
{ 
    String VIP = "test";
    WebClient proxy = new WebClient();
    proxy.OpenReadCompleted += (s, args) =>
    {
         // Do stuff that uses VIP.
    }
    String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
    proxy.OpenReadAsync(new Uri(urlStr, UriKind.Relative), VIP);
}    

请注意,如果异步回调方法写入数据绑定变量,则可能会出现跨线程异常。您需要使用BeginInvoke()返回UI线程。下面是一个使用WCF服务的示例,但原理是相同的

public void examsCallback(IAsyncResult result)
{
    try
    {
        EntityList<ExamEntity> examList = ((IExamService) result.AsyncState).EndGetAllExams(result);
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            foreach (ExamEntity exam in examList)
            {
                Exams.Add(exam);
            }
            ItemCount = Exams.Count;
            TotalItemCount = Exams.ItemCount;
        });
    }
    catch (Exception ex)
    {
        ErrorHandler.Handle(ex);
    }
}
public void examCallback(IAsyncResult结果)
{
尝试
{
EntityList examList=((IExamService)result.AsyncState).EndGetAllExams(result);
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
foreach(examList中的考试)
{
考试。添加(考试);
}
ItemCount=考试数;
TotalItemCount=examies.ItemCount;
});
}
捕获(例外情况除外)
{
ErrorHandler.Handle(ex);
}
}
public void examsCallback(IAsyncResult result)
{
    try
    {
        EntityList<ExamEntity> examList = ((IExamService) result.AsyncState).EndGetAllExams(result);
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            foreach (ExamEntity exam in examList)
            {
                Exams.Add(exam);
            }
            ItemCount = Exams.Count;
            TotalItemCount = Exams.ItemCount;
        });
    }
    catch (Exception ex)
    {
        ErrorHandler.Handle(ex);
    }
}