【问题标题】:Xamarin - Cannot use PopModalAsyncXamarin - 无法使用 PopModalAsync
【发布时间】:2017-02-16 09:01:19
【问题描述】:

我正在尝试使用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(便携式)

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

LoginPage.xaml.cs(便携式)

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(便携式)

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 


         }
     }
 }

【问题讨论】:

  • 将 LoginPage 包装在 NavigationPage 中
  • 我尝试了MainPage = new NavigationPage(new View.LoginPage());,但仍然导致相同的结果。而且我不想把它添加到NavigationPage
  • 您想弹出已验证的浏览器对吗?你需要替换那里的pop。
  • 对不起,我无法理解你的意思。我的目的是在从服务器获取结果后,我想推送模式页面AuthenicationBrowser 并关闭模式页面AuthenicationBrowser
  • 是的,这就是我的意思。您必须这样做:AuthenticationBrowser 中的 Navigation.Pop。不在登录页面中。因为你希望 AuthenticationBrowser 被解除,所以把它放在那里。

标签: c# xamarin xamarin.android xamarin.forms


【解决方案1】:

最后,我可能会得到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();

        }



    }

【讨论】:

    【解决方案2】:

    看来你有错误的Navigation 对象。就我而言,我也犯了这个错误。

    我显示了一个模式页面,它有自己的导航栏(或导航对象)。所以当我想关闭这个模式页面时,我得到了提到的异常,因为导航堆栈上没有其他页面。这是错误的对象...

    【讨论】:

      【解决方案3】:

      对我来说,当我应该调用 PushAsync 时,我调用的是 PushModalAsync,没有要推送的项目来推送 PushModelAsync

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多