2011-03-28 64 views
1

我一直在嘗試將項目添加到comboxbox,但似乎無法獲得文件後面的代碼來識別添加到xaml的組合框。我很確定我缺少一些簡單的東西。基本上這裏的xaml展示了一個空的組合框。後面的代碼執行服務,等待json返回並反序列化它。不幸的是我不能讓Combobox在Silverlight中不可見代碼背後

XAML:

<navigation:Page x:Class="Growing.Views.Room" 
     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" 
     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
     d:DesignWidth="950" d:DesignHeight="480" 
     Title="Home" Style="{StaticResource PageStyle}" DataContext="{Binding}" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 
<Grid x:Name="LayoutRoot" ShowGridLines="True" Background="#FF631C00"> 
    <Grid.ColumnDefinitions> 
    </Grid.ColumnDefinitions> 
    <Rectangle Height="298" HorizontalAlignment="Left" Margin="195,94,0,0" Name="rect" Stroke="Black" StrokeThickness="2" VerticalAlignment="Top" Width="582" Fill="#FFAAAAAA" RadiusY="0.25" RadiusX="0.25" /> 
    <sdk:Label Height="38" HorizontalAlignment="Left" Margin="387,160,0,0" Name="label1" VerticalAlignment="Top" Width="203" Content="Select a Room" FontSize="24" FontWeight="Bold" /> 
    <sdk:Label Height="18" HorizontalAlignment="Left" Margin="312,240,0,0" Name="label2" VerticalAlignment="Top" Width="69" Content="Area:" FontSize="14" /> 
    <ComboBox x:Name="RoomAreas" Height="23" HorizontalAlignment="Left" Margin="418,235,0,0" VerticalAlignment="Top" Width="209" /> 
</Grid> 

背後的代碼:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Runtime.Serialization.Json; 
using System.ServiceModel.Web; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Navigation; 
using Growing.DataConnectionRef; 

namespace Growing.Views 
{ 
    public partial class Room : Page 
    { 
     public Room() 
     { 
      InitializeComponent(); 
      //Asynchronously call the EndReceive Web Service to change the status of an existing open lot record 
      WebClient GRService = new WebClient(); 
      GRService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GRService_DownloadStringCompleted); 
      GRService.DownloadStringAsync(new Uri("/servicestack/GetAreas", UriKind.Relative)); 
     } 

     static void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      List<Area> dataList = new List<Area>(); 
      MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result)); 
      DataContractJsonSerializer ser = new DataContractJsonSerializer(dataList.GetType()); 
      dataList = ser.ReadObject(memoryStream) as List<Area>; 
      memoryStream.Close(); 
      RoomAreas.ItemSource = dataList; 

     } 

    } 
} 

在RoomAreas.ItemSource我得到一個錯誤An object reference is required for the non-static field, method, or property 'Growing.Views.Room.RoomAreas'

很抱歉,如果這是很難跟隨。任何人有任何想法可能會發生在這裏?

預先感謝您!

回答

3

讓GRService_DownloadStringCompleted方法非靜態

void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      List<Area> dataList = new List<Area>(); 
      MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result)); 
      DataContractJsonSerializer ser = new DataContractJsonSerializer(dataList.GetType()); 
      dataList = ser.ReadObject(memoryStream) as List<Area>; 
      memoryStream.Close(); 
      RoomAreas.ItemSource = dataList; 

     } 
+0

我想這和它的工作。感謝您的答覆! – kereberos 2011-03-28 20:06:44

0

嘗試增加的ItemsSource = 「{結合}」 到ComboBox屬性中XAML中