FLASH論壇-Flash之神魂顛倒Microsoft 應用技術區Microsoft Silverlight 銀光專區 → [轉帖]銀光+LINQ+WCF 範例(四)

MSDN 研討會資料下載 熱門租屋行情 網路行銷秘訣大公開 磷蝦油=比魚油更強 Microsoft Silverlight
墨水匣、碳粉匣 - 天天都便宜 888Boss 創業加盟網 碳粉匣、墨水匣 - 天天都特價 減肥診所 雷射抽脂
ASP.NET完全攻略與快速上手 買墨水送神幣喔 抽脂 近視雷射手術 電波拉皮
網路創業 SEO 借錢 投影機 公仔
植牙 貸款 白蟻 氣球 眼袋
虛擬主機神魂特惠 大陸新娘 NCCU集中營 網頁設計 愛情城市
微軟MIX08 Taipei 落健 液晶電視

  共有1068人閱讀過本文章展開列印

主題:[轉帖]銀光+LINQ+WCF 範例(四)

帥哥喲,離線,有人找我嗎?
ruok
  1樓 個人化首頁 | 個人資料 | 搜尋 | EMAIL | 首頁 | |

加到: FunP 書籤加到: 黑米書籤加到: MyShare 書籤加到: 美味書籤加到: Furl  書籤加到: YaHoo 分享書籤加到: Google 書籤加到: UDN 書籤加到: Technorati 書籤



加好友 悄悄話 神魂小飛俠
等級:論壇游俠 文章:298 經驗:798 威望:0 精華:0 註冊:2006-11-22 13:05:00
[轉帖]銀光+LINQ+WCF 範例(四)  發表心情 Post By:2008-7-4 17:01:00



Placing controls in the top row

I want to place a Textblock (for the prompt) and TextBox (for input) and a button in the top row. The easiest way to do so is with a stack panel, and I'll surround it all with a border to set it off from the results.

<Border BorderBrush="Black" BorderThickness="2" Grid.Row="1" Grid.Column="1"/>

<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal">

    <TextBlock Text="Last name to search for: " VerticalAlignment="Bottom"

      FontSize="18" Margin="15,0,0,0" />

    <TextBox x:Name="LastName" Width="250" Height="30" Margin="2,0,0,4"

      VerticalAlignment="Bottom"/>

    <Button x:Name="Search" Width="75" Height="30"

     Margin="20,0,0,4" Content="Search"

    VerticalAlignment="Bottom"  Background="Blue" FontWeight="Bold"

     FontSize="14" />

</StackPanel>

Finally, drag a DataGrid from the Toolbox onto the XAML.

<my:DataGrid x:Name="theDataGrid" AlternatingRowBackground="Beige" AutoGenerateColumns="True" Width="700" Height="500" Grid.Row="2" Grid.Column="1" CanUserResizeColumns="True"  />

You'll notice that it is given the prefix my and that a new namespace is declared to support it,

xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

  

Write the Event Handler for the Search Button

When the user clicks the search button we want to pick up the text in the Text box and give it to the web service, and get back a collection of customers. Let's set up the boilerplate event handling code in page.xaml.cs,

public Page()

{

    InitializeComponent();

    Loaded += new RoutedEventHandler(Page_Loaded);

}

 

void Page_Loaded(object sender, RoutedEventArgs e)

{

    Search.Click += new RoutedEventHandler(Search_Click);

}

 

void Search_Click(object sender, RoutedEventArgs e)

{

   

}

Call the Service Asynchronously

The only way to call a web service from Silverlight is asynchronously (which is fair as it is running in a browser and can't afford to block!)

The first task is to get a reference to the Web Service's Service1Client member. You can examine this in the object browser to see that it is this object that has the Asynchronous methods we'll need,

Figure 3-14. The Web Service client seen in the object browser (Click to view full-size image)

(image slightly abridged to save space)

We assign the Service1Client to the local object webService,

void Search_Click(object sender, RoutedEventArgs e)

{

   ServiceReference1.Service1Client webService =

    new SQLData.ServiceReference1.Service1Client();

We then use webService to set up an event handler for the method that will be called when the GetCustomersByLastNameCompleted event is called 

webService.GetCustomersByLastNameCompleted +=

    new EventHandler<SQLData.ServiceReference1.

    GetCustomersByLastNameCompletedEventArgs>

    (webService_GetCustomersByLastNameCompleted);

Finally, we make the asynchronous call

webService.GetCustomersByLastNameAsync(LastName.Text);

}

When the service completes, the GetCustomersByLastNameCompleted event is raised, and our method is invoked. The carefully constructed list of Customers is stashed in e.Result which we assign to the DataGrid's ItemSource property and all the bindings now have a source to bind to.

void webService_GetCustomersByLastNameCompleted(

    object sender,

    SQLData.ServiceReference1.GetCustomersByLastNameCompletedEventArgs e)

{

 

    theDataGrid.ItemsSource = e.Result;

}

   

Figure 3-15. The Running Program (Click to view full-size image)



神魂大俠徒弟~~神魂小飛俠
近視雷射手術 支持(0中立(0反對(0回到頂部