添加用户控件的WPF错误

时间:2010-11-10 14:07:22

标签: .net wpf visual-studio

当我在此行中包含我自己的usercontrol时,会发生错误(拖放):

<my:ItemParte x:Name="UcItemParte" Panel.ZIndex="6" />

并且错误说:“对象引用未设置为对象的实例。”

我不知道为什么......有任何帮助吗?

更新: 这是Sub New()

共享子新()         ListViewFontSizeProperty = DependencyProperty.Register(“ListViewFontSize”,_             GetType(Double),GetType(ItemParte))     结束子

Public Sub New()
    Try
        mModeloItem = Modelo_Item.Instance
        mModeloParte = Modelo_Parte.Instance
        mModeloParteLinea = Modelo_Parte_Linea.Instance

        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        If Not IsInDesignMode Then
            mTextos = Textos.Instance
            mConfig = Config.Instance

            MainWin = My.Application.MainWindow

            AddHandler MainWin.VentanaMensaje.ButtonAceptarExec, AddressOf VentanaMensaje_AceptarClickCallback

            AddHandler MainWin.Fuentes.PropertyChanged, AddressOf Fuentes_PropertyChanged

            MainWin.Fuentes.RecargarFontSize()

        End If

    Catch ex As Exception
        My.Log.WriteEntry(ex.Message, TraceEventType.Critical)
    End Try
End Sub

2 个答案:

答案 0 :(得分:0)

错误可能是您在用户控件的Load事件中发生了某些事情 - 即使加载表单以供设计人员使用时,此事件也会触发,而不仅仅是在运行时。

如果是这种情况,请在Load方法中添加

if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode)
{
  // ...your code
}

解释here

答案 1 :(得分:0)

相关问题