Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新复选框并更改相应的列表项_C#_Xamarin_Mvvmcross - Fatal编程技术网

C# 更新复选框并更改相应的列表项

C# 更新复选框并更改相应的列表项,c#,xamarin,mvvmcross,C#,Xamarin,Mvvmcross,我希望实现以下目标: MyViewModel中的IsDeleteBtnShow属性的功能是,如果项目的任何复选框为true,则显示删除图标。但是,由于复选框的属性可在ListViewModel中访问,因此我需要将此更新从ListViewModel传递到MyViewModel 但是,我在ListItemViewModel中得到了null异常。但是当我调用ListItemViewModelconstructorparent时,它不为空。我想知道我做错了什么 MyViewModel.cs publi

我希望实现以下目标:


MyViewModel
中的
IsDeleteBtnShow
属性的功能是,如果项目的任何复选框为true,则显示删除图标。但是,由于复选框的属性可在
ListViewModel
中访问,因此我需要将此更新从
ListViewModel
传递到
MyViewModel

但是,我在
ListItemViewModel
中得到了null异常。但是当我调用
ListItemViewModel
constructor
parent
时,它不为空。我想知道我做错了什么

MyViewModel.cs

 public class MyViewModel:MvxViewModel
 {

    private ObservableCollection<ListItemViewModel> _myListViews;

    public ObservableCollection<ListItemViewModel> MyListViews
    {
        get { return _myListViews; }
        set
        {
            _myListViews= value;
            RaisePropertyChanged(() => MyListViews);
        }
    }

    private bool _isDeleteBtnShow = false;
    public bool IsDeleteBtnShow
    {
        get {
            return _isDeleteBtnShow;
        }
        set {
            _isDeleteBtnShow = value;
            RaisePropertyChanged(() => IsDeleteBtnShow);
        }
    }

    public void Init(string myId)
    {
         List<ListItemViewModel> allListItems = new List<ListItemViewModel>();
         allListItems = _myService.GetAllItems(myId);

         foreach (var myTest in allListItems)
         {
           _myListViews.Add(ListItemViewModel.CreateViewModel(myTest));
         }
        ListItemViewModel obj = new ListItemViewModel(this);

      }
   }
public class ListItemViewModel
{
    public int Id { get; set; }
    public string Text { get; set; }
    private bool _isChecked;
    public DateTime Date { get; set; }

    readonly MyViewModel _parent;

    public ListItemViewModel()
    {
        // parameterless constructor
    }

    public ListItemViewModel(MyViewModel parent)
    {
      // _parent is not null here
        _parent = parent;
    }

   public static ListItemViewModel CreateViewModel(Test entity)
    {
        if (entity == null)
        {
            return null;
        }
        return new ListItemViewModel
        {
            Date = entity.Date,
            IsSelected = entity.IsSelected,
         };
    }

     public ICommand CheckBoxSelectionCommand
    {
        get
        {
            return new MvxCommand(() =>
            {
                var isChecked = IsSelected;
                // the following _parent is null
                _parent.IsDeleteBtnShow = true;
            });
        }
    }

    private bool _isSelected;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            RaisePropertyChanged(() => IsSelected);
        }
    }

    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
        }
    } 
}

由于调用
CreateViewModel
创建视图模型时使用了
ListItemViewModel
的无参数构造函数,因此会出现异常

您需要向其添加
MyViewModel
参数,并在创建
ListItemViewModel
时传递视图模型:

     List<ListItemViewModel> allListItems = new List<ListItemViewModel>();
     allListItems = _myService.GetAllItems(myId);

     foreach (var myTest in allListItems)
     {
          _myListViews.Add(ListItemViewModel.CreateViewModel(this, myTest));
     }
    public static ListItemViewModel CreateViewModel(MyViewModel parent, Test entity)
    {
        if (entity == null)
        {
            return null;
        }
        return new ListItemViewModel(parent)
        {
            Date = entity.Date,
            IsSelected = entity.IsSelected,
        };
    }

由于调用
CreateViewModel
创建视图模型时使用了
ListItemViewModel
的无参数构造函数,因此会出现异常

您需要向其添加
MyViewModel
参数,并在创建
ListItemViewModel
时传递视图模型:

     List<ListItemViewModel> allListItems = new List<ListItemViewModel>();
     allListItems = _myService.GetAllItems(myId);

     foreach (var myTest in allListItems)
     {
          _myListViews.Add(ListItemViewModel.CreateViewModel(this, myTest));
     }
    public static ListItemViewModel CreateViewModel(MyViewModel parent, Test entity)
    {
        if (entity == null)
        {
            return null;
        }
        return new ListItemViewModel(parent)
        {
            Date = entity.Date,
            IsSelected = entity.IsSelected,
        };
    }

一旦用户更改了它,我想更新-你的意思是当ViewModel通过您正在使用的框架从用户那里返回时?这是调用所需的构造函数还是默认构造函数?
IsDeleteBtnShow
属性中的
MyViewModel
是在项目的任何复选框相应为true或false时显示或隐藏delete图标。但是,由于复选框的属性可在
ListViewModel
中访问,因此我需要将此更新从
ListViewModel
传递到
MyViewModel
。一旦用户更改它,我想进行更新-您的意思是当ViewModel通过您使用的框架从用户返回时?这是调用所需的构造函数还是默认构造函数?
IsDeleteBtnShow
属性中的
MyViewModel
是在项目的任何复选框相应为true或false时显示或隐藏delete图标。但是,由于复选框的属性可在
ListViewModel
中访问,因此我需要将此更新从
ListViewModel
传递到
MyViewModel
CreateViewModel
只需要一个参数。请给我5分钟,我现在正在重建解决方案并将其部署到XAP。
CreateViewModel
只接受一个参数。请给我5分钟,我现在正在重建解决方案并将其部署到XAP。