You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI iOS应用添加NavigationPage后启动即崩溃的问题求助

MAUI iOS应用添加NavigationPage后启动即崩溃的问题求助

我现在正在开发一个MAUI iOS应用,原本功能完全正常,没任何问题。但头疼的是,iOS端不像Android那样自带返回按钮,也没有Xamarin里默认生成的NavigationBar——而且我了解到,如果不用NavigationBar或者NavigationPage,iOS的侧滑返回功能也会被禁用,所以我想加个NavigationBar/NavigationPage让用户能返回上一页。

我试着在App.xaml.cs里把主页面的代码改成了这样:

MainPage = new NavigationPage(new Logon());

结果一在测试设备上运行,应用直接崩溃了!断点停在iOS文件夹下Program.cs的这行代码:

UIApplication.Main(args, null, typeof(AppDelegate));

报错提示是System.NullReferenceException Message=Object reference not set to an instance of an object.

对应的Program.cs完整代码是这样的:

public class Program
{
    // This is the main entry point of the application.
    static void Main(string[] args)
    {
        // if you want to use a different Application Delegate class from "AppDelegate"
        // you can specify it here.
        UIApplication.Main(args, null, typeof(AppDelegate));
    }
}

我已经把这个问题反馈给微软官方了,之前排查时遇到的其他问题,在更新了Mac和测试iPhone的系统后都解决了,唯独这个崩溃问题一直没搞定。


补充:Logon页面的完整代码

