我試圖使用PopModalAsync
刪除模態頁面。然而,Navigation.ModalStack.Count
爲0。如果我使用PopModalAsync
,它會拋出異常:Xamarin - 無法使用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(飲用水)*編輯:把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
}
}
}
裹在一個NavigationPage – Mittchel
的LoginPage我試圖用'=的MainPage新NavigationPage(新View.LoginPage());',但是仍然會導致同樣的結果。我不想在「NavigationPage」中添加它 –
你想彈出Authenticated瀏覽器嗎?您需要替換那裏的流行音樂。 – Mittchel