I have a page that is registered in the AppShell like this:
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute(nameof(LesezeichenPage), typeof(LesezeichenPage));
}
...and the Flyout:
<FlyoutItem FlyoutDisplayOptions="AsMultipleItems" >
<ShellContent Title="Lesezeichen" Route="LesezeichenPage" ContentTemplate="{DataTemplate v:LesezeichenPage}">
</ShellContent>
</FlyoutItem>
Additionally the page can be opened from other pages in the App with an ImageButton:
Shell.Current.GoToAsync("LesezeichenPage");
The problem is with the following approach: I save an Observablecollection to my App's Properties on this page with the following method:
private void LesezeichenSpeichern(ContentPage contentPage)
{
var parentPage = (TabbedPage)contentPage.Parent;
parentPage.CurrentPage = parentPage.Children[1];
Bookmarks.Add(new Bookmark { Novene = PickedNovene, Tag = PickedTag, Date = DateTime.Today.ToString("d/M/yy"), ID = Bookmarks.Count }) ;
string LesezeichenJson = JsonConvert.SerializeObject(Bookmarks);
SetProperties("JsonLesezeichen", LesezeichenJson);
}
public async static void SetProperties(string property, object value)
{
var app = (App)Application.Current;
app.Properties[property] = value;
await app.SavePropertiesAsync();
}
When I open the page the Json will then be deserialised and added to an ObservableCollection in the constructor:
public LesezeichenViewModel()
{
Bookmarks = new ObservableCollection<Bookmark>();
////Lade Properties wenn in einer vorherigen Session gespeichert
if (Application.Current.Properties.ContainsKey("JsonLesezeichen"))
{
string savedLesezeichen = Application.Current.Properties["JsonLesezeichen"].ToString();
var InterimBookmarks = JsonConvert.DeserializeObject<ObservableCollection<Bookmark>>(savedLesezeichen);
foreach (var bookmark in InterimBookmarks)
{
Bookmarks.Add(new Bookmark { Novene = bookmark.Novene, Tag = bookmark.Tag, Date = bookmark.Date, IsChecked = false, ID = bookmark.ID });
}
}
Unfortunately when I make changes to the ObservableCollection after navigating to the page with Shell.Current.GoToAsync("LesezeichenPage") and then open the page about the FlyoutItem, the constructor won't fire for a second time. Hence the Json cannot be deserialised and the changes won't be visible in the ListView of the page.
I have to make the constructor fire somehow every time when opening the page, if that's possible. Seen a post with UWP about a NavigationCacheMode that can be disabled though ContentPage/TabbedPage don't have these options.
Maybe someone has another idea/approach to solve this?