WPF .NET 4.6' No NameScrope'错误 - 但在以前的版本中工作

时间:2015-08-05 23:20:37

标签: .net wpf vb.net

我刚刚使用VS 2015在.NET 4.6中启动了一个应用程序并且遇到了意想不到的问题。以下是一个弹出窗口,注册控件失败,并且没有找到NameScope来注册名称' PU_Assoc_DGV'。"

解释

Private MainPopUp As Window
Private MainGrid As Grid
Private MainPopupStatusBar As DockPanel
Private MainPopupStatusBarLabel As Label
Private MainPopupstatusBarText As TextBlock

调用Sub来打开弹出窗口

Public Sub PopupModals_Associations()
    Try
        AssociationID = 0
        MainPopUp = New Window
        MainGrid = New Grid
        MainPopUp.Content = MainGrid
        ' AddHandler MainPopUp.Loaded, AddressOf Assoc_Loaded
        With MainPopUp
            .Width = 600
            .Height = 300
            .ResizeMode = ResizeMode.NoResize
            .Icon = BitmapFrame.Create(ReturnIconImage("Assoc_ICO.ico"))
        End With



        For i As Integer = 0 To 2
            Dim vRow As New RowDefinition
            If i = 1 Then
                vRow.Height = New GridLength(135, GridUnitType.Star)
            Else
                vRow.Height = New GridLength(35)
            End If
            MainGrid.RowDefinitions.Add(vRow)
        Next



        Dim vToolBar As New ToolBar

        Dim InsertButton As New Button
        With InsertButton
            .Content = ReturnToolBarImage("Insert.png")
            .ToolTip = "Insert the association"
        End With
        ' RegisterControl(MainGrid, InsertButton)
        vToolBar.Items.Add(InsertButton)
        vToolBar.Items.Add(TS_Separator)

        Dim SearchBox As New WatermarkTextBox
        With SearchBox
            .Watermark = "Search Phrase"
            .ToolTip = "Search Phrase"
            .Name = "PU_Assoc__SearchBox"
            .Width = 300
            .Height = 25
            .HorizontalAlignment = Windows.HorizontalAlignment.Center
        End With
        '  RegisterControl(MainGrid, SearchBox)
        vToolBar.Items.Add(SearchBox)

        Dim SearchButton As New Button
        With SearchButton
            .Content = ReturnToolBarImage("Search.png")
            .ToolTip = "Search for Association"
            .Name = "PU_Assoc__SearchButton"
            .IsDefault = True
        End With
        '  RegisterControl(MainGrid, SearchButton)
        vToolBar.Items.Add(SearchButton)
        vToolBar.Items.Add(TS_Separator)

        Grid.SetRow(vToolBar, 0)
        MainGrid.Children.Add(vToolBar)

        Dim DGV As New CustomControl.DGVx
        With DGV
            .Name = "PU_Assoc_DGV"
        End With
        RegisterControl(MainGrid, DGV)
        Grid.SetRow(DGV, 1)
        MainGrid.Children.Add(DGV)


        Dim vStatusBar As DockPanel = PopupStatusBar()
        Grid.SetRow(vStatusBar, 2)
        MainGrid.Children.Add(vStatusBar)

        MainPopUp.WindowStartupLocation = WindowStartupLocation.CenterOwner
        Dim CurApp As Application = Application.Current
        Dim vWindow As Window = CurApp.MainWindow
        MainPopUp.Owner = vWindow
        MainPopUp.ShowDialog()

    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

注册控件的功能

Public Sub RegisterControl(HoldingControl As Grid, ControlType As Control)
    Dim vO As Object = HoldingControl.FindName(ControlType.Name.ToString)
    If vO Is Nothing Then
        HoldingControl.RegisterName(ControlType.Name.ToString, ControlType)
    End If
End Sub

有什么想法吗?

谢谢

1 个答案:

答案 0 :(得分:-1)

似乎答案是设置NameScope - 将其添加到sub后它按预期工作 - 不确定4.6中的更改但是在之前的版本中它没有用 - 叹息

并不是说我担心收视率,但如果你不能回答这个问题,那么就不要再去了!

 NameScope.SetNameScope(MainGrid, New NameScope())