我是WP8的新手& MVVM。我創建了wp8應用程序,它在用戶登錄後請求各種數據位。我只是無法讓我的pivot標題動態創建,我不知道是否因爲我在綁定中做了某些事情, INotifyPropertyChanged,兩者或別的東西!使用Observable Collection MVVM綁定樞軸控件(windows phone 8)
這是我迄今所做的:
我得在App.cs定義的全局MainViewModel將存儲在用戶登錄時都返回的數據。
一旦登錄成功並且數據已被加載到MainViewModel中,我將其重定向到包含數據透視控件的測試頁面,並試圖動態創建數據透視項目。
這是我的測試頁的xaml,即MainPivotPage.xaml和我的MainPivotViewModel被初始化,因爲它被定義爲本地資源,並且設置爲數據透視控件的數據上下文,我不知道我是否在做這個權利,但我將「名稱」屬性分配給PivotItem的Header,這是存儲在我的可觀察集合Pivots中的對象。屬性Name是我在一個名爲Pivot的類中包含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,我的支點觀察集合設置爲存儲在我的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事件時都這樣做。
這是我的基本視點代碼:
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));
}
}
}
我的透視類看起來是這樣的:
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類需要的,但我嘗試了不同的場景,現在就把它留下。
我不知道我在做什麼錯,但無論我嘗試什麼,我都無法使「名稱」屬性顯示爲標題的文本。我很確定MainPivotViewModel被初始化時,它被分配爲本地資源,因爲它正確地調用我的構造函數,然後初始化我的可觀察集合,但這就像它一樣。
但它絕對沒有顯示!
我注意到的另一件事是,當我在BaseViewModel類的OnPropertyChanged方法的「Set」中放置斷點時,eventHandler始終爲空,無論我認爲應該如何,但我可以'看看我做錯了什麼。
我有很多關於stackoverflow和其他人的文章,我只是不明白我做錯了什麼?任何人有任何想法?
謝謝。
你是否在任何地方設置了你的頁面的'DataContext'? – 2013-03-27 07:12:26
嗨,是的,它設置在上面的xaml中。這只是不可見,但如果你滾動,你會看到它被設置。 – Thierry 2013-03-27 13:00:22
你有一個'Pivot'的ObservableCollection。這些是你自己的班級嗎?如果是這樣,他們看起來像什麼? – 2013-03-27 15:00:18