Resumé

WCF DataService in Windows Phone 7

Cristian Merighi () 0,00

Poche righe di codice esemplificative su come consumare un DataContext proxy in modalità asincrona.
Questo articolo è da considerarsi obsoleto. Alcune funzionalità potrebbero non essere più disponibili e non è possibile aggiungere commenti.

Come consumare i dati esposti da un WCF DataService in Windows Phone 7 (ovvero in Silverlight)?
A dire il vero è piuttosto semplice e non è forse questo un argomento particolarmente originale. Scrivo quest'articolo soprattutto per fissare le righe di codice chiave per eseguire tale operazione e averle comode per il futuro.
(La sintassi è pressoché identica per il mondo WPF, ndr.)

Lettura dei dati (C#):
  1. // DataContext proxy instance:
  2. var proxy = new ToolSpare.ToolSpareContext(new Uri("http://myserver/mydataservice.svc/"));
  3. // Build up a query to be executed (cast it as a System.Data.Services.Client.DataServiceQuery) then...
  4. var query = proxy.MovementSet.Where(m => !m.IsIncome).OrderByDescending(m => m.Amount).Take(20) as DataServiceQuery<ToolSpare.Movement>;
  5. // ...execute it asynchronously
  6. query.BeginExecute((asyncResult) =>
  7. {
  8.     // use state token to get the results:
  9.     var state = asyncResult.AsyncState as
  10.         DataServiceQuery<ToolSpare.Movement>;
  11.     var list = state.EndExecute(asyncResult).ToList();
  12.     list.ForEach(outcome => Dispatcher.BeginInvoke(() =>
  13.     {
  14.         // populate some collection in the ViewModel (execute this code in the UI Thread)
  15.         var item = new ItemViewModel { LineOne = outcome.Amount.ToString("c2"), LineTwo = outcome.Caption, LineThree = string.Format("{0:D} {0:t}", outcome.Date) };
  16.         this.Items.Add(item);
  17.     }));
  18. }, /* state -> */query);

Lettura dati, in alternativa (C#):
  1. // using System.Data.Services.Client.DataServiceCollection
  2. DataServiceCollection<ToolSpare.Movement> outcomes = new DataServiceCollection<ToolSpare.Movement>(proxy);
  3. outcomes.LoadCompleted += (s, e) => // inline delegate, do not try this at home! Always detach handlers :)
  4. {   
  5.     // ensure execution in the UI-Thread;
  6.     foreach (var outcome in outcomes)
  7.     {
  8.         var item = new ItemViewModel { LineOne = outcome.Amount.ToString("c2"), LineTwo = outcome.Caption, LineThree = string.Format("{0:D} {0:t}", outcome.Date) };
  9.         this.Items.Add(item);
  10.     }
  11. };
  12. outcomes.LoadAsync(/* query from above -> */query);

Scrittura dei dati (C#):
  1. // changes have been made to the model, DataContext.UpdateObject(...)
  2. proxy.BeginSaveChanges((asyncResult)
  3.     => this.Dispatcher.BeginInvoke(()=>
  4. {
  5.     // execute code in the UI Thread...
  6.     var ctx = asyncResult.AsyncState as ToolSpare.ToolSpareContext;
  7.     ctx.EndSaveChanges(asyncResult);
  8.     // do something else...
  9. }, null), /* state -> */proxy);

Spero possa essere stato d'aiuto.

Take care. Bye.

feedback
 

Syndicate

Autore

Cristian Merighi facebook twitter google+ youtube

Ultimi articoli

Top rated

Archivio

Dove sono?

Autore

Cristian Merighi facebook twitter google+ youtube

Le mie letture

Feeds