Skip to main content
 首页 » 编程设计

c#之Windows 应用商店应用程序数据将 TextBlock 绑定(bind)到来自 Web 服务的对象

2024年01月01日49artech

我有一个 Web 服务,它返回一个包含 FirstName、LastName、EmailAddress 等的用户对象...我正在构建一个 Windows 8 Store 应用程序,我需要将 Web 服务中的对象绑定(bind)到我的应用程序控件。我可以调用 Web 服务,然后在我期望的用户对象中取回值。我不知道如何将信息绑定(bind)到 Windows 8 应用商店应用程序控件。我的第一次尝试是将名字绑定(bind)到名为 txtUserName 的 TextBlock:

这是文本 block 所在的网格的 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 
            } 
        }; 
    } 
} 

当前用户类:

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); 
 
        } 
    } 

我想我遗漏了一些明显的东西。我已经完成了一些教程,但找不到我缺少的东西。

请您参考如下方法:

发现我需要在代码后面进行数据绑定(bind),因为用户需要作为对象传递:

user = (User)e.Parameter; 
            CurrentUserDataSource cuDS = new CurrentUserDataSource(user); 
            MainMenuGrid.DataContext = cuDS; 

来自 http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/cbb3bed3-c883-40ff-b2aa-5b2aacb4bf27/