使用Observable Collection MVVM绑定Pivot控件(Windows Phone 8)

时间:2013-03-27 03:27:58

标签: c# silverlight mvvm windows-phone-8 inotifypropertychanged

我是WP8&的新手。 MVVM。我创建了wp8应用程序,一旦用户登录就会请求各种数据。我只是无法动态创建我的枢轴标题,我不知道是不是因为我在绑定中做了一些事情, INotifyPropertyChanged,两者或其他什么!!

这是我到目前为止所做的:

我在App.cs中定义了一个全局MainViewModel,它将存储登录时返回的所有数据。

一旦登录成功并且数据已加载到MainViewModel中,我将其重定向到包含数据透视控件的测试页面,并且我正在尝试动态创建数据透视表项。

这是我的测试页面的xaml,即MainPivotPage.xaml和我的MainPivotViewModel被初始化,因为它被定义为本地资源并被设置为Pivot控件的datacontext,我不知道我是否正在这样做是的,但我将“Name”属性分配给PivotItem的Header,它是存储在我的可观察集合Pivots中的对象。属性Name是我在名为Pivot的类中包含的2个属性之一,它包含PivotId和Name。

<phone:PhoneApplicationPage
    x:Class="TestApp.Views.MainPivotPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:viewModel="clr-namespace:TestApp.ViewModels"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">
    <!--LayoutRoot is the root grid where all page content is placed-->

    <phone:PhoneApplicationPage.Resources>
        <viewModel:MainPivotViewModel x:Key="MainPivotViewModel" />
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <!--Pivot Control-->
        <phone:Pivot Title="My Search Options" x:Name="MainPivots" ItemsSource="{Binding Pivots}" DataContext="{StaticResource MainPivotViewModel}">
            <phone:PivotItem Header="{Binding Name}">
                <!--<Grid/>-->
            </phone:PivotItem>
        </phone:Pivot>
    </Grid>
</phone:PhoneApplicationPage>

创建MainPivotViewModel时,我将Pivots observable集合设置为存储在MainViewModel中的同一个可观察集合,该集合包含登录时返回的所有数据。正如您所看到的,我将它分配给属性而不是内部变量,以确保它将触发INotifyPropertyChanged(好吧......,我认为)

public class MainPivotViewModel : BaseViewModel
{
    private ObservableCollection<Pivot> _pivots = null;

    public MainPivotViewModel()
    {
        Pivots = App.MainViewModel.Pivots;            
    }

    public ObservableCollection<Pivot> Pivots
    {
        get
        {
            return _pivots;
        }

        set
        {
            if (_pivots != value) this.SetProperty(ref this._pivots, value);
        }
    }
}

我使用SetProperty函数,该函数包含在我的基类中,用于生成INotifyPropertyChanged事件,并允许我这样做,而无需在每次需要INotifyPropertyChanged事件时设置属性名称。

这是我的BaseView的代码:

