![](/img/trans.png)
[英]How can I place a button in the the tabs of a TabbedPage in Xamarin.Forms?
[英]How can I detect the clicking of a tab button in Xamarin.Forms?
這是我的代碼。 我想知道如何檢測用戶何時單擊已選中的選項卡,因為我想在 play.png 和 pause.png 之間切換 aPage 的圖標,另外我還想在 APage 上調用一個方法。
public partial class MainPage : TabbedPage
{
public MainPage()
{
InitializeComponent();
var aPage = new NavigationPage(new APage())
{
Title = "Play",
Icon = "play.png"
};
var bPage = new NavigationPage(new BPage())
{
Title = "Settings",
Icon = "b.png"
};
Children.Add(aPage);
Children.Add(bPage);
}
}
請注意,如果可能的話,我想找到一個不涉及 iOS 和 Android 的自定義渲染器的解決方案。我想知道我是否可以重新定義 TabbedPage 並將邏輯放入 class?
我知道您想避免使用自定義渲染器,但這只能通過使用自定義渲染器來實現。
using Android.Content;
using Android.Support.Design.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
[assembly: ExportRenderer(typeof(MainPage), typeof(MainPageRenderer))]
namespace YourNameSpace
{
public class MainPageRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
{
MainPage _page;
public MainPageRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
_page = e.NewElement as MainPage;
else
_page = e.OldElement as MainPage;
}
void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)
{
System.Diagnostics.Debug.WriteLine("Tab Reselected");
//Handle Tab Reselected
}
}
}
using System;
using System.Diagnostics;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(MainPage), typeof(MainPageRenderer))]
namespace YourNameSpace
{
public class MainPageRenderer : TabbedRenderer
{
MainPage _page;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
_page = e.NewElement as MainPage;
else
_page = e.OldElement as MainPage;
try
{
if (ViewController is UITabBarController tabBarController)
tabBarController.ViewControllerSelected += OnTabbarControllerItemSelected;
}
catch (Exception exception)
{
Debug.WriteLine(exception);
}
}
void OnTabbarControllerItemSelected(object sender, UITabBarSelectionEventArgs eventArgs)
{
if (_page?.CurrentPage?.Navigation != null && _page.CurrentPage.Navigation.NavigationStack.Count > 0)
{
Debug.WriteLine("Tab Tapped");
//Handle Tab Tapped
}
}
}
}
如果要獲取選定的選項卡,則需要使用ItemSource和SelectedItem屬性,如 ListView。
您可以在 iOS 中輕松完成此操作,但在 Android 中您需要自定義渲染器。 只需查看此博客http://motzcod.es/post/162985782667/dynamically-sharing-xamarin-forms-tab-icons-when-select
你不能。 TabbedPage
從MultiPage
中插入,您可以從這里檢查源。 所有的選擇、取消選擇、更新、模板和邏輯都在這里實現。 您想觀看CurrentPage
屬性,但如果已選擇,則它具有值檢查,因此您無法使用。
如果您使用的是 shell 和 MAUI,這就是它的完成方式: https ://docs.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation#navigation-deferral
this.PropertyChanging += async (object sender, PropertyChangingEventArgs e) =>
{
if (e.PropertyName == "CurrentPage")
{
if (this.CurrentPage == null)
return;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.