我正在了解有關 TabbedPage 的更多資訊。我有一個問題希望有人可以幫助我:
我有 TabbedPage 的頁面(Page1、Page2、Page3、Page4、Page5)。當我選擇 TabbedPage 的 Page5 時,它會顯示 Popup。當我關閉彈出視窗時,它如何回傳到我觸摸 TabbedPage 的上一個頁面。
例如:我選擇Page2 TabbedPage,接下來我選擇Page5 TabbedPage,它顯示Popup,我關閉Popup,它會回到選擇TabbedPage的Page2,類似:我選擇Page3 TabbedPage,接下來我選擇Page5 TabbedPage,它顯示Popup ,我關閉Popup,它會回到選擇Page3 TabbedPage,.....
請注意,它將回傳到先前選擇的 TabbedPage。
主視圖.xaml
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
BackgroundColor="#fff".....>
<!--Pages can be added as references or inline-->
<views:Page1 Title="Page 1" IconImageSource="homeicon" BackgroundColor="#f7f7f7"/>
<views:Page2 Title="Page 2" IconImageSource="manageorder" BackgroundColor="#fff"/>
<views:Page3 Title="Page 3" IconImageSource="feeds" BackgroundColor="#fff"/>
<views:Page4 Title="Page 4" IconImageSource="moneys" BackgroundColor="#fff"/>
<views:Page5 Title="Page 5" IconImageSource="accounticon" BackgroundColor="#fff"/>
</TabbedPage>
MainView.xaml.cs
private async void PopupAlert()
{
await PopupNavigation.Instance.PushAsync(new PopupAlertPage());
}
protected override void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
if (CurrentPage is Page5)
{
PopupAlert();
}
}
PopupAlertPage.xaml.cs
private void close_Tapped(object sender, EventArgs e)
{
//App.Current.MainPage = new NavigationPage(new MainView(0));
PopupNavigation.Instance.PopAllAsync();
Application.Current.MainPage.Navigation.PopAsync();
}
請求幫忙。非常感謝
更新1
MainView.xaml.cs
public partial class MainView
{
public MainView(int index)
{
NavigationPage.SetHasNavigationBar(this, false);
InitializeComponent();
On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
//CurrentPage = Children[0];
SetPage(index);
}
void SetPage(int index)
{
CurrentPage = Children[index];
}
private async void PopupAlertLogin()
{
await PopupNavigation.Instance.PushAsync(new PopupAlertLogin());
}
protected override void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
if (CurrentPage is Page5)
{
PopupAlertLogin();
}
}
}
PopupAlertPage.xaml.cs
private void close_Tapped(object sender, EventArgs e)
{
PopupNavigation.Instance.PopAllAsync();
var tab = (TabbedPage)Application.Current.MainPage;
tab.CurrentPage = tab.Children[0];
//var mainPage = this.Parent as TabbedPage;
//mainPage.CurrentPage = mainPage.Children[4];
}
錯誤:

uj5u.com熱心網友回復:
即使我不支持你的邏輯,但你可以通過這種方式實作這一點。
在您的 MainView 類中,宣告一個屬性 OldPageIndex 并添加此函式。
public static string OldPageIndex { get; set; }
protected override async void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
if (this.CurrentPage is Page5)
{
//await Navigation.PushAsync(new popup());
// Navigate to any page you want .
//PopupAlertLogin
}
else
{
OldPageIndex = this.CurrentPage.Title;
}
}
在您導航PopupAlertLogin到關閉功能的頁面上。
private void close_Tapped(object sender, EventArgs e)
{
var tab = (App.Current.MainPage as NavigationPage).RootPage as TabbedPage;
tab.CurrentPage = tab.Children.FirstOrDefault(c => c.Title == MainView.OldPageIndex);
PopupNavigation.Instance.PopAllAsync();
}
uj5u.com熱心網友回復:
您可以使用Children.IndexOf(CurrentPage)來獲取 TabbedPage 的標簽索引。然后使用MessageCenter導航到具有特定索引的頁面。
標簽頁1:
public partial class TabbedPage1 : Xamarin.Forms.TabbedPage
{
public static int index { get; set; }
public TabbedPage1()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
InitializeComponent();
On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
//CurrentPage = Children[0];
MessagingCenter.Subscribe<Object, int>(this, "Navigation", (arg, idx) =>
{
// idx: the index of pages in tabbed that you want to navigate
CurrentPage = this.Children[idx];
});
}
private async void PopupAlert()
{
await PopupNavigation.Instance.PushAsync(new PopupAlertLogin());
}
protected override void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
if (CurrentPage is Page5)
{
PopupAlert();
}
else
{
index = Children.IndexOf(CurrentPage);
}
}
}
彈出警報登錄:
private void close_Tapped(object sender, EventArgs e)
{
MessagingCenter.Send<object, int>(this, "Navigation", TabbedPage1.index);// if you want to navigation to the specific page .
PopupNavigation.Instance.PopAllAsync();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442279.html
標籤:xamarin
