Xamarin MVVMCross更改列表内容

时间:2017-05-10 13:07:01

标签: c# ios listview xamarin mvvmcross

我使用MVVMCross 4.4.0,我的FirstView是像WhatsApp一样的列表视图。当我单击一个项目时,我的SecondView是一个常见视图,只有一个字段来更新属性并更改我的FirstView,但这不会发生。

我的第一个查看代码

.select("div[id~=content$], div[class~=content$]");

和ViewModel

public sealed partial class MyOrdersView : MvxViewController
{
    UITableView tableView;

    public MyOrdersView() : base("MyOrdersView", null)
    {
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        tableView = new UITableView
        {
            Frame = new CoreGraphics.CGRect(0, 0, View.Bounds.Width, View.Bounds.Height),
            CellLayoutMarginsFollowReadableWidth = true,
        };
        Add(tableView);

        var tableSource = new MvxSimpleTableViewSource(tableView, OrderCell.Key, OrderCell.Key);
        tableView.Source = tableSource;


        var set = this.CreateBindingSet<MyOrdersView, Core.ViewModels.MyOrdersViewModel>();

        set.Bind(tableSource).To(vm => vm.Tasks);
        set.Bind(tableSource).For(s => s.SelectionChangedCommand).To(vm => vm.ItemSelectedCommand);
        set.Apply();

        tableView.ReloadData();

        Title = "Meus Pedidos";

        AppDelegate app = UIApplication.SharedApplication.Delegate as AppDelegate;

        NavigationItem.SetLeftBarButtonItem(
            new UIBarButtonItem(UIImage.FromBundle("threelines")
                , UIBarButtonItemStyle.Plain
                , (sender, args) => app.SidebarController.ToggleMenu()), true);
    }
}

我的第二个查看代码

public class MyOrdersViewModel : BaseViewModel
{
    private IOrderService orderService;

    public ObservableCollection<ValOrders2TO> tasks;

    private MvxCommand<ValOrders2TO> _itemSelectedCommand;

    public MyOrdersViewModel(IOrderService orderService)
    {
        this.orderService = orderService;

        tasks = orderService.getAvailableOrders();

        searchMyOrders();
    }

    private async Task<bool> searchMyOrders()
    {
        orderService.clearAvailableOrders();

        orderService.addAllAvailableOrders(await orderService.searchMyOrders());

        return true;
    }

    public ObservableCollection<ValOrders2TO> Tasks
    {
        get { return tasks; }
        set { tasks = value; RaisePropertyChanged(() => Tasks); }
    }

    public ICommand ItemSelectedCommand
    {
        get
        {
            _itemSelectedCommand = _itemSelectedCommand ?? new MvxCommand<ValOrders2TO>(DoSelectItem);

            return _itemSelectedCommand;
        }
    }
    private void DoSelectItem(ValOrders2TO item)
    {
        ShowViewModel<MyOrdersDetailModel>(new { idOrder = item.id });
    }
}

ViewModel

public sealed partial class MyOrdersDetailView : MvxViewController<MyOrdersDetailModel>
{
    public MyOrdersDetailView() : base("MyOrdersDetailView", null)
    {
        //ViewDidLoad();
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var btnBack = UIButton.FromType(UIButtonType.System);
        btnBack.Frame = new CGRect(2, 35, 50, 20);
        btnBack.SetTitle("Voltar", UIControlState.Normal);
        Add(btnBack);

        var set = this.CreateBindingSet<MyOrdersDetailView, Core.ViewModels.MyOrdersDetailModel>();

        set.Bind(txtItemDes).To(vm => vm.txtItemDes);
        set.Bind(btnUpdate).To(vm => vm.update);

        set.Apply();
    }
}

这是我的Bean Code

public class MyOrdersDetailModel : MvxViewModel
{
    public ValOrders2TO _valItem;

    public INC<string> txtItemDes = new NC<string>();

    public MyOrdersDetailModel(IMessageService messageService, IOrderService orderService)
    {
    }

    public ValOrders2TO Item
    {
        get { return _valItem; }
        set { _valItem = value; RaisePropertyChanged(() => Item); }
    }

    public void Init(ValOrders2TO item)
    {
        Item = item;

        ConfigProp();
    }

    void ConfigProp()
    {
        txtItemDes.Value = Item.itemsDes;
    }

    public IMvxCommand Update
    {
        get
        {
            return new MvxCommand(() => updateOrder());
        }
    }
    private bool updateOrder()
    {
        Item.itemsDes = txtItemDes.Value;

        return true;
    }

    public IMvxCommand CloseCommand
    {
        get { return new MvxCommand(() => Close(this)); }
    }
}

这是我的OrderCell

public class ValOrders2TO
{
    //[JsonProperty()]
    public int id { get; set; }

    //[JsonProperty()]
    public string itemsDes { get; set; }
}

我知道如何从ListView中删除项目,如下所示:

public partial class OrderCell : MvxTableViewCell
{
    public static readonly UINib Nib = UINib.FromName("OrderCell", NSBundle.MainBundle);
    public static readonly NSString Key = new NSString("OrderCell");

    private readonly MvxImageViewLoader _loader;

    public OrderCell(IntPtr handle) : base(handle)
    {
        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<OrderCell, Core.ValOrders2TO>();

            set.Bind(lblItemDes).To(item => item.itemsDes);

            set.Apply();
        });
    }

    public static OrderCell Create()
    {
        return (OrderCell)Nib.Instantiate(null, null)[0];
    }
}

但我不知道如何更新商品清单。

关上我的打印屏幕 enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试为

实施RaisePropertyChanged
public string itemsDes { get; set; }