首页 > 解决方案 > 绑定可绑定属性命令在 Xamarin 表单中不起作用

问题描述

我有一个具有可绑定属性的自定义视图。

我正在尝试在我的主页视图模型中绑定命令。

这是我所做的方式,但绑定不起作用。

<ContentView.Content>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>

            </Grid.ColumnDefinitions>

            <Label
                Grid.Column="0"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="StartAndExpand"
                FontFamily="{StaticResource Filled}"
                TextColor="{StaticResource PrimaryLight}"
                FontSize="25"
                Text="{Binding Source={x:Reference TitleView}, Path=LeftIcon}">

                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                </Label.GestureRecognizers>

            </Label>

            <Label
                Grid.Column="0"
                HorizontalOptions="CenterAndExpand"
                VerticalOptions="CenterAndExpand"
                FontAttributes="Bold"
                HorizontalTextAlignment="Center"
                FontSize="{StaticResource FontSize16}"
                TextColor="{StaticResource PrimaryLight}"
                Text="{Binding Source={x:Reference TitleView}, Path=Title}"/>

            <Label
                Grid.Column="0"
                Margin="0,0,16,0"
                HorizontalOptions="End"
                VerticalOptions="CenterAndExpand"
                FontFamily="{StaticResource FontIconsFamily}"
                TextColor="{StaticResource PrimaryLight}"
                FontSize="25"
                Text="{Binding Source={x:Reference TitleView}, Path=RightIcon}">

                <Label.Triggers>
                    <DataTrigger
                        TargetType="Label" 
                          Binding="{Binding Source={x:Reference TitleView}, Path=IsFilledFontFamily}" Value="true">
                        <Setter Property="FontFamily" Value="{StaticResource Filled}" />
                    </DataTrigger>
                </Label.Triggers>

                <Label.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding FilterCommand}"/>
                </Label.GestureRecognizers>

            </Label>
        </Grid>

  </ContentView.Content>

我的 .cs 文件如下所示

public partial class NavigationBarTitleView : ContentView
    {
        public static readonly BindableProperty LeftIconProperty = BindableProperty.Create(
            nameof(LeftIcon),
            typeof(string),
            typeof(NavigationBarTitleView),
            string.Empty);

        public static readonly BindableProperty RightIconProperty = BindableProperty.Create(
            nameof(RightIcon),
            typeof(string),
            typeof(NavigationBarTitleView),
            string.Empty);

        public static readonly BindableProperty TitleProperty = BindableProperty.Create(
            nameof(Title),
            typeof(string),
            typeof(NavigationBarTitleView),
            string.Empty);

        public static readonly BindableProperty IsFilledFontFamilySelectorProperty = BindableProperty.Create(
            nameof(IsFilledFontFamily),
            typeof(bool),
            typeof(NavigationBarTitleView),
            false);

        public static readonly BindableProperty LeftIconCommandProperty = BindableProperty.Create(
            nameof(LeftIconCommand),
            typeof(ICommand),
            typeof(NavigationBarTitleView), default(ICommand));

        //private static void OnTapCommandPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        //{
        //    if (bindable is NavigationBarTitleView headerTemplate && newValue is ICommand command)
        //    {
        //        headerTemplate.LeftIconCommand = command;
        //    }
        //}

        public NavigationBarTitleView()
        {
            InitializeComponent();
        }

        public string LeftIcon
        {
            get => (string)GetValue(LeftIconProperty);
            set => SetValue(LeftIconProperty, value);
        }

        public string RightIcon
        {
            get => (string)GetValue(RightIconProperty);
            set => SetValue(RightIconProperty, value);
        }

        public string Title
        {
            get => (string)GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        public bool IsFilledFontFamily
        {
            get => (bool)GetValue(IsFilledFontFamilySelectorProperty);
            set => SetValue(IsFilledFontFamilySelectorProperty, value);
        }

        public ICommand LeftIconCommand
        {
            get => (ICommand)GetValue(LeftIconCommandProperty);
            set => SetValue(LeftIconCommandProperty, value);
        }

        private void TapGestureRecognizer_Tapped(object sender, System.EventArgs e)
        {
            LeftIconCommand?.Execute(null);
        }

我需要在我的主页中绑定它,如下所示。

<NavigationPage.TitleView>
        <CommonViews:NavigationBarTitleView
            LeftIcon="search"
            Title="My Shipments"
            RightIcon="{StaticResource Filter}"
            LeftIconCommand="{Binding BackNavigationCommand}"/>
    </NavigationPage.TitleView>

我的视图模型我只是添加一个命令

public class ShipmentTabbedPageViewModel
    {
        private Command backNavigationCommand;

        public ICommand BackNavigationCommand => backNavigationCommand
                                       ?? (backNavigationCommand = new Command(() =>
                                       {
                                           Application.Current.MainPage = new NavigationPage(new ShipmentPage());
                                       }));
    }

但我的命令绑定不工作其他绑定是好的。

有什么我做错了吗?

标签: c#xamarin.formsbindableproperty

解决方案


在您的 MainPage.xaml.cs 文件中尝试:

public MainPage()
{
    InitializeComponent();
    BindingContext = new ShipmentTabbedPageViewModel();
}

推荐阅读