I am using Stack-Based Navigation (similar to master-details in Xmarion forms). The application works fine for Android and IOS simulators but in physical iPhone it crash randomly.
This is my App.xaml:
MainPage = new Sidebar()
{
FlowDirection = FlowDirection.LeftToRight
};
This is sibebar.xmal:
public partial class Sidebar : FlyoutPage
{
public Sidebar()
{
InitializeComponent();
Detail = new NavigationPage(new MainPage());
}
private async void GoToApplicationSettingsPage(object sender, TappedEventArgs e)
{
await Detail.Navigation.PushAsync(new ApplicationSetting());
IsPresented = false;
}
private async void GoToHxSettingsPage(object sender, TappedEventArgs e)
{
await Detail.Navigation.PushAsync(new MedicalHistorySettings(), true);
IsPresented = false;
}
private async void GoToVisitsSettingsPage(object sender, TappedEventArgs e)
{
await Detail.Navigation.PushAsync(new VisitsSettings(), true);
IsPresented = false;
}
private async void GoToOperationsSettingsPage(object sender, TappedEventArgs e)
{
await Detail.Navigation.PushAsync(new OperationsSettings(), true);
IsPresented = false;
}
private async void GoToVisitsSerachPage(object sender, TappedEventArgs e)
{
await Detail.Navigation.PushAsync(new VisitsSearch(), true);
IsPresented = false;
}
private async void GoToOperationsSearchPage(object sender, TappedEventArgs e)
{
await Detail.Navigation.PushAsync(new OperationsSearch(), true);
IsPresented = false;
}
}
This is what happen using iPhone 13:
As seen the app is crashed when this code executed 3 times:
await Detail.Navigation.PushAsync(new ApplicationSetting(), true);
I tried on another device (iPhone 13 pro max) in the first time the app deployed to the device its work without any problem, but when deploy again it also crashed but when navigating for 5 or 7 times. It really very random and unexpected. I tried on debug and release mode but it doesn’t solve the problem. Someone suggest to empty the stack before navigation, so I tried this:
await Detail.Navigation.PopAsync();
await Detail.Navigation.PushAsync(new ApplicationSetting());
But also same problem. It’s worth mentioning that there no error or exceptions occur during the run time, but when I test it several times, only two times I get this error:
An unhandled exception of type ‘System.InvalidCastException’ occurred in Microsoft.iOS.dll: ‘Specified cast is not valid.’
at UIKit.UIApplication.xamarin_UIApplicationMain(Int32 argc, IntPtr argv, IntPtr principalClassName, IntPtr delegateClassName, IntPtr* gchandle)
at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
at EClinicMaui.Program.Main(String[] args) in /Users/sysprobs/Desktop/E-Clinic/EClinicMaui/Platforms/iOS/Program.cs:line 14
I upload the project to github so that anyone can test it.
I am using Visual studio code in MAC OS.
2
Answers
Could you try pushing already formed page instead of forming new one every time and check if this fixes the problem? Like to change it to:
Or try to change the behaviour from PushAsync to PushModalAsync, like
Well then you should rather use AppShell with pre-determined content and routing like so:
In your case you would probably need to adjust FlyoutWidth and FlyoutBehaviour,also you should define your own style for the items like so:
As for the navigation, click on items or use routing from the code behind:
For register:
And for navigation:
More on the subject:
https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation?view=net-maui-9.0