与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信
生活随笔
收集整理的這篇文章主要介紹了
与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文:與眾不同 windows phone (29) - Communication(通信)之與 OData 服務通信
[索引頁]
[源碼下載]
作者:webabcd
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之通信
- 與 OData 服務通信
示例
1、服務端(采用 Northwind 示例數據庫)
NorthwindService.svc.cs
2、客戶端(ViewModel 層)
MyViewModel.cs
3、客戶端(View 層)
App.xaml
App.xaml.cs
private void Application_Activated(object sender, ActivatedEventArgs e){// 從 tombstone 狀態返回,則恢復相關數據,參見:“調用 OData 服務”if (!e.IsApplicationInstancePreserved){if (PhoneApplicationService.Current.State.ContainsKey("categoriesState")){var vm = App.Current.Resources["ODataViewModel"] as MyViewModel;string categoriesState = PhoneApplicationService.Current.State["categoriesState"] as string;vm.RestoreState(categoriesState);}}}private void Application_Deactivated(object sender, DeactivatedEventArgs e){// 保存相關數據,參見:“調用 OData 服務”var vm = App.Current.Resources["ODataViewModel"] as MyViewModel;PhoneApplicationService.Current.State["categoriesState"] = vm.GetState();}Demo.xaml
<phone:PhoneApplicationPage x:Class="Demo.Communication.ODataClient.Demo"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"FontFamily="{StaticResource PhoneFontFamilyNormal}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"shell:SystemTray.IsVisible="True"DataContext="{Binding Source={StaticResource ODataViewModel}}"xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"><phone:PhoneApplicationPage.Resources><DataTemplate x:Key="item"><ListBox ItemsSource="{Binding Products}"><ListBox.ItemTemplate><DataTemplate><Grid Width="460" HorizontalAlignment="Center"><Grid.ColumnDefinitions><ColumnDefinition Width="5*" /><ColumnDefinition Width="5*" /></Grid.ColumnDefinitions><StackPanel Orientation="Horizontal" VerticalAlignment="Center"><TextBlock Text="{Binding ProductName}" /><TextBlock Text="{Binding UnitPrice}" Margin="10 0 0 0" /></StackPanel><StackPanel Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right"><Button x:Name="btnUpdate" Content="更新" Click="btnUpdate_Click" /><Button x:Name="btnDelete" Content="刪除" Click="btnDelete_Click" /></StackPanel></Grid></DataTemplate></ListBox.ItemTemplate></ListBox></DataTemplate><DataTemplate x:Key="header"><TextBlock Text="{Binding CategoryName}" /></DataTemplate></phone:PhoneApplicationPage.Resources><Grid x:Name="LayoutRoot" Background="Transparent"><controls:Pivot x:Name="pivot"Title="產品列表" ItemTemplate="{StaticResource item}" HeaderTemplate="{StaticResource header}"ItemsSource="{Binding Categories}"></controls:Pivot></Grid><phone:PhoneApplicationPage.ApplicationBar><shell:ApplicationBar Mode="Default" IsVisible="True"><shell:ApplicationBarIconButton x:Name="btnAd" IconUri="/ApplicationBarDemo/Assets/appbar.add.rest.png" Text="添加" Click="btnAd_Click" /></shell:ApplicationBar></phone:PhoneApplicationPage.ApplicationBar></phone:PhoneApplicationPage>Demo.xaml.cs
/** 調用 OData 服務的 View 層*/using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls;using Demo.Communication.ODataClient.ViewModel; using Demo.NorthwindContext;namespace Demo.Communication.ODataClient {public partial class Demo : PhoneApplicationPage{public Demo(){InitializeComponent();this.Loaded += new RoutedEventHandler(Demo_Loaded);}void Demo_Loaded(object sender, RoutedEventArgs e){// 加載數據var vm = App.Current.Resources["ODataViewModel"] as MyViewModel;vm.LoadData();}private void btnDelete_Click(object sender, RoutedEventArgs e){var product = (sender as Button).DataContext as Product;// 刪除指定的產品數據var vm = App.Current.Resources["ODataViewModel"] as MyViewModel;vm.DeleteProduct(product);}private void btnUpdate_Click(object sender, RoutedEventArgs e){// 更新指定的產品數據var product = (sender as Button).DataContext as Product;product.UnitPrice = 8.88m;var vm = App.Current.Resources["ODataViewModel"] as MyViewModel;vm.UpdateProduct(product);}private void btnAd_Click(object sender, EventArgs e){// 在指定的類別下添加一個新的產品數據var category = pivot.SelectedItem as Category;Product product = new Product();product.CategoryID = category.CategoryID;product.ProductName = "測試" + new Random().Next(10, 99);product.UnitPrice = 6.66m;product.Discontinued = false;var vm = App.Current.Resources["ODataViewModel"] as MyViewModel;vm.AddProduct(product);}} }
OK
[源碼下載]
總結
以上是生活随笔為你收集整理的与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信支付支付方式优先怎么设置
- 下一篇: 三星移动端浏览器正测试集成 ChatGP