public partial class Logon : ContentPage
{
    public Logon()
    {
        InitializeComponent();
    }
    User MyUser = new User();
    CompanyInformation MyCompany = new CompanyInformation();
    bool error = false;
    private async void OnCounterClicked(object sender, EventArgs e)
    {
        string username = TheUsername.Text;
        string password = ThePassword.Text;
        if (username != null)
        {
            if (password != null)
            {
                activityIndicator.IsVisible = true;
#if ANDROID
                await Task.Run(() => SignOn());
#elif IOS
                iOSSignon();
#endif
            }
            else
            {
                await DisplayAlert("ERROR", "Enter your password.", "OK");
            }
        }
        else
        {
            await DisplayAlert("ERROR", "Enter your username.", "OK");
        }

    }
    public void iOSSignon()
    {
        string result = "";


        MyUser.Username = TheUsername.Text.Trim();
        MyUser.Password = ThePassword.Text.Trim();
        try
        {

            bool isOnline = Utils.IsConnectedToTheInternet();

            if (isOnline)
            {
                MyServiceSoapClient myService = new MyServiceSoapClient(MyServiceSoapClient.EndpointConfiguration.MyServiceSoap);
                try
                {
                    result = myService.Logon(MyUser.Username, MyUser.Password);
                }
                catch (Exception ex)
                {
                    Utils.LogError("App:MyApp/Logon.xaml.cs/SignOn - An error occured in SignOn function, the error is: " + ex.Message + System.Environment.NewLine + Utils.GetExceptionInfo(ex));
                    DisplayAlert("ERROR", "There was an error in the logon process. The error was reported to technical support, please try again later of contact technical support at Support@MyCompany.com.", "OK");
                }
            }
            else
            {
                DisplayAlert("Information", "There is no internet connection or cell phone connection.  Connect to a wifi network or a cellular network.", "OK");
            }
        }
        catch (Exception ex)
        {
            Utils.LogError("App:MyApp/Logon.xaml.cs - An error occured in the SignOn function, the error is: " + ex.Message + System.Environment.NewLine + Utils.GetExceptionInfo(ex));
            DisplayAlert("ERROR", "An error occured in the logon process.  The error was reported to technical support, please try again later or contact technical support at Support@MyCompany.com.", "OK");
        }

        if (result == "CONNECTED")
        {
            Menu MyMenu = new Menu();
            MyMenu.MyUser = MyUser;
            Navigation.PushModalAsync(MyMenu);
        }
        else if (result == "DISABLED")
        {
            DisplayAlert("Information", "Your account has been disabled, contact your supervisor for more information.", "OK");
        }
        else
        {
            try
            {
                error = true;
                DisplayAlert("Information", "Invalid username or password.", "OK");
            }
            catch (Exception ex)
            {
                Utils.LogError("App:MyApp/Logon.xaml.cs/SignOn() - An error occured in the Logon.xaml.cs SignOn function result = connected, the error is: " + ex.Message + System.Environment.NewLine + Utils.GetExceptionInfo(ex));
                DisplayAlert("ERROR", "An error occurred during the logon process.  The error has been reported to technical support, please try again or contact technical support at Support@MyCompany.com", "OK");
            }
        }
        ;
        try
        {
            if (error)
            {
                Utils.LogError("App:MyApp/Logon.xaml.cs/SignOn() (2) - An error occured in the Logon.xaml.cs SignOn function result = connected, the error is: ");
            }
        }
        catch (Exception ex)
        {
            Utils.LogError("App:MyApp/Logon.xaml.cs/SignOn() (2) - An error occured in Logon.xaml.cs Signin function (second block), the error is: " + ex.Message + System.Environment.NewLine + Utils.GetExceptionInfo(ex));
            DisplayAlert("ERROR", "An error occurred during the logon process (2).  The error has been reported to technical support, please try again or contact technical support at Support@MyCompany.com", "OK");
        }
    }
    private async Task SignOn()
    {
        string result = "";


        MyUser.Username = TheUsername.Text.Trim();
        MyUser.Password = ThePassword.Text.Trim();
        try
        {

            bool isOnline = Utils.IsConnectedToTheInternet();

            if (isOnline)
            {
                MyServiceSoapClient myService = new MyServiceSoapClient(MyServiceSoapClient.EndpointConfiguration.MyServiceSoap);
                try
                {
                    result = myService.Logon(MyUser.Username, MyUser.Password);
                }
                catch (Exception ex)
                {
                    Utils.LogError("App:MyApp/Logon.xaml.cs/SignOn - An error occured in SignOn function, the error is: " + ex.Message + System.Environment.NewLine + Utils.GetExceptionInfo(ex));
                    await DisplayAlert("ERROR", "There was an error in the logon process. The error was reported to technical support, please try again later of contact technical support at Support@MyCompany.com.", "OK");
                }
            }
            else
            {
                await DisplayAlert("Information", "There is no internet connection or cell phone connection.  Connect to a wifi network or a cellular network.", "OK");
            }
            ;
        }
        catch (Exception ex)
        {
            Utils.LogError("App:MyApp/Logon.xaml.cs - An error occured in the SignOn function, the error is: " + ex.Message + System.Environment.NewLine + Utils.GetExceptionInfo(ex));
            await DisplayAlert("ERROR", "An error occured in the logon process.  The error was reported to technical support, please try again later or contact technical support at Support@MyCompany.com.", "OK");
        }
        ;

        if (result == "CONNECTED")
        {
                Menu MyMenu = new Menu();
                MyMenu.MyUser = MyUser;
                await Navigation.PushModalAsync(MyMenu);
        }
        else if (result == "DISABLED")
        {
            await DisplayAlert("Information", "Your account has been disabled, contact your subervisor for more information.", "OK");
        }
        else
        {
            try
            {
                error = true;
                await DisplayAlert("Information", "Invalid username or password.", "OK");
            }
            catch (Exception ex)
            {
                Utils.LogError("App:MyApp/Logon.xaml.cs/SignOn() - An error occured in the Logon.xaml.cs SignOn function result = connected, the error is: " + ex.Message + System.Environment.NewLine + Utils.GetExceptionInfo(ex));
                await DisplayAlert("ERROR", "An error occurred during the logon process.  The error has been reported to technical support, please try again or contact technical support at Support@MyCompany.com", "OK");
            }
        }
    }
}

有没有大佬能帮我看看是哪里出问题了?

备注:内容来源于stack exchange,提问作者Prescott Chartier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:57:59