Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Rg.plugins Popup with FreshMVVM callback issue

stoneyrichstoneyrich Member ✭✭

When the savecommand is triggered I do not get the Event OnOrderItemUpdated fired to the calling PageModel within a Tabbed Page.

//UpdateOrderPageModel Event

public event EventHandler OrderItemUpdated;

    protected virtual void OnOrderItemUpdated(OrderItemEventArgs e)
    {
        EventHandler<OrderItemEventArgs> handler = OrderItemUpdated;
        handler?.Invoke(this, e);
    }

//Save Command in UpdateOrderPageModel Class
var e = new OrderItemEventArgs(new OrderItem());
OnOrderItemUpdated(e);

//Calling Tabbed Page Model

public OrderItem SelectedOrderItem
{
get { return _selectedOrderItem; }
set {
_selectedOrderItem = value;
Task.Run(async () => await PopoUpdatePage());
RaisePropertyChanged(nameof(SelectedOrderItem));
}
}

    private async Task PopoUpdatePage()
    {
        //Private Property UpdateOrder 
        this.UpdateOrder = FreshIOC.Container.Resolve<UpdateOrderPageModel>();
        UpdateOrder.OrderItemUpdated += U_OrderItemUpdated;          
        await CoreMethods.PushPopupPageModel<UpdateOrderPageModel>(SelectedOrderItem,true);
    }

This is not working OnOrderItemUpdated not firing ..

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    edited May 2020

    I'm still confused about the logic you posted. Could you please post a small sample to specify this?
    But I saw you registered the OrderItemUpdated event in the thread, try to put it on the main thread like:

    Device.BeginInvokeOnMainThread(() =>
    {
        this.UpdateOrder = FreshIOC.Container.Resolve<UpdateOrderPageModel>();
        UpdateOrder.OrderItemUpdated += U_OrderItemUpdated;  
        // ... 
    });
    

    And could you make sure the register instruction is completed before the firing method?

  • stoneyrichstoneyrich Member ✭✭
    edited May 2020

    @LandLu

        private async Task PopoUpdatePage()
        {
                    Device.BeginInvokeOnMainThread(async () =>
                    {
                        this.UpdateOrder = FreshIOC.Container.Resolve<UpdateOrderPageModel>();
                        UpdateOrder.OrderItemUpdated += U_OrderItemUpdated;
                        await CoreMethods.PushPopupPageModel<UpdateOrderPageModel>(SelectedOrderItem, true);
                    });
        }
    
        //This event does not get called
        private void U_OrderItemUpdated(object sender, OrderItemEventArgs e)
        {
                    throw new NotImplementedException();
        }
    

    Still doesn't work.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Could you please share a small sample to help me spot this issue?
    I want to know more information about the tabbed page and how you trigger this event.

  • stoneyrichstoneyrich Member ✭✭

    @LandLu Firstly I have an OrderPageModel with is the Tabbed Page Model

         public void OrderPageInit(Table table)
                {
    
                     this.AddOrder = FreshIOC.Container.Resolve<AddOrderPageModel>();
                     this.MyOrder = FreshIOC.Container.Resolve<MyOrderPageModel>(); 
         }
    

    Secondly MyOrderPageModel a tab page triggers the Popup UpdateOrderPageModel

     public OrderItem SelectedOrderItem
            {
                get { return _selectedOrderItem; }
                set
                {
                    _selectedOrderItem = value;
                    Task.Run(async () => await PopoUpdatePage());
                    RaisePropertyChanged(nameof(SelectedOrderItem));
                }
            }
    
            private async Task PopoUpdatePage()
            {
                Device.BeginInvokeOnMainThread(async () =>
                {
                    this.UpdateOrder = FreshIOC.Container.Resolve<UpdateOrderPageModel>();
                    UpdateOrder.OrderItemUpdated += U_OrderItemUpdated;
                    await CoreMethods.PushPopupPageModel<UpdateOrderPageModel>(SelectedOrderItem, true);
    
                });
                       }
    
            private void U_OrderItemUpdated(object sender, OrderItemEventArgs e)
            {
                throw new NotImplementedException();
            }
    

    Thirdly on the Page UpdateOrderPageModel that is where I trigger the event and here is the entire class below

    public class UpdateOrderPageModel : FreshBasePageModel
        {
            public OrderItem updateThisOrderItem { get; set; }
            public event EventHandler<OrderItemEventArgs> OrderItemUpdated;
    
            protected virtual void OnOrderItemUpdated(OrderItemEventArgs e)
            {
                EventHandler<OrderItemEventArgs> handler = OrderItemUpdated;
                handler?.Invoke(this, e);
            }
    
            public override void Init(object initData)
            {
                base.Init(initData);
            }
    
            public UpdateOrderPageModel(OrderItem selectedOrderItem)
            {
                updateThisOrderItem = selectedOrderItem;
            }
    
            private int _qty;
    
            public int Qty
            {
                get { return _qty; }
                set { _qty = value; RaisePropertyChanged(nameof(Qty)); }
            }
    
            private string _note;
    
            public string Note
            {
                get { return _note; }
                set { _note = value; RaisePropertyChanged(nameof(Note)); }
            }
    
            public ICommand SaveCommand => new Command(() =>
            {                
                OnOrderItemUpdated(new OrderItemEventArgs());
            });
    
            public Command ClosePopupCommand
            {
                get
                {
                    return new Command(async () =>
                    {
                        await CoreMethods.PopPopupPageModel();                  
                    });
                }
            }
        }
    
Sign In or Register to comment.