技术开发 频道

Silverlight 3 RIA服务编程中使用自定义方法及服务操作

 Silverlight用户界面及基本后台代码编程

 其实,从分层角度来看,就客户端Silverlight项目这边来看,也可进一步分为若干子层。例如,.xaml文件可看作最上面的展示层,相应的后台代码文件.xaml.cs则又是一层。

 Silverlight 3项目自动生成的主页面文件为MainPage.xaml。现在,打开此文件,并输入如下标记代码:

 <UserControl

 xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="S3RIACustomSample.MainPage"

 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

 mc:Ignorable="d" d:DesignWidth="Auto" d:DesignHeight="Auto">

 <Grid x:Name="LayoutRoot">

 <StackPanel Orientation="Vertical">

 <data:DataGrid IsReadOnly="True" Name="productsGrid" Height="Auto" Width="Auto"

 SelectionChanged="DataGrid_SelectionChanged"/>

 <StackPanel Orientation="Horizontal">

 <TextBlock Text="Competitor's price" Margin="2"/>

 <TextBlock x:Name="compPrice" Margin="2"/>

 </StackPanel>

 <StackPanel Orientation="Horizontal">

 <Button Click="Button_Click" Content="Discount" x:Name="discountButton" Margin="2"/>

 <TextBox x:Name="discountPercent" Text="10" Margin="2,2"/>

 <TextBlock Text="%" Margin="2,5,0,2"/>

 </StackPanel>

 </StackPanel>

 </Grid>

 </UserControl>

 上面的XAML代码非常简单,不再赘述。

 相应的后台代码文件MainPage.xaml.cs的内容如下所示:

 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 System.Windows.Ria.Data;

 using RIA=S3RIACustomSample.Web;

 namespace S3RIACustomSample

 {

 public partial class MainPage : UserControl

 {

 RIA.Catalog catalog = new RIA.Catalog();//创建Catalog对象实例

 public MainPage()

 {

 InitializeComponent();

 productsGrid.ItemsSource = catalog.Products;

 var query = from p in catalog.GetProductQuery()

 where p.ListPrice > 3000 select p;

 catalog.Load(query);//把满足要求的产品数据加载到DataGrid控件

 }

 private void Button_Click(object sender, RoutedEventArgs e)

 {

 RIA.Product selectedProduct = (RIA.Product)productsGrid.SelectedItem;

 if (selectedProduct != null) {

 //int percentage = int.Parse(discountPercent.Text);

 //selectedProduct.DiscountProduct(percentage);

 //catalog.SubmitChanges();

 }

 }

 private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) {

 RIA.Product selectedProduct = (RIA.Product)productsGrid.SelectedItem;

 if (selectedProduct != null) {

 //catalog.GetCompetitorsPrice(selectedProduct,

 // (invokeOperation) =>

 // {

 //     compPrice.Text = invokeOperation.Value.ToString();

 // }, null);

 }

 }

 }

0
相关文章