【问题标题】:Button Command not finding Binded ViewModel using rg.plugin.popup按钮命令未使用 rg.plugin.popup 找到绑定的 ViewModel
【发布时间】:2020-02-27 06:30:23
【问题描述】:

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

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="" 但我不知道如何用于这种情况。

项目链接Open/Public on Git

谢谢

【问题讨论】:

  • 如果我的回复解决了您的问题,请记得将我的回复标记为答案,谢谢。

标签: xamarin mvvm xamarin.forms popup


【解决方案1】:

标签的 Text 属性test(代码如下)不起作用,但 Button 的 Text 属性绑定到发送到此视图模型的另一个对象并且正常工作。我只是想了解为什么绑定无法正常工作

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

 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="",我给你做一个样例。

这是视图模型类:

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>

更详细的inf,大家可以看看:

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

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

【讨论】:

  • 是啊!!,现在解决了!!太感谢了!我刚刚尝试使用您向我展示的相同代码。将 &lt;Content.BindingContext&gt; 更改为 &lt;pages: PopupPage.BindingContext&gt; 并在 XAML 中添加: 1- xmlns:Page="clr-namespace:Taskinho.ViewModels.Popups" 2- &lt;Page:PopupViewModel /&gt; 用于将 ViewModel 作为 BindingContext 传递。
猜你喜欢
  • 2011-09-17
  • 2020-05-28
  • 2012-09-03
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 2013-10-09
  • 1970-01-01
相关资源
最近更新 更多