Xamarin Forms命令在自定义控件中总是为空。

问题描述 投票:0回答:1

我试图创建一个用户控件。该用户控件将是一个显示加载器的按钮。

我试着从我的页面中传递一条绑定命令到我的控件中

我的XAML PAge是。

                <sharedviews:ButtonLoader TextLabel="{extensions:Translate Save}"   BackgroundColorButton="{StaticResource RedColor}" HeightRequest="50" HorizontalOptions="Center" Animation="Cupertino" TextColorIndicator="White" 
                                          TextColorLabel="White"  VerticalOptions="End" Command="{Binding ValidateMandatoryCommand}" TextWaiting="{extensions:Translate Saving...}" />

                <Button HeightRequest="50" WidthRequest="150" VerticalOptions="End" Margin="0,10,0,0"  HorizontalOptions="Center"
                    Style="{StaticResource ValidButtonStyle}" FontSize="15" Padding="5"
                    Text="{extensions:Translate Next}"
                            Command="{Binding ValidateMandatoryCommand}" />

当我在标准按钮中使用ValidateMandatoryCommand命令时,它运行良好。当我在自定义按钮(sharedviews:ButtonLoader)中使用它时,它总是空的。这证实了我的命令ValidateMandatoryCommand工作良好。

我的自定义按钮的XAML看起来像。

 <ContentView.Content>
    <Grid>
        <buttons:SfButton x:Name="btn" Clicked="btn_Clicked" CornerRadius="30" HorizontalOptions="Center" VerticalOptions="Center">
            <buttons:SfButton.Content>
                <StackLayout Orientation="Horizontal" Margin="10">
                    <busyindicator:SfBusyIndicator IsVisible="False" x:Name="busyIndicator" AnimationType="Cupertino" IsBusy="True" TextColor="White" WidthRequest="40" ViewBoxWidth="40"/>
                    <Label x:Name="labelLoader" Text="Loading..." FontSize="15" VerticalTextAlignment="Center" HorizontalOptions="CenterAndExpand" TextColor="White" />
                </StackLayout>
            </buttons:SfButton.Content>
        </buttons:SfButton>
    </Grid>
</ContentView.Content>

我的命令的按钮加载器的C#代码是:


     public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(ButtonLoader), default(ICommand), defaultBindingMode: BindingMode.TwoWay);
        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }

            set
            {
                SetValue(CommandProperty, value);
            }
        }

     private async void btn_Clicked(object sender, EventArgs e)
        {
            labelLoader.Text = TextWaiting;
            busyIndicator.IsBusy = true;
            busyIndicator.IsVisible = true;

            //if (Command != null) <= //**Always NULL**
            //{
                await Task.Run(() => {
                    Command.Execute(null);
                });

            //}


            busyIndicator.IsVisible = false;
            busyIndicator.IsBusy = false;
            labelLoader.Text = TextLabel;
        }

根据评论进行编辑

我的ValidateMandatoryCommand被定义为。

public ICommand ValidateMandatoryCommand => AsyncCommand.Create(ValidateMandatory);

    private async Task ValidateMandatory()
      {}

而AsyncCommand

   public class AsyncCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    private readonly ICommand _command;

    protected AsyncCommand(ICommand command)
    {
        _command = command;
        command.CanExecuteChanged += (sender, args) => CanExecuteChanged.InvokeSafely(sender, args);
    }

    public bool CanExecute(object parameter)
    {
        return _command.CanExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _command.Execute(parameter);
    }

    public static AsyncCommand Create(Func<System.Threading.Tasks.Task> func)
    {
        return new AsyncCommand(new Command(async () => await func()));
    }
}
xamarin mvvm xamarin.forms icommand
1个回答
0
投票

如果你从Button继承了你的View, 那么你需要改变你的BindableProperty的名字. Button类已经有一个同名的属性。

© www.soinside.com 2019 - 2024. All rights reserved.