按钮命令未使用 rg.plugin.popup 找到绑定的 ViewModel

Button Command not finding Binded ViewModel using rg.plugin.popup

当尝试将视图的属性绑定到在 ViewModel 上创建的任何源时出现问题,例如标签的文本 属性<-binding->test(下面的代码)不起作用,但是Button 的文本 属性 我绑定到另一个发送到此 viewModel 的对象并且工作正常。我只是想了解为什么 Binding 无法正常工作。 这是代码。

XAML.cs

public PopupView(Func<bool> metodo, Tarefa tarefa)
{
    BindingContext = new ViewModels.Popups.PopupViewModel(metodo, tarefa);
    InitializeComponent();
}

XAML

<pages:PopupPage 
         xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
         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="Taskinho.Views.Popups.PopupView"
         xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup">
    <StackLayout>
        <Label HorizontalOptions="Center" Text="{Binding test}" />
        <Button Text="{Binding tarefa.TarefaTitulo}" Command="{Binding Confirmar}" />
    </StackLayout>
</pages:PopupPage>

视图模型

//Property
private Tarefa _Tarefa;
public Tarefa tarefa
{
    get { return _Tarefa; }
    set { _Tarefa = value;
        NotifyPropertyChanged("Tarefa");
    }
}
//another property
public string test = "Test Name";


//Constructor
public PopupViewModel(Func<bool> metodoParam, Tarefa TarefaParam)
{            
    this.tarefa = new Tarefa();
    ExecutarCommand = new Command(ExecutarAction);
}

//The binded Command(Not finded by the binding)
public Command ExecutarCommand
{
    get;
    set;
}
//Action of Command
void ExecutarAction()
{
    //do something
}

我尝试在 Button 绑定上使用 Path="" 和 Source="",但我不知道如何在这种情况下使用。

项目LinkOpen/Public on Git

谢谢

the label's Text property<-binding->test(below on code) not works, but Text property of Button i make a bind to another object sended to this viewModel and work's normally. I just would like to understand why the Binding is not working as usual

首先,只能绑定属性,不能绑定字段。替换您的代码:

 private string _test;
    public string test
    {
        get { return _test; }
        set
        {
            _test = value;
            NotifyPropertyChanged("test");
        }
    }   
    public PopupViewModel(Func<bool> metodoParam, Tarefa TarefaParam)
    {
        this.tarefa = new Tarefa();
        ExecutarCommand = new Command(ExecutarAction);
        test = "this is test!";
    }

关于 Path="" 和 Source="",我给你做一个例子。

这是视图模型class:

public class viewmodel1: INotifyPropertyChanged
{
    private string _test;
    public string test
    {
        get
        {
            return _test;
        }
        set
        {
            _test = value;
            RaisePropertyChanged("test");
        }
    }
    public  Command command1 { get; set; }

    public viewmodel1()
    {
        test = "this is test!";
        command1 = new Command(method1);
    }

    private void method1()
    {
        Console.WriteLine("this is test!!!!!!!");
    }


    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }


}

视图如下:

<ContentPage
x:Class="demo2.simplecontrol.Page18"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:demo2.simplecontrol">
<ContentPage.BindingContext>
    <model:viewmodel1 x:Name="data1" />
</ContentPage.BindingContext>
<ContentPage.Content>
    <StackLayout>
        <Label
            HorizontalOptions="CenterAndExpand"
            Text="{Binding Path=test, Source={x:Reference data1}}"
            VerticalOptions="CenterAndExpand" />

        <Button Command="{Binding Path=command1, Source={x:Reference data1}}" Text="click1" />
    </StackLayout>
</ContentPage.Content>

更详细的资料,你可以看看:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/binding-path

如果我的回复对您有帮助,请记得将我的回复标记为答案,谢谢