1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Xamarin.Forms】TabbedPageのタブ押下時に処理をしたい

Last updated at Posted at 2019-10-18

#何がしたいのか

  • TabbedPageのタブをタップした際に、何か処理をしたい。
  • TabbedPage内にNavigationPageがあり、それをRootに遷移したい。
  • 選択済のタブ押しても、Rootに遷移したい。

#どう実装したのか
まず、Forms側だとChangeイベントは取れたが選択のイベントがなかったので素直にCustomRendererを作成。

CustomTabbedRenderer.cs
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == "Renderer")
            {
                navigationBar = (GetChildAt(0) as Android.Widget.RelativeLayout).GetChildAt(1) as BottomNavigationView;
                navigationBar.NavigationItemSelected += ItemSelected;
                navigationPage = ((NavigationPage)(sender as TabbedPage).Children[0]);
            }
        }

async void ItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
        {
            this.OnNavigationItemSelected(e.Item);
            await navigationPage.Navigation.PopToRootAsync();
            MessagingCenter.Send(navigationPage, "doit!");
        }

Rendererの際に、NavigationBarのItemSelectedイベント設定と
NavigationPageを取得。
選択された際に、NavigationPageをRootに戻し
Messengerで強引に、処理を発火。

#おわり
なんかもうちょい綺麗に出来る気がするんですが、同じような記事が全くなくて
どうしようもありませんでした・・・。
知見溢れる方、ご指導頂けると助かります!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?