SfListView's ViewModel Not Available During Behaviour's OnAttachedTo() When Implementing Sorting/Filtering

Hi,
I am using FreshMvvm in my Xamarin Forms project (Android only at present), and have been implementing the sorting/filtering behaviour to an SfListView, as per the SortingFiltering demo project. In the demo project an instance of the page (view)  model is created within the behaviour's OnAttachedTo() method.
        protected override void OnAttachedTo(SampleView bindable)
        {
            ListView = bindable.FindByName<Syncfusion.ListView.XForms.SfListView>("listView");
            sortingFilteringViewModel = new SortingFilteringViewModel();

I don't want to do this as I have (or will have) a pagemodel in memory who's properties are already reflected within the page's views. Instead I have been attempting to access the existing page model.
        protected override void OnAttachedTo(SelectMachinePage xbindable)
        {
            ListView = xbindable.FindByName<SfListView>("listView");
            sortingFilteringViewModel = (SelectMachinePageModel)xbindable.BindingContext;

But it seems that at the time OnAttachedTo() is called, the BindingContext seems to be null. This is true whether I assign the behaviour to the page within xaml, or in code behind within the page's constructor. My workaround for this is to instead assign the behaviour during the page's OnAppearing() method (only available due to me using FreshMvvm).
        protected override void OnAppearing()
        {
            this.Behaviors.Add(new SelectMachineListViewFilteringBehaviour());
        }

I have previously started a thread to which the conclusion was that it was a defect (perhaps only apparent due to my use of FreshMvvm) and that it would be fixed in a new SyncFusion release soon. Do you think this is related to the defect, something new, or am I attempting something that would never be possible? Since FreshMvvm is creating the page and the page model and assigning it's BindingContext, perhaps this is something I should be asking it's author? (just thought I'd check if you thought it was the same defect first).

Thanks in advance.

Paul








2 Replies

PP Paul Parkins August 30, 2018 01:07 PM UTC

Having thought about this further and additional researching, I think my workaround is probably the best solution. When I use FeshMvvm's PushPageModel() method to display the page, it creates instances of the page and the page model, then binds them. Therefore the binding has not yet  occurred during the construction of either the page or the page model, and so if the behaviour is assigned during the page construction (xaml or in constructor), then the binding is just not there yet
        public async Task PushPageModel<T, TPage> (object data, bool modal = false, bool animate = true) where T : FreshBasePageModel where TPage : Page
        {
              T pageModel = FreshIOC.Container.Resolve<T> ();
     TPage page = FreshIOC.Container.Resolve<TPage>();
     FreshPageModelResolver.BindingPageModel(data, page, pageModel);
          await PushPageModelWithPage(page, pageModel, data, modal, animate);
        }

        public static Page BindingPageModel(object data, Page targetPage, FreshBasePageModel pageModel)
        {
            pageModel.WireEvents (targetPage);
            pageModel.CurrentPage = targetPage;
            pageModel.CoreMethods = new PageModelCoreMethods (targetPage, pageModel);
            pageModel.Init (data);
            targetPage.BindingContext = pageModel;
            return targetPage;
        }            

I have proven that if I define both the binding context and the behaviour assignment in xaml, then my page model is available during the behaviour's OnAttachedTo()
    <local:HandheldBasePage.BindingContext>
        <models:SelectMachinePageModel />
    </local:HandheldBasePage.BindingContext> 
    
    <local:HandheldBasePage.Behaviors>
        <behaviours:SelectMachineListViewFilteringBehaviour/>
    </local:HandheldBasePage.Behaviors>

However since I am using FreshMvvm's PushPageModel, this does cause 2 instances of my page model being created (once due to the xaml declaration, and once due to FreshMvvm)


So I no longer suspect this to be a Syncfusion defect, and will continue to use my workaround.
Will leave the thread here in case anybody has any other suggestions, or in case it helps anoyone else.

Paul





JN Jayaleshwari N Syncfusion Team August 31, 2018 11:11 AM UTC

Hi Paul, 
 
Thanks for the update.  
Please let us know if you need any other assistance on this. 
 
Regards, 
Jayaleshwari N. 


Loader.
Up arrow icon