簡體   English   中英

MessagingCenter.Send 上的 System.Reflection.TargetInvocationException

[英]System.Reflection.TargetInvocationException on MessagingCenter.Send

我正在嘗試使用MessagingCenter構建簡單的導航,但是當我按下后退按鈕(硬件按鈕)時,我收到了System.Reflection.TargetInvocationException

這是我得到錯誤的方式;

應用加載后,
我按下后退按鈕(硬件按鈕)
然后在應用程序最小化后,我在最近的應用程序中打開它
之后,我單擊“登錄”,然后出現此錯誤:

Unhandled Exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

指着

MessagingCenter.Send<object>(this, App.EVENT_LAUNCH_MAIN_PAGE);

在 LoginPage.xaml.cs 中的登錄方法中

PS:如果我不按后退按鈕(硬件按鈕),代碼運行良好

這是代碼:

App.xaml.cs
public partial class App : Application
{
    public static string EVENT_LAUNCH_LOGIN_PAGE = "EVENT_LAUNCH_LOGIN_PAGE";
    public static string EVENT_LAUNCH_MAIN_PAGE = "EVENT_LAUNCH_MAIN_PAGE";

    public App()
    {
        InitializeComponent();

        MainPage = new App3.LoginPage();

        MessagingCenter.Subscribe<object>(this, EVENT_LAUNCH_LOGIN_PAGE, SetLoginPageAsRootPage);
        MessagingCenter.Subscribe<object>(this, EVENT_LAUNCH_MAIN_PAGE, SetMainPageAsRootPage);
    }

    private void SetLoginPageAsRootPage(object sender)
    {
        MainPage = new NavigationPage(new LoginPage());
    }

    private void SetMainPageAsRootPage(object sender)
    {
        MainPage = new NavigationPage(new App3.MainPage());
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

LoginPage.xaml.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage
{
    public Command LoginCommand { get; }

    public LoginPage()
    {
        InitializeComponent();

        LoginCommand = new Command(() => Login());

        Button btn = new Button { Text = "Login", Command = LoginCommand };

        Content = new StackLayout
        {
            Children =
            {
                btn
            }
        };
    }

    public void Login()
    {
        MessagingCenter.Send<object>(this, App.EVENT_LAUNCH_MAIN_PAGE);
    }
}

MainPage.xaml.cs
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        ToolbarItems.Add(new ToolbarItem("Logout", "", () => Logout()));
    }

    public void Logout()
    {
        MessagingCenter.Send<object>(this, App.EVENT_LAUNCH_LOGIN_PAGE);
    }
}

這是 Xamarin.Forms 版本 2.3.3.175 中的一個錯誤。 若要修復此錯誤,請安裝早期版本的 Xamarin.Forms。 我讓我的應用程序以 2.3.0.107 版運行。

版本 2.3.3.175 中的錯誤應在版本 2.3.4-pre1 中修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM