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>."); $"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( // NavigationPage expects Page instances. Wrap any fallback control
$"Template for {vm.GetType().Name} returned {view.GetType().Name}, expected a Page."); // (e.g. ViewLocator error TextBlock) into a ContentPage so it can render.
page = new ContentPage { Content = view };
} }
page.DataContext = vm; page.DataContext = vm;

View file

@ -21,6 +21,18 @@ public class ViewLocator : IDataTemplate
} }
public Control Build(object? data) 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 return data switch
{ {
@ -32,7 +44,7 @@ public class ViewLocator : IDataTemplate
CirclesPageViewModel => _services.GetRequiredService<CirclesPage>(), CirclesPageViewModel => _services.GetRequiredService<CirclesPage>(),
PostAclDialogViewModel => _services.GetRequiredService<PostAclDialog>(), PostAclDialogViewModel => _services.GetRequiredService<PostAclDialog>(),
null => new TextBlock { Text = "No view for <null>" }, null => new TextBlock { Text = "No view for <null>" },
_ => new TextBlock { Text = $"No view for {data.GetType().Name}" } _ => new TextBlock { Text = $"No view for {data.GetType().Name}" }
}; };
} }

View file

@ -437,7 +437,11 @@ public partial class MainPageViewModel : ViewModelBase
[RelayCommand(CanExecute = nameof(CanManageAcl))] [RelayCommand(CanExecute = nameof(CanManageAcl))]
public async Task ManageAcl() 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); await ((App)App.Current!).PushPageAsync(GetACLViewModel(SelectedPost)).ConfigureAwait(true);
} }