How to reuse visual elements created for document and tool

Docking/MDI for Avalonia Forum

Posted 4 days ago by TYTYXI
Version: 25.2.1
Avatar

I am using MVVM pattern to create View from VIewModes, and i want one ViewModel instance corresponds to one instance of a View. But i can not find some samples about this.

This means when i drag a ToolView into another window, it will create a new ToolView, not reuse the one has been created last time.

[Modified 4 days ago]

Comments (4)

Posted 4 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

When using the MVVM pattern in a single DockSite, it should have a one-to-one correspondence between ViewModel and View.  For instance, if I open our MVVM Features sample and put a breakpoint in the ToolsSampleMainView constructor, it only fires when the sample is first loaded.  I can move that tool window around and undock it and it's not created again.  The same View instance is reused.

UPDATE: The above was meant to say ToolItem1View constructor. After further testing, ToolItem1View does appear to be re-created in some scenarios (some moves to MDI and auto-hide popup display) and not others.

If you need us to look into a particular scenario in more detail, please provide exact instructions on how to reproduce it with that sample, including any code that is necessary.  Thanks!

[Modified 3 days ago]


Actipro Software Support

Posted 3 days ago by TYTYXI
Avatar

Yes, the ToolsSampleMainView will be created for once, but the ToolItem1View will be instanced for multi times when you drag it into another window. Due to our software has some C++ wrapper object must be used in a View, we do not want the View to be instanced for many times, this will cause the wrapper to be instanced, too.

We used to use a RecycleControl to cache the View created, and when the Template call the Build, it can return a cached View, not a new instance.

Example:

I did a demo about the feature i want, you can download the code in RecycleDockControlDemo , overwrite the files in MvvmFeatures.Views' files. When you drag ToolItemView into other windows, it will not create a new ToolItemVie's instance, the breakpoint in ToolItemView 's constructor will not be hit.

[Modified 3 days ago]

Answer - Posted 3 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

We had a typo in the previous reply and thank you for the additional information.  We were able to track things down.  Avalonia's ContentPresenter watches for logical tree changes and completely rebuilds the "view" when one is detected.  That is the root cause of what you're seeing. 

We had added some code in the Avalonia version of Docking/MDI (the problem doesn't exist in WPF) to work around this but the workaround wasn't effective in a couple scenarios.  We've revisited it and refactored the workaround code to something that should be effective in all scenarios.

We'd appreciate it if you could test out the updated logic.  It is up on nuget.org under the "25.2.2-beta.2" version that we published today.  Please let us know the test results.  Thanks!


Actipro Software Support

Posted 9 hours ago by TYTYXI
Avatar

Thanks, the beta version works, it does not recreate View for same ViewModel

Add Comment

Please log in to a validated account to post comments.