2012-11-04 31 views
2

我有一個Web服務,返回一個用戶對象的名字,姓氏,EmailAddress等......我建立一個Windows 8商店應用程序,我需要綁定對象從Web服務到我的應用程序控件。我可以調用Web服務,並將我期望的User對象中的值返回。我無法弄清楚如何將信息綁定到Windows 8 Store應用程序控件。我第一次嘗試是將名字綁定到一個名爲txtUserName的TextBlock:Windows應用商店應用DataBind TextBlock從對象從Web服務

這裏是面向網格的文本塊是在XAML:

<Grid x:Name="MainMenuGrid"> 
      <Grid.DataContext> 
       <local:CurrentUserDataSource/> 
      </Grid.DataContext> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="100"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 
      <TextBlock TextWrapping="Wrap" Text="Welcome to Cuisinier Creatif"  VerticalAlignment="Center" FontSize="64" Margin="5" HorizontalAlignment="Center"/> 
      <VariableSizedWrapGrid HorizontalChildrenAlignment="Stretch" ItemHeight="195" ItemWidth="200" MaximumRowsOrColumns="2" Orientation="Vertical" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"> 
       <Button x:Name="btnMyRecipeBooks" Content="My Reipie Books" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/> 
       <Button x:Name="btnewRecipeBook" Content="New Recipe Book" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/> 
       <Button x:Name="btnMyRecipes" Content="My Reipies" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/> 
       <Button x:Name="btnNewRecipe" Content="New Recipe" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/> 
       <Button x:Name="btnShoppingList" Content="Shopping List" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/> 
       <Button x:Name="btnMyInformation" Content="My Information" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2" /> 
      </VariableSizedWrapGrid> 
      <TextBlock x:Name="txtUserName" TextWrapping="Wrap" Text="{Binding ThisUser[0].FirstName}" VerticalAlignment="Center" FontSize="64" HorizontalAlignment="Left" Margin="5" Width="50" Height="150"/> 
     </Grid> 

類CurrentUserDataSource類:

class CurrentUserDataSource 
{ 
    public CurrentUserDataSource() 
    { 
    } 
    public CurrentUserDataSource(User user) 
    { 
     LoadUser(user); 
    } 
    private ObservableCollection<CurrentUser> currentUser; 

    public ObservableCollection<CurrentUser> ThisUser 
    { 
     get { return currentUser; } 
     set { currentUser = value; } 
    } 
    private void LoadUser(User user) 
    { 
     currentUser = new ObservableCollection<CurrentUser> 
     { 
      new CurrentUser 
      { 
       FirstName = user.FirstName, 
       LastName = user.LastName, 
       EmailAddress = user.LastName, 
       LastSignIn = user.LastSignIn, 
       SignUpdate = user.SignUpdate 
      } 
     }; 
    } 
} 

CurrentUserClass:

class CurrentUser:INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     private string _firstname; 
     private string _lastname; 
     private DateTime _lastsignin; 
     private DateTime _signupdate; 
     private string _emailaddress; 

     public CurrentUser() 
     { 

     } 
     public CurrentUser(User user) 
     { 
      this.FirstName = user.FirstName; 
      this.LastName = user.LastName; 
      this.LastSignIn = user.LastSignIn; 
      this.SignUpdate = user.SignUpdate; 
      this.EmailAddress = user.EmailAddress; 
     } 
     private void NotifyPropertyChanged(string property) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
      } 
     } 
     public string FirstName 
     { 
      get { return this._firstname; } 
      set 
      { 
       _firstname = value; 
       NotifyPropertyChanged("FirstName"); 
      } 
     } 
    } 
} 

代碼在頁面上:

public sealed partial class MainMenu : Page 
    { 
     User usr = new User(); 
     public MainMenu() 
     { 
      this.InitializeComponent(); 

     } 

     /// <summary> 
     /// Invoked when this page is about to be displayed in a Frame. 
     /// </summary> 
     /// <param name="e">Event data that describes how this page was reached. The Parameter 
     /// property is typically used to configure the page.</param> 
     protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      usr = (User)e.Parameter; 
      CurrentUserDataSource cuDS = new CurrentUserDataSource(usr); 

     } 
    } 

我想我錯過了一些明顯的東西。我已經通過一些教程,並找不到我缺少的東西。

回答

相關問題