1

我有一個號碼的用戶,項目,會議的WinRT的應用等有鑑於複雜的對象更新時,屬性改變 - 不觸發屬性更改事件 - 的WinRT/XAML

我有一個主屏幕,以一個主屏幕視圖模型,它應該顯示CurrentUser並具有綁定到CurrentUser.ProjectList的ListView。

我使用UserProvider類初始化ViewModel中的CurrentUser,該類從數據庫獲取所有必需的信息。然後

我的問題就變得非常類似:Subscribe to INotifyPropertyChanged for nested (child) objects

我有一個用戶和項目模型:

public class User 
{ 
    public int id { get; set; } 
    public string ForeName { get; set; } 
    public string Surname { get; set; } 
    ... etc ... 
    public ObservableCollection<Project> ProjectList { get; set; } 
    public ObservableCollection<User> FriendList { get; set; } 

    ... constructor 

} 

public class Project 
{ 
    public String Name { get; set; } 
    public int Id { get; set; } 
    public List<User> Users { get; set; } 
    public List<Meeting> Meetings { get; set; } 

    .. constructor ... 
} 

視圖模型有以下:

class HomeScreenViewModel : INotifyPropertyChanged { 

private User _currentUser; 
public User CurrentUser 
{ 
    get { return this._currentUser; } 
    set 
     { 
      if (Equals(_currentUser, value)) return; 

      this._currentUser = value; 
      RaisePropertyChanged("CurrentUser"); 
     } 
    } 

    //[field: NonSerialized] 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void RaisePropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

...我在這個視圖模型中有一個方法來獲取當前用戶

public async Task<bool> GetLoggedInUserAsync() 
    { 
     int testId = 0; 
     CurrentUser = await userProvider.GetCurrentUser(testId); 

     UserProjects = await userProvider.GetUsersProject(CurrentUser); 
     CurrentUser.ProjectList = UserProjects; 

     return true; 
    } 

這就是所謂的視圖的LoadState的

public MainPage() 
    { 
     this.InitializeComponent(); 
     addMeeting = new AddMeetingFlyout(); 

     _vm = new HomeScreenViewModel(); 
     this.DataContext = _vm; 
    } 

    protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) 
    { 

     await _vm.GetLoggedInUserAsync() 
    } 

和我的XAML綁定,爲ProjectList並用的名字,例如,如下:

 <CollectionViewSource 
     x:Name="projectsViewSource" 
     Source="{Binding CurrentUser.ProjectList}"/> 

    ... 
    <ListView 
     x:Name="projectList" 
     ItemsSource="{Binding Source={StaticResource projectsViewSource}}" 
     Grid.Row="1" 
     SelectionMode="None" 
     Style="{StaticResource DraggableListView}" 
     ScrollViewer.VerticalScrollBarVisibility="Visible" 
     IsItemClickEnabled="True" 
    > 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <Button Style="{StaticResource ProjectTileButton}" Content="{Binding    Name}" Click="ProjectItem_Click" /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
     <AddDeleteThemeTransition/> 
    </ListView> 

     ... 
     <Button ...> 
      <TextBlock ..."> 
      <Run Text="{Binding CurrentUser.ForeName}" /> 
      </TextBlock> 
     </Button> 

按鈕的內容,CurrentUser。 CurrentName在ViewModel中初始化時觸發一個INotifyPropertyChanged事件。這反映在視圖中 - 但對CurrentUser.ForeName的任何進一步更改不會觸發任何後續的INotifyPropertyChanged事件。即使我知道它在那裏,ProjectList也不會在視圖中顯示,也不會觸發INotifyPropertyChanged事件。

我花了很多天看着實現INotifyPropertyChanged,以便嵌套的子複雜對象(如CurrentUser.ProjectList)的更改將傳播到視圖。在一分鐘,出現這種情況的唯一辦法是,如果我強行

  this._currentUser = value; 
      RaisePropertyChanged("CurrentUser"); 

一個電話,我正在與在視圖模型調用名爲MakeChange()方法

public void MakeChange() 
    { 
     User updatedCurrentUser = CurrentUser; 
     CurrentUser = updatedCurrentUser; 

    }  

此作品一鍵測試,所以我知道所有的數據都是從數據庫中正確地得到的,並且所有的數據都應該是這樣的 - 只需要擔心一件事!

但是,我根本無法獲得視圖來顯示頁面加載用戶項目,或添加新項目時。

我嘗試使用這一解決方案:https://gist.github.com/thojaw/705450,然而,WinRT的反射capabilites已經改變了,我不知道怎麼去以下留置權到我的項目的環境中工作,因爲這已經超出了我:

 //from property 
     //in _type.GetProperties(BindingFlags.Instance | BindingFlags.Public) 
     //where _inotifyType.IsAssignableFrom(property.PropertyType) 
     //select property; 

任何幫助將不勝感激 - 我真的認爲我所要做的就是將CurrentUser.ProjectList綁定到一個ListView。

回答

1

由於要更換整個的ObservableCollection本身,那麼你也將需要引入另一個屬性更改事件,並支持字段的集合屬性。

有一個很好的例子here

相關問題