Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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# 在WP7上等待异步完成的最佳方法_C#_Windows Phone 7 - Fatal编程技术网

C# 在WP7上等待异步完成的最佳方法

C# 在WP7上等待异步完成的最佳方法,c#,windows-phone-7,C#,Windows Phone 7,我有一些艰难的时间在这方面,我正试图得到我的第一个WP7应用程序了。 我有一个从网站下载html并对其进行正则化的方法,但问题是,当我第一次单击按钮时,什么都没有发生,在第二次尝试时,它完美地填充了网格,当我调试时,我看到带有html的字符串在该方法开始之前已经被正确分配。所以,问题是,等待异步方法完成的最简单方法是什么? 我已经搜索过CTP async和其他一些方法,但我无法让它工作。 这是密码 public static void client_DownloadStringComple

我有一些艰难的时间在这方面,我正试图得到我的第一个WP7应用程序了。 我有一个从网站下载html并对其进行正则化的方法,但问题是,当我第一次单击按钮时,什么都没有发生,在第二次尝试时,它完美地填充了网格,当我调试时,我看到带有html的字符串在该方法开始之前已经被正确分配。所以,问题是,等待异步方法完成的最简单方法是什么? 我已经搜索过CTP async和其他一些方法,但我无法让它工作。 这是密码

   public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        doc = e.Result;
    }

    public static List<Row> Search(string number)
    {
        WebClient wClient = new WebClient();

        sNumber = number;
        int i = 0;
        DateTime datetime;

        wClient.DownloadStringAsync(new Uri(sURL + sNumber));
        wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
              /*More code*/
     }
   public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result != null)
        {
            //populate grid view
        }
    }

    public static void Search(string number)
    {
        WebClient wClient = new WebClient();

        sNumber = number;
        int i = 0;
        DateTime datetime;

        wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); //this should be added in the constructor, so it would only be added once
        wClient.DownloadStringAsync(new Uri(sURL + sNumber));
     }
publicstaticvoid客户端\u DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
doc=e.结果;
}
公共静态列表搜索(字符串编号)
{
WebClient wClient=新的WebClient();
sNumber=数量;
int i=0;
日期时间日期时间;
DownloadStringAsync(新Uri(sURL+sNumber));
wClient.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(客户端\u DownloadStringCompleted);
/*更多代码*/
}

该按钮调用方法Search(),并使用返回的列表填充网格。

您的代码中至少有两个问题:在调用
DownloadStringAsync
之前,您需要订阅
DownloadStringCompleted
——否则,在订阅之前下载可能会完成。此外,由于完成方法非常短,因此可以使用lambda内联完成此操作

其次,您的方法是异步的——它将返回一个
列表
,因为web调用是异步执行的。您必须在完成方法中填充网格,并使方法返回为空。这就是为什么它第二次“工作”的原因——现在返回第一次调用的已完成结果

wClient.DownloadFileCompleted += (sender, e) =>
{
    //you should also do error checking here
    //populate grid 
};
wClient.DownloadStringAsync(new Uri(sURL + sNumber));

您的代码中至少有两个问题:在调用
DownloadStringAsync
之前,您需要订阅
DownloadStringCompleted
——否则,下载可能在订阅之前完成。此外,由于完成方法非常短,因此可以使用lambda内联完成此操作

其次,您的方法是异步的——它将返回一个
列表
,因为web调用是异步执行的。您必须在完成方法中填充网格,并使方法返回为空。这就是为什么它第二次“工作”的原因——现在返回第一次调用的已完成结果

wClient.DownloadFileCompleted += (sender, e) =>
{
    //you should also do error checking here
    //populate grid 
};
wClient.DownloadStringAsync(new Uri(sURL + sNumber));

wClient.DownloadStringAsync(新Uri(sURL+sNumber))方法在执行该方法中的所有代码后执行

1) 首先,
doc
为空

2) 然后调用
wClient.DownloadStringAsync(新Uri(sURL+sNumber))但不执行

3) 然后返回单据(仍然为空)

4) 在所有这些之后,
wClient.DownloadStringAsync(新Uri(sURL+sNumber))并填写
单据

这就是为什么当你第二次按下搜索按钮时,网格被完全填满了

注意:在调用异步方法之前,必须注册
下载StringCompletedEventHandler
。您只需注册此事件处理程序一次,即在构造函数中,因为每次执行此方法时都要添加事件处理程序。因此,如果您按搜索按钮5次,网格将填充5次,尽管您没有注意到

一种解决办法是:

这是密码

   public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        doc = e.Result;
    }

    public static List<Row> Search(string number)
    {
        WebClient wClient = new WebClient();

        sNumber = number;
        int i = 0;
        DateTime datetime;

        wClient.DownloadStringAsync(new Uri(sURL + sNumber));
        wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
              /*More code*/
     }
   public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result != null)
        {
            //populate grid view
        }
    }

    public static void Search(string number)
    {
        WebClient wClient = new WebClient();

        sNumber = number;
        int i = 0;
        DateTime datetime;

        wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); //this should be added in the constructor, so it would only be added once
        wClient.DownloadStringAsync(new Uri(sURL + sNumber));
     }

wClient.DownloadStringAsync(新Uri(sURL+sNumber))方法在执行该方法中的所有代码后执行

1) 首先,
doc
为空

2) 然后调用
wClient.DownloadStringAsync(新Uri(sURL+sNumber))但不执行

3) 然后返回单据(仍然为空)

4) 在所有这些之后,
wClient.DownloadStringAsync(新Uri(sURL+sNumber))并填写
单据

这就是为什么当你第二次按下搜索按钮时,网格被完全填满了

注意:在调用异步方法之前,必须注册
下载StringCompletedEventHandler
。您只需注册此事件处理程序一次,即在构造函数中,因为每次执行此方法时都要添加事件处理程序。因此,如果您按搜索按钮5次,网格将填充5次,尽管您没有注意到

一种解决办法是:

这是密码

   public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        doc = e.Result;
    }

    public static List<Row> Search(string number)
    {
        WebClient wClient = new WebClient();

        sNumber = number;
        int i = 0;
        DateTime datetime;

        wClient.DownloadStringAsync(new Uri(sURL + sNumber));
        wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
              /*More code*/
     }
   public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result != null)
        {
            //populate grid view
        }
    }

    public static void Search(string number)
    {
        WebClient wClient = new WebClient();

        sNumber = number;
        int i = 0;
        DateTime datetime;

        wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); //this should be added in the constructor, so it would only be added once
        wClient.DownloadStringAsync(new Uri(sURL + sNumber));
     }

DownloadStringAsync
可能在设置
DownloadStringCompleted
之前完成。您应该始终在调用异步操作之前设置事件处理程序。
DownloadStringAsync
可能在设置
DownloadStringCompleted
之前完成。在调用异步操作之前,应该始终设置事件处理程序。