[英]How can I detect the clicking of a tab button in Xamarin.Forms?
Here is the code that I have.这是我的代码。 I would like to know how I can detect when a user clicks a tab that is already selected as I want to toggle the icon for the aPage between play.png and pause.png plus I also want to call a method on APage.
我想知道如何检测用户何时单击已选中的选项卡,因为我想在 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);
}
}
Note that if possible I would like to find a solution that does not involve custom renderers for both iOS and Android. I'm wondering can I redefine the TabbedPage and put the logic in that class?请注意,如果可能的话,我想找到一个不涉及 iOS 和 Android 的自定义渲染器的解决方案。我想知道我是否可以重新定义 TabbedPage 并将逻辑放入 class?
I know you want to avoid using custom renderers, but this is only possible by using a Custom Renderer.我知道您想避免使用自定义渲染器,但这只能通过使用自定义渲染器来实现。
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
}
}
}
}
Code credit: @Kyle https://stackoverflow.com/a/42909203/5953643代码信用: @Kyle https://stackoverflow.com/a/42909203/5953643
如果要获取选定的选项卡,则需要使用ItemSource和SelectedItem属性,如 ListView。
You can do this easily in iOS, but in Android you need a custom renderer.您可以在 iOS 中轻松完成此操作,但在 Android 中您需要自定义渲染器。 Just check this blog http://motzcod.es/post/162985782667/dynamically-changing-xamarin-forms-tab-icons-when-select
只需查看此博客http://motzcod.es/post/162985782667/dynamically-sharing-xamarin-forms-tab-icons-when-select
You can't.你不能。
TabbedPage
interited from MultiPage
that you can check the source from here . TabbedPage
从MultiPage
中插入,您可以从这里检查源。 All select, deselect, update, template and logic is implemented here.所有的选择、取消选择、更新、模板和逻辑都在这里实现。 You suppose to watch
CurrentPage
property but it has value check if already selected, so you cannot use.您想观看
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.