Xamarin - 无法使用PopModalAsync

Jac*_*hek 8 c# xamarin.android xamarin xamarin.forms

我试图用来PopModalAsync删除模态页面.但是,Navigation.ModalStack.Count为0.如果我使用PopModalAsync,它将抛出异常:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

我在用Xamarin.Forms.以下是一些示例代码:

App.cs(Potable)

public class App : Application
{
    public App()
    {
        // The root page of your application
        MainPage = new View.LoginPage();
    }
}
Run Code Online (Sandbox Code Playgroud)

LoginPage.xaml.cs(Potable)

public partial class LoginPage : ContentPage
{
    public INavigation _Navigate;
    public LoginPage()
    {
        InitializeComponent();
        _Navigate = Navigation;
    }

    async void LoginBtnClicked(object sender, EventArgs args)
    {
        await _Navigate.PushModalAsync(new AuthenicationBrowser());
        //await _Navigate.PopModalAsync(); it is work at here
        Debug.WriteLine("Navigation.NavigationStack  LoginBtnClicked ===> {0}", Navigation.NavigationStack.Count); //getting 0
         Debug.WriteLine("Navigation.ModalStack  LoginBtnClicked ===> {0}", Navigation.ModalStack.Count);  // getting 1    
    }

    public async void PopModal()
    {
        Debug.WriteLine(Navigation.NavigationStack.Count);
        await Navigation.PopModalAsync();
    }



}
Run Code Online (Sandbox Code Playgroud)

AuthenicationBrowser.cs(Potable)*编辑:放PopModalAsync*

public partial class AuthenicationBrowser : ContentPage
{
    public AuthenicationBrowser()
    {
      InitializeComponent();
    }
    public async void PopModal()
    {
       Debug.WriteLine("Navigation.ModalStack  AuthenicationBrowser .PopModal===> {0}", Navigation.ModalStack.Count);  // getting 0    
       await Navigation.PopModalAsync();
    }
}
Run Code Online (Sandbox Code Playgroud)

BrowserView.cs(Potable)

public class BrowserView : WebView
{
    public BrowserView()
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

AuthenicationBrowserRenderer.cs(Droid)*编辑:调用PopModal*

  [assembly: ExportRenderer(typeof(BrowserView), typeof(AuthenicationBrowserRenderer))] 
 namespace App.Droid
 {
     class AuthenicationBrowserRenderer : WebViewRenderer
     {
       ... // Doing some Auth in OnElementChanged and using JavaScriptCallBack class after received json in Webview
     }
     public class JavaScriptCallBack: Java.Lang.Object, IValueCallback
     {
        public JavaScriptCallBack()
        {

        }
        public async void OnReceiveValue(Java.Lang.Object result)
        {
            Java.Lang.String json = (Java.Lang.String)result;
            string raw_json = json.ToString();
            Debug.WriteLine("raw_json  ====>>> {0}", raw_json);
            var login_page = new LoginPage();
            var auth_page = new AuthenicationBrowser();

            Debug.WriteLine(login_page.Navigation.ModalStack.Count); // getting 0
            Debug.WriteLine(auth_page.Navigation.ModalStack.Count); // getting 0
            auth_page.PopModal(); // Trying to do PopModalAsync 


         }
     }
 }
Run Code Online (Sandbox Code Playgroud)

Jac*_*hek 6

App.Current.MainPage.Navigation.PopModalAsync();最后,我可能会得到可以解决问题的答案 。原因是该页面new LoginPage()被称为新的Content Page不存在页面。

如果我从App.Current.MainPage,它可以从模态堆栈中获取现有的模态。

所以解决方案可以是:

    public partial class LoginPage : ContentPage
    {

        public LoginPage()
        {
            InitializeComponent();

        }


        async void LoginBtnClicked(object sender, EventArgs args)
        {
            await Navigation.PushModalAsync(new AuthenicationBrowser());
        }

        public async void PopModal()
        {

            Debug.WriteLine("Navigation.ModalStack  PopModal ===> {0}", App.Current.MainPage.Navigation.ModalStack.Count);
            await App.Current.MainPage.Navigation.PopModalAsync();

        }



    }
Run Code Online (Sandbox Code Playgroud)