public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
    {
        if (object.Equals(storage, value)) return false;

        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我的Pivot类看起来像这样:

public class Pivot: BaseModel
{
    private int _pivotId;
    private string _name = string.Empty;

    public Pivot()
    {

    }

    public Pivot(int pivotId, string name)
    {
        PivotId = pivodId;
        Name = name;
    }

    public int PivotId
    {
        get { return _pivotId; }
        set { if (_pivotId != value) this.SetProperty(ref this._pivotId, value); }
    }

    public string Name
    {
        get { return _name; }
        set { if (_name != value) this.SetProperty(ref this._name, value); }
    }
}

您可能会注意到这个是从BaseModel继承的。这与BaseViewModel中的代码完全相同,但我想将两者分开。我不确定我的Pivot课程是否需要这个,但我正在尝试不同的场景并暂时保留它。

我不知道我做错了什么,但无论我尝试什么,我都无法将“Name”属性显示为Header的文本。我很确定MainPivotViewModel在被指定为本地资源时被初始化,因为我正在正确地调用我的构造函数,然后初始化我的可观察集合,但这就是它的目的。

但它绝对没有显示任何内容!

我注意到的另一件事是当我在BaseViewModel类中的OnPropertyChanged方法的“Set”中放置断点时,eventHandler始终为null,无论我假设不应该是什么情况,但我不能看看我做错了什么。

我有很多关于stackoverflow和其他文章的文章,我只是看不出我做错了什么?有人有任何想法吗?

感谢。

3 个答案:

答案 0 :(得分:3)

问题已解决!!!

我的代码一直都是正确的,但XAML不是!

我猜是陡峭而痛苦的学习曲线!无论如何,我在找到一篇关于stackoverflow的文章后找到了一个解决方案,它基本上向我展示了我编写xaml的方式是不恰当的。

我会说实话,我不明白为什么这不会按照定义的方式工作但简而言之,我必须使用HeaderTemplate和ItemTemplate才能在绑定到ViewModel时正确显示数据! / p>

以下是帖子:Databound pivot is not loading the first PivotItem in Windows Phone 8

答案 1 :(得分:2)

你回答错了,你的代码错了。

错误1:

    set
    {
        if (_pivots != value) this.SetProperty(ref this._pivots, value);
    }

在这里,如果更改属性或变量,绑定将丢失,则无关紧要。

错误2:从ItemsControl派生的所有UIElements都忽略了INotifyPropertyChanged,因为它不会仅仅更新ItemsSource DataContext。

工作示例

    public ObservableCollection<string> LstLog { get; set; }
    private ObservableCollection<string> _lstContent = new ObservableCollection<string>();
    public ObservableCollection<string> LstContent
    {
        get
        {
            LstLog.Add("get");
            return _lstContent;
        }
        set
        {
            LstLog.Add("set");
            _lstContent = value;
        }
    }
    public MainWindow()
    {
        LstLog = new ObservableCollection<string>();

        InitializeComponent();
        this.DataContext = this;
    }

    private void Add_Click(object sender, RoutedEventArgs e)
    {
        LstContent.Add("Value added");
    }

    private void New_Click(object sender, RoutedEventArgs e)
    {
        _lstContent = new ObservableCollection<string>();
    }

    private void NewBind_Click(object sender, RoutedEventArgs e)
    {
        _lstContent = new ObservableCollection<string>();
        listObj.ItemsSource = _lstContent;
    }

    private void NewProp_Click(object sender, RoutedEventArgs e)
    {
        LstContent = new ObservableCollection<string>();
    }

    private void NewPropBind_Click(object sender, RoutedEventArgs e)
    {
        LstContent = new ObservableCollection<string>();
        listObj.ItemsSource = LstContent;
    }

和ui

<Grid DataContext="{Binding}">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition Height="25" />
    </Grid.RowDefinitions>

    <ItemsControl Grid.Row="0" Name="logObj" ItemsSource="{Binding Path=LstLog}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    <ItemsControl Grid.Row="1" Name="listObj" ItemsSource="{Binding Path=LstContent}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    <StackPanel Grid.Row="2" Orientation="Horizontal">
        <Button Name="Add" Content="Add" Click="Add_Click"/>
        <Button Name="New" Content="New" Click="New_Click"/>
        <Button Name="NewBind" Content="New Bind" Click="NewBind_Click"/>
        <Button Name="NewProp" Content="New Prop" Click="NewProp_Click"/>
        <Button Name="NewPropBind" Content="New Prop Bind" Click="NewPropBind_Click"/>
    </StackPanel>
</Grid>

LstLog只是为了查看事件列表,如果点击add它会更新两个列表但是如果点击NewNew Prop绑定是丢失,直到您更新New BindNew Prop Bind

希望这会澄清XAML List事件的重复出现问题。

PS:这是在WPF中,但在WP8和Windows应用商店中的工作方式相同。

答案 2 :(得分:1)

这一切对我来说都很好,所以我认为App.MainViewModel.Pivots在调用构造函数时为null或为空(因此Pivot控件为空),并且最终创建了一个新的Pivots实例实例化MainPivotViewModel后,在MainViewModel中。

您是否尝试在MainPivotViewModel.Pivots的getter中设置断点以确认是否有某些项?