【问题标题】:Web Service Completed Events not show On Metro Windows 8 App Development在 Metro Windows 8 应用程序开发中未显示 Web 服务已完成事件
【发布时间】:2012-04-20 15:09:09
【问题描述】:

您好,这是我在这里的第一个问题,我创建了一个托管在 Azure 上的 Web 服务......我有一个 Windows 手机客户端应用程序和一个 Windows 8 Metro 客户端应用程序,现在当我连接 wp7 应用程序时,我使用了这些从 windows phone 上的服务获取内容的方法:

        TimeTierBusiness.BusinessesClient client = new TimeTierBusiness.BusinessesClient();

        client.GetAllCompleted += new EventHandler<TimeTierBusiness.GetAllCompletedEventArgs>(client_GetAllCompleted);
        client.GetAllAsync();

为了获得结果,我只需转到 client_GetAllCompleted,如下所示:

    void client_GetAllCompleted(object sender, TimeTierBusiness.GetAllCompletedEventArgs e)
    {
        listNearbyBusinesses.ItemsSource = e.Result;
        myPopup.IsOpen = false;
    }

现在在 Windows 8 Metro 上,没有可以添加的 GetAllCompleted 事件来获得结果,当我在 Windows 8 上调用客户端时,我得到的只是等待的 GetAllAsync() 方法...

任何帮助将不胜感激,因为我现在无法在我的 Metro 应用程序上使用此服务

谢谢:)

好的,所以解决方案是,要创建一个异步方法,请参见下面的代码:

        //My WCF Service Client
        TimeTierBusiness.BusinessesClient bClient = new TimeTierBusiness.BusinessesClient();
        //The list I am going to get from the service
        public List<TimeTierBusiness.BusinessRatingViewModel> listBusinessViewModel;

此方法从服务中异步填充列表

       private async void GetAllAsyc()
    {

        System.Collections.ObjectModel.ObservableCollection<TimeTierBusiness.BusinessRatingViewModel> x = await bClient.GetAllAsync();
        listBusinessViewModel = x.ToList();
        ItemListView.ItemsSource = listBusinessViewModel;
    }

【问题讨论】:

    标签: wcf windows-phone-7 asynchronous wcf-data-services windows-runtime


    【解决方案1】:

    Windows 8(实际上是它附带的 .NET 4.5)具有有史以来最令人讨厌的特性之一。它基于 await/async 关键字,它本质上允许您将异步代码编写为如果是同步的。在工作和宠物项目中,我最终做了大量的异步代码,在看到这个特性之后,我决定将我的下一个孩子作为牺牲献给 .NET 人(因为这个特性就是很好,因为我已经有 3 个孩子了,真的不想再要一个了)。

    它的要点是您编写调用异步服务的代码(在这种情况下)就像您使用常规方法一样,但是您无需挂钩事件以完成,您只需“等待”异步的结果调用并在它下面的行中,继续处理结果 - 编译器做了一些黑魔法(如果你曾经使用过 yield return,它是一种非常相似的黑魔法)并将你的方法变成一系列方法 + 状态机将以正确的顺序调用。

    Read more about it here

    【讨论】:

    • 嘿,谢谢我解决了 :) 非常高兴,我将发布我如何解决它的代码...在您的链接的帮助下 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2012-07-17
    相关资源
    最近更新 更多