PostIt: show ViewLocator fallback errors in navigation

This commit is contained in:
Paul Schneider 2026-08-19 17:32:51 +01:00
commit 600bb81be1
Signed by: notazof
GPG key ID: 1DD5D838E5343B06
3 changed files with 23 additions and 5 deletions

View file

@ -324,10 +324,12 @@ public partial class App : Application
$"Template for {vm.GetType().Name} returned <null>.");
}
if (view is not Page page)
var page = view as Page;
if (page is null)
{
throw new InvalidOperationException(
$"Template for {vm.GetType().Name} returned {view.GetType().Name}, expected a Page.");
// NavigationPage expects Page instances. Wrap any fallback control
// (e.g. ViewLocator error TextBlock) into a ContentPage so it can render.
page = new ContentPage { Content = view };
}
page.DataContext = vm;

View file

@ -21,6 +21,18 @@ public class ViewLocator : IDataTemplate
}
public Control Build(object? data)
{
try
{
return BuildCore(data);
}
catch (Exception ex)
{
return new TextBlock { Text = $"ViewLocator threw: {ex}" };
}
}
private Control BuildCore(object? data)
{
return data switch
{

View file

@ -437,7 +437,11 @@ public partial class MainPageViewModel : ViewModelBase
[RelayCommand(CanExecute = nameof(CanManageAcl))]
public async Task ManageAcl()
{
if (SelectedPost is null) return;
if (SelectedPost is null)
{
StatusMessage = "Select an existing post before managing ACL.";
return;
}
await ((App)App.Current!).PushPageAsync(GetACLViewModel(SelectedPost)).ConfigureAwait(true);
}