有关如何在 XAML Metro 应用程序中使用 WCF RIA 服务的任何示例? [英] Any sample on how to use WCF RIA Services in XAML metro app?

查看:16
本文介绍了有关如何在 XAML Metro 应用程序中使用 WCF RIA 服务的任何示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人尝试在基于 XAML 的 Metro 应用程序中使用 WCF RIA 服务.如果您有任何博客或示例,请分享.

I wonder if someone have tried using WCF RIA Services in XAML based metro application. If you have any blog or sample please share.

推荐答案

事实上我做到了,这就是诀窍:)

As a matter of fact I did and here's the trick :)

我向我的 WCF 服务添加了一个服务参考",公开了一个 ADO.NET 实体框架 模型.问题是在 XAML/C# Metro 应用程序中,执行以下代码失败:

I added a "Service Reference" to my WCF service exposing an ADO.NET Entity Framework model. The problem is that in a XAML/C# Metro app, executing the following code fails:

SampleEntities ctx = ((App)Application.Current).Context;
var query = from p in ctx.Products
              where p.Name == name
              select p;

foreach (Product p in query) /** this line fails **/
{
    // do stuff
}

这是您在运行时会遇到的异常:

Here's the exception you'll get at runtime:

Silverlight 无法让您直接枚举数据服务查询.这是因为枚举会自动发送一个对数据服务的同步请求.因为只有 Silverlight支持异步操作,您必须改为调用BeginExecute 和 EndExecute 方法获取查询结果支持枚举."}[System.NotSupportedException]: {"Silverlight 不允许您直接枚举数据服务查询.这是因为枚举自动向数据发送同步请求服务.因为 Silverlight 只支持异步操作,您必须改为调用 BeginExecute 和 EndExecute 方法获取支持枚举的查询结果."

"Silverlight does not enable you to directly enumerate over a data service query. This is because enumeration automatically sends a synchronous request to the data service. Because Silverlight only supports asynchronous operations, you must instead call the BeginExecute and EndExecute methods to obtain a query result that supports enumeration."} [System.NotSupportedException]: {"Silverlight does not enable you to directly enumerate over a data service query. This is because enumeration automatically sends a synchronous request to the data service. Because Silverlight only supports asynchronous operations, you must instead call the BeginExecute and EndExecute methods to obtain a query result that supports enumeration."

啊,这真是太好了!

如例外中所述,您需要异步服务器调用,就像在 Silverlight 中一样.

As stated in the exception, you need your server calls to be asynchronous, just as in Silverlight.

这是一个示例,说明如何在 C# Metro 应用程序中使用 WCF RIA 服务,以老派方式进行:

Here's a sample of how you can consume a WCF RIA Service in a C# Metro App doing it the old school way:

(...)
var query = from p in ctx.Products
            where p.Name == Name
            select p;

((DataServiceQuery<Product>)query).BeginExecute(OnLoadItemsCompleted, query);
(...)

private void OnLoadItemsCompleted(IAsyncResult result)
{
    var query = result.AsyncState as DataServiceQuery<Product>;
    IEnumerable<Product> response = query.EndExecute(result);

    foreach (Product o in response)
    {
         // Do stuff
    }
}

现在使用 .NET 4.5 及其新的 await &async 关键字,您可以获得相同的结果,同时避免您的代码被所有这些小回调方法分块.

Now using .NET 4.5 and its new await & async keywords, you can get the same result whilst avoiding your code to be chunked with all those little callback methods.

例子:

async void GetProducts()
{
    SampleEntities ctx = ((App)Application.Current).Context;
    var query = from p in ctx.Products
                  where p.Name == name
                  select p;
    DataServiceQuery<Product> dqs = (DataServiceQuery<Product>)(query);
    TaskFactory<IEnumerable<Product>> tf = new TaskFactory<IEnumerable<Product>>();
    myListView.ItemsSource = await tf.FromAsync(dqs.BeginExecute(null, null), 
                               iar => dqs.EndExecute(iar));
}

使用最后两种方法中的任何一种对我来说都很好:)

Using either of the last two methods worked fine for me :)

这篇关于有关如何在 XAML Metro 应用程序中使用 WCF RIA 服务的任何示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