Refresh in a View from a Viewmodel

Asked

Viewed 148 times

0

Team, I have a question. I am developing a detail screen of an order, where there is a button used to take this order.

To make it clearer, this detail screen behaves in different ways to different status:

using CoreGraphics;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Core.ViewModels;
using MvvmCross.iOS.Views;
using UIKit;
using Valdemar.Core.ViewModels;

namespace Valdemar.IOs
{
    public sealed partial class DetailView : MvxViewController<DetailViewModel>
    {
        private bool _constructed;

        public DetailView() : base("DetailView", null)
        {
            ViewDidLoad();
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

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

            if (ViewModel == null)
            {
                return;
            }

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

            /*
             * Binding
             */
            var set = this.CreateBindingSet<DetailView, Core.ViewModels.DetailViewModel>();

            set.Bind(lblWhat).To(vm => vm.Item.itemsDes);
            set.Bind(lblHowMuch).To(vm => vm.Item.valueDes);
            set.Bind(lblWhen).To(vm => vm.Item.limitTime);
            set.Bind(lblStatus).To(vm => vm.Item.status);
            set.Bind(lblUser).To(vm => vm.Item.usersTOByIdClientUser.personsTO.namePerson);
            //set.Bind(lblAdress).To(vm => vm.Item.usersTOByIdClientUser);
            set.Bind(lblResponsible).To(vm => vm.Item.valdecoOwner);
            set.Bind(lblConfirmed).To(vm => vm.Item.confirmed);
            set.Bind(lblCreated).To(vm => vm.Item.created);
            set.Bind(close).To(vm => vm.CloseCommand);
            set.Bind(close).For("Clicked").To(vm => vm.CloseCommand);

            if (lblStatus.Text == "A")
            {
                btnAction.SetTitle("Assumir Pedido!", UIControlState.Normal);
                set.Bind(btnAction).To(vm => vm.BtnAssumeCommand);
                lblStatus.Hidden = true;
                lblConfirmation.Hidden = true;
                lblResponsible.Hidden = true;
            }
            else if (lblStatus.Text == "V")
            {
                set.Bind(lblStatus).To(vm => vm.V);
                btnAction.SetTitle("Entreguei!", UIControlState.Normal);
                set.Bind(btnAction).To(vm => vm.BtnDeliverCommand);
                lblConfirmation.Hidden = true;
            }
            else if (lblStatus.Text == "E")
            {
                set.Bind(lblStatus).To(vm => vm.E);
                lblConfirmation.Hidden = false;
                btnAction.Hidden = true;
            }
            else if (lblStatus.Text == "C")
            {
                lblConfirmation.Hidden = true;
                btnAction.Hidden = true;
                lblConfirmed.Hidden = false;
                set.Bind(lblStatus).To(vm => vm.C);
            }

            set.Apply();
        }
    }
}

When I trigger the button command, I will have to change the status of my request, and consequently hide the "Take Over button".

private async Task<bool> assumeOrder()
{
    var response = await orderService.assumeResp(getOrder());

    if (response == null)
    {
        messageService.showMessage("Pedido " + Item.id + " assumido com sucesso!");

        orderService.deleteAvailableOrder(Item);

        RaisePropertyChanged(() => Item);

        return false;
    }

    return true;
}

What’s the best way to do it, especially the "disappear" button? I am trying through Raisepropertychanged(() => Item), it updates the properties, but does not access the View again to change it according to its new Status.

  • I don’t know if I got it right xaml would have to define the property IsVisible="{Binding OcultarBotao, Mode=TwoWay}"to hide a button.

1 answer

0

Dude, I haven’t had to do anything like this for Xamarin.Ios specifically and if I get it you want the button to be active or not according to the validation of it, right ? if so it would not be the case that you assign a Canexecute in Btnassumecommand ?

It would look something like this:

public ICommand DeleteItemCommand{
get { 
    return m_DeleteItemCommand ?? (m_DeleteItemCommand = new MvxCommand<int>(ExecDeleteItem,CanExecDeleteItem)); 
}}

public void ExecDeleteItem(int index){
Mvx.Trace($"Deleting item at {index}");}


public bool CanExecDeleteItem(int index){Mvx.Trace($"Checking removal at {index}");return index > 0;}

See full example here.

I’m giving you this information based on what I’ve done in Xamarin.Forms, as I said I haven’t done anything like this on Ios, but I hope I’ve helped.

OBS. The example code did not enter as "Code sample" when it was idented, so it is this way.

  • 1

    Brian, the links break and can leave the answer here unusable. The intention here is to have the content within the site itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.