Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Xamarin Forms: BaseContentPage and BackButtonBehavior Binding not working (Shell)

$
0
0

Hey guys,

I created my own BaseContentPage and created a BindableProperty for a custom BackButtonBehavior.
My goal is to change the standard backbutton icon and allow a custom BackButtonBehavior with own Commands.

My BaseContentPage looks like that:

    public class BaseContentPage : ContentPage
    {
        public static readonly BindableProperty CustomBackButtonBehaviorProperty =
            BindableProperty.Create(nameof(CustomBackButtonBehavior), typeof(BackButtonBehavior), typeof(BaseContentPage),
                                            null, defaultBindingMode: BindingMode.Default
                                            ,propertyChanged: OnBackButonBehaviorPropertyChanged);


        public BackButtonBehavior CustomBackButtonBehavior
        {
            get
            {
                if (GetValue(CustomBackButtonBehaviorProperty) == null)
                    return CreateDefaultBackButtonBehavior();
                else
                    return (BackButtonBehavior)GetValue(CustomBackButtonBehaviorProperty);
            }
            set { this.SetValue(CustomBackButtonBehaviorProperty, value); }
        }

        public BaseContentPage()
        {
            //Shell.SetBackButtonBehavior(this, CustomBackButtonBehavior);
        }

        private BackButtonBehavior CreateDefaultBackButtonBehavior()
        {
            Application.Current.Resources.TryGetValue("FontAwesomeSolid", out var fontAwesomeSolid);

            var behavior = new BackButtonBehavior()
            {
                Command = new Command(async () =>
                {
                    await Shell.Current.GoToAsync("..");
                }),
                IconOverride = new FontImageSource()
                {
                    FontFamily = ((OnPlatform<string>)fontAwesomeSolid).Platforms.FirstOrDefault(p => p.Platform[0] == Device.RuntimePlatform).Value.ToString(),
                    Glyph = Controls.FontAwesomeSolid.IconConstants.ChessPawn,
                    Size = 22,
                    Color = Color.White
                },
            };

            return behavior;
        }

        static void OnBackButonBehaviorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {

        }
    }

I use it for a new Xaml Page, which looks like that:

<?xml version="1.0" encoding="utf-8" ?>
<d:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:d="clr-namespace:IncomingDocumentApp.Controls"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="xxxx.Views.NewItemPage.NewItemPage"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
             ios:Page.UseSafeArea="true"
             Title="New Item"
             Visual="Material">

than i tried to set the BackButtonBehavior:

   <d:BaseContentPage.CustomBackButtonBehavior>
        <BackButtonBehavior Command="{Binding GoBackCommand}"
                            IsEnabled="True"/>
    </d:BaseContentPage.CustomBackButtonBehavior>

However, it the CustomBackButtonBehaviorProperty is never called.
The Binding is defined in the codebehind of the NewItemPage right after the InitializeComponent() call.

Any help would be appreciated!

snoova


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>