Xamarin.forms 将命令传递给ContentView中的按钮

Xamarin.forms 将命令传递给ContentView中的按钮,xamarin.forms,Xamarin.forms,我有一个ContentView,在网格中有一个按钮。我为按钮上的文本值和命令创建了bindable属性。文本值设置正确,但按钮设置不正确?我不明白这里发生了什么。有人有什么见解吗 <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://

我有一个
ContentView
,在网格中有一个按钮。我为按钮上的文本值和命令创建了bindable属性。文本值设置正确,但按钮设置不正确?我不明白这里发生了什么。有人有什么见解吗

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Aboo.Components.BusyButton"
             BindingContext="{Binding Source={RelativeSource Self}}">
  <ContentView.Content>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button x:Name="Button" Text="{Binding Text}" Command="{Binding Command}" Grid.ColumnSpan="3" />
        <ActivityIndicator Grid.Column="1" IsRunning="{Binding IsBusy}" Color="White"/>
      </Grid>
  </ContentView.Content>
</ContentView>

使用如下按钮:

请删除
BindingContext=“{Binding Source={RelativeSource Self}}”
此行

添加
Content.BindingContext=thisBusyButton
构造函数中的code>。根据我的测试
BindingContext=“{Binding Source={RelativeSource Self}}”
在nasted绑定中不起作用

请在
ContentView
中为
按钮添加名称
x:name=“MyButton”
,然后在
IsBusyChanged
方法中更改按钮名称

这是我的试卷

这是我的代码。
BusyButton.xaml

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App2.BusyButton"

             x:Name="MyContentview"
             >
    <ContentView.Content>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button x:Name="MyButton" Text="{Binding Text}" Command="{Binding Command}" Grid.ColumnSpan="3" />
            <ActivityIndicator Grid.Column="1" IsRunning="{Binding IsBusy,Mode=TwoWay}" Color="White"/>
        </Grid>
    </ContentView.Content>
</ContentView>
我在
MainPage.xaml
中测试它

   <StackLayout>
        <!-- Place new controls here -->
        <local:BusyButton x:Name="busyButton" Text="{Binding Name}" Command="{Binding ChangeCommand}" IsBusy="{Binding Changed, Mode=TwoWay}"></local:BusyButton>


    </StackLayout>

这是我的测试
MyModelView

    public class MyModelView: INotifyPropertyChanged
    {
        public ICommand ChangeCommand { protected set; get; }


        public string Name { get; set; }
        bool _changed = false;


        public bool Changed
        {
            get
            {
                return _changed;
            }

            set
            {
                if (_changed != value)
                {
                    _changed = value;
                    OnPropertyChanged("Changed");

                }
            }

        }
        public MyModelView() {

            Name = "test";


           ChangeCommand = new Command(() =>
            {

                Changed = true;
                Console.WriteLine("================test Command execute=================");
            });
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
这是我的模式


@Inrego是否有此问题的更新?我更新了我的演示,你可以参考它。如果回复有帮助,请将其标记为答案(单击“✔” 在这个答案的左上角),它将帮助其他有类似问题的人。嗨,我刚刚测试过,它确实很有帮助!
   <StackLayout>
        <!-- Place new controls here -->
        <local:BusyButton x:Name="busyButton" Text="{Binding Name}" Command="{Binding ChangeCommand}" IsBusy="{Binding Changed, Mode=TwoWay}"></local:BusyButton>


    </StackLayout>

 public partial class MainPage : ContentPage
    {

        public MainPage()
        {
            InitializeComponent();
            Content.BindingContext = new MyModelView();



        }
    }
    public class MyModelView: INotifyPropertyChanged
    {
        public ICommand ChangeCommand { protected set; get; }


        public string Name { get; set; }
        bool _changed = false;


        public bool Changed
        {
            get
            {
                return _changed;
            }

            set
            {
                if (_changed != value)
                {
                    _changed = value;
                    OnPropertyChanged("Changed");

                }
            }

        }
        public MyModelView() {

            Name = "test";


           ChangeCommand = new Command(() =>
            {

                Changed = true;
                Console.WriteLine("================test Command execute=================");
            });
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }