Ticket T1063013
Visible to All Users

SetBinding and mappings in InitializeBindings are ignore when use ViewModelCreate with DI Container

created 3 years ago

Hello there.

I'm creating a .NET Framework Windows Forms project with MVVM capabilities and Microsoft DI Container (Microsoft.Extensions.Hosting + Microsoft.Extensions.DependencyInjection, version 6.0.0). After to resolve a few errors for accomplish the integration, I found an behavior that I can't resolve: When I configure the MVVMContextCompositionRoot.ViewModelCreate event on Program.cs or using MVVMContext.ViewModelCreate inside of the Views, all bindings and mappings maded inside of InitializeBindings method are ignore by the MVVM Framework.

I attached a solution with 2 projects, MVVMDI.NotError project is a demo project using Microsoft DI but without configure the ViewModelCreate event. In this case, the MVVM Framework respect all binding and mappings. In MVVMDI.WithError, I only added the ViewModelCreate event in Program.cs (used for generate my POCO ViewModels with/without dependencies) and the corresponding application not working.

I seen these posts about similar problems, but I they was not enough for find a solution:

Winforms MVVM, Autofac, and ViewModel Management | DevExpress Support
Dependency injection works in Debug build, but not in Release build | DevExpress Support

Comments (3)
DevExpress Support Team 3 years ago

    Hello,

    Thank you for your sample. I reproduced this behavior. At present, I am working on the issue and need additional time. Once I get any results, I will share them with you.

    Should you have additional questions, let me know.

    L L
    Luis Gustavo Fernández Batres 3 years ago

      Hello Alexey Z,

      Are there some news about this issue? It's very important to me know if it's possible use DI and MVVM Framework, right now this problem it's a stopper in my current development.

      Thank you for working on my issue.

      DevExpress Support Team 3 years ago

        Hello,

        I understand how urgent this is for you. So, I personally contacted our developers in this regard. Once they have an approximate estimation for a fix or find a workaround, I will post a new comment in this thread.

        Feel free to contact me if you have additional questions.

        Answers approved by DevExpress Support

        created 3 years ago

        Hello,

        We researched your application and found the cause of the issue. Allow me to describe why this occurs. According to our documentation, if ViewModel follows the POCO concept, the MVVM Framework dynamically creates a new class that corresponds to this ViewModel and operates with it. The MVVM Framework does not work directly with the current ViewModel type:

        If a ViewModel follows the POCO concept, the MVVM Framework dynamically transforms this ViewModel to a new class containing the necessary infrastructure (to support, for instance, simplified data bindings). The Framework works with dynamically created class instances, which means you cannot initially access these instances at runtime because their types have not been determined.

        Since you register the MainViewModel type, the MVVM Framework works directly with this type. In this case, the MainViewModel type does not operate as POCO and, as a result, your binding settings are lost.

        To resolve the issue, you need to register a corresponding runtime ViewModel type as described here: ViewModel Management. In short, you need to use the e.RuntimeViewModelType property instead of e.ViewModelType in the ViewModelCreate event handler. A more detailed implementation depends on the Microsoft DI Container specifics.

        Also, it is important to declare variables as properties/fields at the level of the object from which a subscription is performed. In your case, you need to declare the IHost variable at the level of the Program class.

        Please try this solution and let me know your results.

          Show previous comments (1)
          DevExpress Support Team 3 years ago

            Hello

            Thank you for informing me that you have found a solution.

            Regarding your post, I am looking forward to it.

            L L
            Luis Gustavo Fernández Batres 3 years ago

              Hello Alexey Z.

              Finally, I finished write the post about the integration between WinForms, MS DI and the DevExpress MVVM Framework. You can read details follow this link:

              WinForms, Inyección de Dependencias y DevExpress MVVM – Hello Batres! (batressc.com)

              An apology:

              The post is in Spanish, there I explain the path what I followed until to find an acceptable implementation of all technologies. If is possible you could evaluate to add similar examples in the documentation for explain how to implement the integration of the MVVM Framework and MS DI.

              Thank you.

              DevExpress Support Team 3 years ago

                Hello,

                Thank you for sharing your experience with us. We greatly appreciate this. We will think about introducing a similar new article based on your topic or extending the current articles in our documentation.

                Feel free to contact us in case of any questions.

                Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.