Xamarin - 无法使用PopModalAsync

时间:2017-02-16 09:01:20

标签: c# xamarin xamarin.android 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();
    }
}

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();
    }



}

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();
    }
}

BrowserView.cs(Potable)

public class BrowserView : WebView
{
    public BrowserView()
    {

    }
}

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 


         }
     }
 }

3 个答案:

答案 0 :(得分:2)

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

如果我从new LoginPage()(现有的LoginPage)调用它,它可以从Modal Stack中获取现有的模态。

所以解决方案可以是:

Content Page

答案 1 :(得分:1)

似乎您有错误的Navigation对象。就我而言,我也有这个错误。

我显示了一个模式页面,该页面具有自己的导航栏(或“导航”对象)。因此,当我想关闭该模式页面时,遇到了上述异常,因为导航堆栈中没有其他页面。这是错误的对象...

答案 2 :(得分:0)

对我来说,当我应该打电话给PushAsync时,我就打电话给PushModalAsync