Current filter:
                                You should refresh the page.
                                  • Hi,
                                    I noticed several problems in our project when deactivating controllers. After debuging it I found this strange behavior:

                                    [C#]
                                    public class Controller1 : ViewController<ListView> { public Controller1() { TargetObjectType = typeof(Class1); } protected override void OnActivated() { View.CollectionSource.Criteria[nameof(Controller1)] = CriteriaOperator.Parse("Name like 'Test%'"); base.OnActivated(); } protected override void OnDeactivated() { base.OnDeactivated(); View.CollectionSource.Criteria.Remove(nameof(Controller1)); } }

                                    When you add this simple controller to add additinal criterias to a ListView collection, it will reload the collection from database on closing thew view (I verified it with database profilers, the collection is completely reloaded without that criteria). However, if you do not remove the criteria in OnDeactivate, the criteria will stay if the controller is deactivated by any other means.

                                    After some testing i found this solution to be working in any scenarios (SingleMDI Xaf, MultiMDI Xaf, View Closing, Activating / Deactivating Controller, etc)

                                    [C#]
                                    public class Controller1 : ViewController<ListView> { public Controller1() { TargetObjectType = typeof(Class1); } protected override void OnActivated() { View.CollectionSource.Criteria[nameof(Controller1)] = CriteriaOperator.Parse("Name like 'Test%'"); base.OnActivated(); _viewClosing = false; View.Closing += View_Closing; Frame.ViewChanging += Frame_ViewChanging; } bool _viewClosing = false; private void View_Closing(object sender, EventArgs e) { _viewClosing = true; } protected override void OnDeactivated() { base.OnDeactivated(); if (!_viewClosing) View.CollectionSource.Criteria.Remove(nameof(Controller1)); View.Closing -= View_Closing; Frame.ViewChanging -= Frame_ViewChanging; } private void Frame_ViewChanging(object sender, ViewChangingEventArgs e) { if(View != null) View.Closing -= View_Closing; } }

                                    However, this seems to be really complicated to simply activate / deactivate a controller properly. Why is this deactivate behavior so strange? Is there a more simple way to make sure that a ListView will never reload its collection during a ViewClosing event, no matter what the controllers do?

                                    Btw, i remember in the past i once had a similar problem with reactivating an action during OnDeactivated of a controller creating a new WindowHandle of the main Frame during application closing (the Frame was already HandleDestroyed when the Action changed back to Visible= true which recreated the Handel of the Frame). But i don't recall the exact scenarior nor do i have a sample project for that.

                                    Stefan

                                • Dave Hesketh (Llamachant Technology) 11.08.2019

                                  Hi Stefan,

                                  If you're closing the view which contains the collection source, then why are you removing the criteria? There should be no need as the collection source will be reloaded without the controller specific criteria the next time the view opens anyway.

                                  Just a thought!

                                  -Dave

                                • Stefan Schäfer 11.11.2019

                                  Hi Dave,
                                  it's true that there is no need to remove the criteria on view closing. My issue is that there is a big problem if you do it anyway. Luckily there is no need, that’s why I was able to build this workaround that only does remove the criteria if the controller was deactivated by any other mean. (for example, one customer project does not need the functionality of a base controller and therefore deactivates it)
                                  But I’m unhappy about this because it basically means: I cannot use the OnDeactivate in any controller for any cleanup in view collection criteria’s, as well as in Active and Enable Boollists of Actions of other Controllers as well as Active of other Controllers, while deregistering off normal events still needs to stay in OnDeactivated.
                                  So all the hundreds / thousands of controllers in all our projects needs to be changed to this pattern. But if that is the intended behavior, then that’s ok, I guess.
                                  Stefan

                                1 Solution

                                Creation Date Importance Sort by

                                Hello Stafan,

                                The collection is reloaded in response to the criterion change because the view control is yet bound to the collection when the controller is deactivated. The control is notified about the collection criterion change and reloads data. We cannot change this behavior without breaking the existing functionality.
                                Your current solution is correct. In a WinForms controller, you can check the WinWindow.IsClosing property instead.