WPF Designer “Could not create an instance of type”

后端 未结 11 1168
悲&欢浪女
悲&欢浪女 2020-12-20 11:45

In my UI XAML I\'m essentially inheriting from a class \"BaseView\" that contains functionality common to several forms, however this is preventing the designer from display

相关标签:
11条回答
  • 2020-12-20 12:01

    Another possible cause, as we just found here, so I'm adding this answer for future users, is if the project is hosted on an untrusted source, such as a file server.

    In that case, the designer wouldn't load the assembly and so gave the same "Could not create instance..." error. The solution would still build and debug OK.

    0 讨论(0)
  • 2020-12-20 12:04

    I don't know what the problem is, but I solved it like this:

    You should not execute the code in the constructor of your BaseClass but from the constructor of the parent class.

    You create a method in the BaseClass that you execute from the constructor of the parent class.

    0 讨论(0)
  • 2020-12-20 12:07

    In WinForms, it's possible to use the designer with abstract controls if you use a custom TypeDescriptionProvider to inform the designer of a concrete implementation:

    I'm using the solution in this answer to another question, which links this article. The article recommends using a custom TypeDescriptionProvider and concrete implementation of the abstract class. The designer will ask the custom provider which types to use, and your code can return the concrete class so that the designer is happy while you have complete control over how the abstract class appears as a concrete class.

    0 讨论(0)
  • 2020-12-20 12:08

    I have the problem that my MVVM class have acces to the database in the constructor and that was the problem, it throws an exception. I only have to check if application is runing in Design mode.

    0 讨论(0)
  • 2020-12-20 12:10

    Yet another possible cause.

    I have a user control which has child controls which generate events e.g. selection_changed on list control. The select_changed event handler makes changes to other child controls.

    During initialisation the selected item property of the list box gets changed and triggers a selection_changed event. The handler tries to update the other child controls but cannot because they have not yet been instantiated. This leads to a null pointer exception and causes the problem.

    Once the null pointer problem was handled the control was able to be instantiated and appeared in the parent control.

    0 讨论(0)
  • 2020-12-20 12:10

    The reason why I was getting this error was simple but tough for me to track down. My converter class was not public. Simply changing the class's accessibility fixed it.

        public class StringToLowerConverter : IValueConverter
    
    0 讨论(0)
提交回复
热议问题