I'm using Prism 6.3 to build a simple cloud client with seafile.
After the user logs in, I navigate to a sidebar in a side region (SidebarRegion). OnNavigatedTo
are the libraries loaded in a collection and displayed.
When the selected library changes, I navigate to a new ItemsView instance (ContentRegion) and load the items (from the library) so they can also be displayed.
If now clicked on an item, I navigate to another side region to display detailed information about the item.
public SeafDirEntry SelectedItem
{
get { return _selectedItem; }
set
{
if (!SetProperty(ref _selectedItem, value) || value == null)
return;
var parameter = new NavigationParameters {{ "item", _selectedItem }};
_regionManager.RequestNavigate(_regionNames.InfoBarRegion, new Uri("ItemInfoView", UriKind.Relative), parameter);
}
}
There's also a delete button, which is hooked to a command which deletes the item
Now, after the item/file is deleted from the server, I hooked up the PubSubEvent
to reload the items from the library with the RefreshItemsAsync()
method.
After the items collection is overwritten, the PropertyChanged
event throws a NullReferenceException
, even if I try this:
public ObservableCollection<SeafDirEntry> Items
{
get { return _items; }
set
{
if (value == _items)
return;
_items = value;
RaisePropertyChanged(); // <- throws here
// SetProperty(ref _items, value); <- same result
}
}
I tried also to remove the item from the collection with the item as payload of the PubSubEvent
, but it also throws an NullReferenceException
at _items.Remove(itemFromPayload)
.
Even if I refresh the collection manually by a button, same result.
The ItemsViewModel
gets only created once per library and resists even after switching between them, so the reference should exists.
What did I miss out here?
AFAICT _items is not initialised unless you navigate to it with "valid" data, which is loaded asynchronously. So _items could be "null" well after the page has been displayed (at which point the XAML would presumably try to use a collection which is null). Consider instead the approach of always initialising it to an empty collection, and then populating that collection (on the correct thread) with the data you want displayed. That's how ObservableCollections are meant to work.