Button命令未使用rg.plugin.popup找到绑定ViewModel



当尝试将视图的属性绑定到在ViewModel上创建的任何源时,就会出现此问题,例如标签的Text属性&lt-binding->test(下面的代码(不起作用,但Button的Text属性i绑定到发送到此viewModel的另一个对象,工作正常。我只是想了解为什么绑定没有像往常一样工作。这是代码。

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>

ViewModel

//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=",但我不知道如何在这种情况下使用。

项目链接在Git 上打开/公开

谢谢

标签的Text属性&lt-绑定-&gt;测试(下面的代码(不起作用,但Button的Text属性我绑定到另一个发送到此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!";
}

关于路径="并且Source="&";,我为你做一个样品。

这是视图模型类:

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

最新更新