2

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

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

        }
    }

我想我错过了一些明显的东西。我已经阅读了一些教程,但找不到我所缺少的。

4

1 回答 1

0

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

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/

于 2012-11-09T02:54:51.980 回答