【问题标题】:ViewCell execue command from ViewModelViewCell 从 ViewModel 执行命令
【发布时间】:2021-06-01 09:41:22
【问题描述】:

我想从具有 ViewModel 的 ViewCell 中绑定我的 MenuItem。 这是我的视图单元格,有人知道这种绑定的正确方法吗? 我有一个ObservableCollection<object> 填充我的ListView,这是我需要绑定的单元格。

<?xml version="1.0" encoding="UTF-8" ?>
<ViewCell
    x:Class="DataTemp.Controls.DeviceViewCell"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Name="this">
    <ViewCell.ContextActions>
        // I've been trying these two ways
        <MenuItem Command="{Binding Source={x:Reference this}, Path=BindingContext.DeviceCommand}" Text="Call" />
        <MenuItem Command="{Binding DeviceCommand}" Text="Call" />
    </ViewCell.ContextActions>
    <ViewCell.View>

        <StackLayout Padding="10">
            <Label Text="{Binding Name}" />
            <Label Text="{Binding Number}" />

        </StackLayout>
    </ViewCell.View>
</ViewCell>

这是我的视图模型 我试图用断点捕捉事件,但我从未进入 OnDeviceCommnad 方法。

public class DeviceViewCellViewModel : BaseViewModel, IDevice
    {
        public string Number
        {  
            get
            {
                return _numberDevice;
            }
            set
            {
                _numberDevice = value;
                OnPropertyChanged("Number");
             }
        }
        public string Name
        {
            get
            {
                return _nameDevice;
            }
            set
            {
                _nameDevice = value;
                OnPropertyChanged("Number");
            }
        }

        public ICommand DeviceCommand { get; set; }
        private string _nameDevice;
        private string _numberDevice;
         

        public DeviceViewCellViewModel()
        {
            DeviceCommand = new Command(OnDeviceCommnad);
        }

        private void OnDeviceCommnad()
        {
            
        }
    }

【问题讨论】:

  • 输出控制台中是否显示任何绑定错误?例如:[0:] Binding: 'LoginClickeddCommand' property not found on 'xxxx.ViewModels.LoginViewModel', target property: 'xxxx.Controls.CustomButton.Command'
  • 不,一点也不,但也不让我执行命令@Andrew
  • 嗨,ViewCell 里面的标签是否显示?您可以在这里分享示例项目链接,我会检查一下。
  • @JuniorJiang-MSFT 是的先生,我把仓库留在这里:github.com/Gatoreno/DateTemplatePractice/blob/main/DataTemp/…
  • @E.Rawrdríguez.Ophanim 感谢分享,我已经更新了答案。有时间可以去看看。

标签: xamarin.forms mvvm data-binding datatemplate


【解决方案1】:

如果你不需要传递参数,第二种方式应该可以工作。

<MenuItem Command="{Binding DeviceCommand}" Text="Call" />

在模型中的示例代码可以如下:

public Command DeviceCommand { get; private set; }

public DeviceViewCellViewModel()
{
    DeviceCommand = new Command(() =>
    {
        // Execute logic here
    });
}

==============================更新================= =================

从共享示例中,我发现绑定的MainViewModel 没有使用DeviceViewCellViewModel,因此不会调用该命令。

修改MainViewModel里面的如下代码:

PaymentsAndDevices.Add(new Devices()
{
    Number = rdn.Next().ToString(),
    Name = "I'm a device"
});

如下:

PaymentsAndDevices.Add(new DeviceViewCellViewModel()
{
    Number = rdn.Next().ToString(),
    Name = "I'm a device"
});

然后OnDeviceCommnad方法将被调用。

【讨论】:

  • @E.Rawrdríguez.Ophanim 我找到了问题,我会在答案中更新。
猜你喜欢
  • 2016-02-17
  • 1970-01-01
  • 2019-10-09
  • 2012-04-21
  • 2011-05-05
  • 2012-07-15
  • 2017-07-04
  • 2011-06-15
  • 2018-08-04
相关资源
最近更新 更多