xamarin 按钮命令无法使用rg.plugin.popup查找绑定的ViewModel

hwamh0ep  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(114)

当试图将视图的属性绑定到ViewModel上创建的任何源代码时,会出现问题,例如标签的Text属性<-binding->测试(下面的代码)不起作用,但Button的Text属性绑定到另一个发送到此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
}

我尝试在按钮绑定中使用Path="”和Source="”,但我不知道如何在这种情况下使用。
项目链接Open/Public on Git
谢谢

vbopmzt1

vbopmzt11#

标签的文本属性<-binding->测试(下面的代码)不工作,但按钮的文本属性我做了一个绑定到另一个对象发送到这个viewModel和工作的正常。我只是想知道为什么绑定不像往常一样工作
首先,你只能绑定属性,不能绑定字段。替换你的代码:

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!";
    }

关于路径="”和源代码="",我做了一个例子给你。
下面是viewmodel类:

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>
</ContentPage>

更详细的信息,大家可以看看:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/binding-path

相关问题