英文:
Wpf usercontrol base, cannot create an instance
问题
我添加了一个Usercontrol基类
我的基类
public class UserControlBase : UserControl
{
    protected IAppLogic app;
    public MainWindow CurrentWindow
    {
        get
        {
            return (App.Current.MainWindow as MainWindow);
        }
    }
    public UserControlBase()
    {
        var _app = IoC.Kernel.Get<IAppLogic>();
        this.app = _app;
    }
    public void MainNavigate(Pages.PageBase p)
    {
        CurrentWindow.MainFrame.Content = p;
    }
}
但是设计没有显示出来。
英文:
I add an Usercontrol base class
my Base Class
 public class UserControlBase : UserControl
    {
        protected IAppLogic app;
        public  MainWindow CurrentWindow{
            get{
                return (App.Current.MainWindow as MainWindow);
            }
        }
         
        public UserControlBase()
        {
            var _app = IoC.Kernel.Get<IAppLogic>();
            this.app = _app;
        }
        public void MainNavigate(Pages.PageBase p)
        {
            CurrentWindow.MainFrame.Content = p;
        }
    }
but the design does not shown
答案1
得分: 1
浏览了一些其他问题,我找到了一些可能发生这种情况的原因
问题1 WPF Designer “Could not create an instance of type”
- 在你的构造函数中将代码包裹在这个条件中:
 
if(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
   // 产生异常的代码         
}
- 基类是抽象类型
 - 在加载自定义控件时,在你的构造函数中抛出了异常。这回溯到第1点。
 
请分享堆栈跟踪以便我们提供更多帮助。
英文:
Browsing around some of the other questions, I found some of the reasons this can happen
Q1 WPF Designer “Could not create an instance of type”
- Suround the code in your constructor with this:
 
if(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
   //code producing exception         
}
- The base class is of the abstract type
 - An exception is thrown in your constructor while loading the custom control. This goes back to 1.
 
Please share the stacktrace for us to help more.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论