所以我试图用一个并排的用户控件创建一个应用程序,当你在它们之间切换时,它会左右滑动。问题是,我的一个用户控件(AddReferencePage),当添加到MainWindow时,导致我的MainWindow上的开始标记变为带有NullReferenceException错误的下划线。设计师还显示错误:
NullReferenceException: Object reference not set to an instance of an object.
StackTrace:
at ARC.AddReferencePage.ReferenceDetails()
at ARC.AddReferencePage.AddReferencePage_Loaded(Object sender, RoutedEventArgs e)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
at MS.Internal.LoadedOrUnloadedOperation.DoWork()
at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler
显示我的用户控件的代码是:
<DockPanel x:Name="SlideGrid" VerticalAlignment="Stretch" Width="{Binding ActualWidth, ElementName=MainGrid, Converter={StaticResource DoubleConverter}}" Margin="0,28,0,0" ClipToBounds="True">
<ItemsControl DockPanel.Dock="Left">
<local:MainPage/>
</ItemsControl>
<ItemsControl DockPanel.Dock="Right">
<local:AddReferencePage/>
</ItemsControl>
</DockPanel>
MainPage工作正常,没有任何错误。它是导致错误的AddReferencePage。我已经检查了每个用户控件各自的代码隐藏,并且它们的目的和目的完全相同。
MainPage的代码隐藏:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Collections.Specialized
Imports System.Windows.Threading
Imports System.Windows.Media.Animation
Imports System.Globalization
Public Class MainPage
Private StartPoint As Point
Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
'Dim PW As MainWindow = Window.GetWindow(MainPage)
'If PW IsNot Nothing Then
' ReferenceList.ItemsSource = PW.ReferenceCollection
' Dim Alphabetical As SortDescription = New SortDescription("PropLastName", ListSortDirection.Ascending)
' ReferenceList.Items.SortDescriptions.Add(Alphabetical)
'End If
RefreshReferences()
End Sub
Public Sub RefreshReferences()
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
ReferenceList.ItemsSource = PW.ReferenceCollection
Dim Alphabetical As SortDescription = New SortDescription("PropLastName", ListSortDirection.Ascending)
ReferenceList.Items.SortDescriptions.Add(Alphabetical)
End If
End Sub
Private Sub ListBoxSearch_TextChanged(sender As Object, e As TextChangedEventArgs) Handles ListBoxSearch.TextChanged
If ListBoxSearch.Text <> "Search..." Then
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
PW.view = CollectionViewSource.GetDefaultView(PW.ReferenceCollection)
PW.view.Filter = New Predicate(Of Object)(AddressOf PW.FilterList)
End If
End If
End Sub
Private Sub ListBoxSearch_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles ListBoxSearch.PreviewMouseDown
If ListBoxSearch.Text = "Search..." Then
ListBoxSearch.FontStyle = FontStyles.Normal
ListBoxSearch.Clear()
End If
End Sub
Private Sub ListBoxSearch_LostFocus(sender As Object, e As RoutedEventArgs) Handles ListBoxSearch.LostFocus
If ListBoxSearch.Text = "" Then
ListBoxSearch.FontStyle = FontStyles.Italic
ListBoxSearch.Text = "Search..."
End If
End Sub
Private Sub ListViewClick(sender As Object, e As RoutedEventArgs) Handles ButtonListView.Click
If ReferenceList.ItemTemplate Is FindResource("GridViewTemplate") Then
ViewBackground.BeginStoryboard(DirectCast(Me.Resources("ViewRightToLeft"), Storyboard))
ReferenceList.ItemTemplate = DirectCast(Me.FindResource("ListViewTemplate"), DataTemplate)
ButtonListView.Content = FindResource("NoListView")
ButtonGridView.Content = FindResource("NoGridView")
End If
End Sub
Private Sub GridViewClick(sender As Object, e As RoutedEventArgs) Handles ButtonGridView.Click
If ReferenceList.ItemTemplate Is FindResource("ListViewTemplate") Then
ViewBackground.BeginStoryboard(DirectCast(Me.Resources("ViewLeftToRight"), Storyboard))
ReferenceList.ItemTemplate = DirectCast(Me.FindResource("GridViewTemplate"), DataTemplate)
ButtonListView.Content = FindResource("NoListView")
ButtonGridView.Content = FindResource("NoGridView")
End If
End Sub
Private Sub ListViewEnter(sender As Object, e As RoutedEventArgs) Handles ButtonListView.MouseEnter
End Sub
Private Sub GridViewEnter(sender As Object, e As RoutedEventArgs) Handles ButtonGridView.MouseEnter
End Sub
Private Sub ListViewLeave(sender As Object, e As RoutedEventArgs) Handles ButtonListView.MouseLeave
End Sub
Private Sub GridViewLeave(sender As Object, e As RoutedEventArgs) Handles ButtonGridView.MouseLeave
End Sub
Private Sub EditListBoxItem(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
PW.ChangeSlide(sender, 1)
PW.EditItem = ReferenceList.SelectedItem
PW.NewItem = False
End If
End Sub
Private Sub DeleteListBoxItem(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
PW.ReferenceCollection.Remove(ReferenceList.SelectedItem)
End If
End Sub
Private Sub NewListBoxItem(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
PW.ChangeSlide(sender, 1)
PW.NewItem = True
End If
End Sub
Private Sub ReferenceList_Loaded(sender As Object, e As RoutedEventArgs) Handles ReferenceList.Loaded
RefreshReferences()
End Sub
Private Sub ReferenceList_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles ReferenceList.PreviewMouseLeftButtonDown
Dim PW As MainWindow = Window.GetWindow(MainPage)
StartPoint = e.GetPosition(Nothing)
PW.Resizing = False
End Sub
Private Sub ReferenceList_PreviewMouseMove(sender As Object, e As MouseEventArgs) Handles ReferenceList.PreviewMouseMove
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW.Resizing = False Then
Dim MousePosition As Point = e.GetPosition(Nothing)
Dim Difference As Vector = StartPoint - MousePosition
Dim StopDrop As Boolean
If e.LeftButton = MouseButtonState.Pressed AndAlso (Math.Abs(Difference.X) > SystemParameters.MinimumHorizontalDragDistance Or Math.Abs(Difference.Y) > SystemParameters.MinimumVerticalDragDistance) Then
Dim LB As ListBox = sender
Dim UIE As UIElement = LB.InputHitTest(MousePosition)
If UIE IsNot Nothing Then
Dim Data As Object = DependencyProperty.UnsetValue
While Data Is DependencyProperty.UnsetValue And UIE IsNot Nothing
Data = LB.ItemContainerGenerator.ItemFromContainer(UIE)
If Data Is DependencyProperty.UnsetValue Then
UIE = VisualTreeHelper.GetParent(UIE)
End If
If UIE Is LB Then
StopDrop = True
End If
End While
If Data IsNot DependencyProperty.UnsetValue Then
StopDrop = False
End If
Else
StopDrop = True
End If
PW.TempItem = LB.SelectedItem
Dim FN As String = PW.TempItem.PropLastName & ", " & PW.TempItem.PropFirstName.Substring(0, 1)
Dim TT As String = PW.TempItem.PropTitle
Dim YR As String = PW.TempItem.PropYear.ToString
Dim ReferenceText As String = FN & " " & YR & ", " & TT
Dim DragData As DataObject = New DataObject(DataFormats.StringFormat, ReferenceText)
If DragData IsNot Nothing And StopDrop = False Then
DragDrop.DoDragDrop(sender, DragData, DragDropEffects.Copy)
End If
End If
End If
End Sub
End Class
MainPage的XAML:
<UserControl x:Class="MainPage" x:Name="MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ARC"
mc:Ignorable="d"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" d:DesignWidth="400" d:DesignHeight="600" SnapsToDevicePixels="True">
<UserControl.Resources>
<local:ImageConverter x:Key="ImageConverter"/>
<Image x:Key="BrownBook" Source="Images\Icons\BrownBook.png" Height="64" Width="64"/>
<Storyboard x:Key="ViewRightToLeft"
AccelerationRatio=".5"
DecelerationRatio=".5">
<ThicknessAnimation Storyboard.TargetName="ViewBackground" Storyboard.TargetProperty="Margin" Duration="0:0:0.15" To="0,0,28,0"/>
</Storyboard>
<Storyboard x:Key="ViewLeftToRight"
AccelerationRatio=".5"
DecelerationRatio=".5">
<ThicknessAnimation Storyboard.TargetName="ViewBackground" Storyboard.TargetProperty="Margin" Duration="0:0:0.15" To="0,0,-28,0"/>
</Storyboard>
</UserControl.Resources>
<Grid Background="#FFF1F1F1">
<Grid>
<DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<DockPanel Margin="0,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="28" DockPanel.Dock="Top" Background="#E5E5E5">
<Button BorderThickness="0" x:Name="AddNewReference" Style="{DynamicResource NoChromeButton}" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="ListViewClick" Height="28" Width="28">
<DynamicResource ResourceKey="SearchBar"/>
</Button>
<Button BorderThickness="0" x:Name="Settings" Style="{DynamicResource NoChromeButton}" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="ListViewClick" Height="28" Width="28">
<DynamicResource ResourceKey="SearchBar"/>
</Button>
<Grid Margin="0,0,0,0" DockPanel.Dock="Right" Width="56" Height="28" VerticalAlignment="Top" HorizontalAlignment="Stretch">
<Button Height="28" Width="28" Margin="0,0,28,0" BorderThickness="0" Style="{DynamicResource NoChromeButton}" x:Name="ViewBackground" Background="#3E3B3B"/>
<Button Margin="0,0,0,0" BorderThickness="0" x:Name="ButtonGridView" Style="{DynamicResource NoChromeButton}" HorizontalAlignment="Right" Click="GridViewClick" Height="28" Width="28">
<DynamicResource ResourceKey="NoGridView"/>
</Button>
<Button Margin="0,0,28,0" BorderThickness="0" x:Name="ButtonListView" Style="{DynamicResource NoChromeButton}" HorizontalAlignment="Right" Click="ListViewClick" Height="28" Width="28">
<DynamicResource ResourceKey="NoListView"/>
</Button>
</Grid>
<Button BorderThickness="0" x:Name="SearchBarIcon" Style="{DynamicResource NoChromeButton}" DockPanel.Dock="Left" HorizontalAlignment="Right" Click="ListViewClick" Height="28" Width="23">
<DynamicResource ResourceKey="SearchBar"/>
</Button>
<TextBox x:Name="ListBoxSearch" Style="{DynamicResource SearchBoxTemplate}" Text="Search..." FontSize="16" HorizontalAlignment="Stretch" FontStyle="Italic" BorderThickness="0" Foreground="Gray" Background="Transparent" VerticalContentAlignment="Top"/>
</DockPanel>
<ListBox AlternationCount="2" DockPanel.Dock="Top" Margin="0,0,0,0" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ReferenceCollection, ElementName=local:MainWindow}" TextSearch.TextPath="SearchString" x:Name="ReferenceList" HorizontalContentAlignment="Stretch" ScrollViewer.CanContentScroll="False" SnapsToDevicePixels="True" AllowDrop="True" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBox.Background>
<LinearGradientBrush StartPoint="1,1" EndPoint="1,0">
<GradientStop Offset="0.0" Color="#190000FF"/>
<GradientStop Color="#000000FF" Offset="1.0"/>
</LinearGradientBrush>
</ListBox.Background>
<ListBox.ItemTemplate>
<StaticResource ResourceKey="ListViewTemplate"/>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu Style="{StaticResource ListBoxContextMenu}">
<MenuItem x:Name="EditContextMenuItem" Header="Edit" Click="EditListBoxItem" Style="{StaticResource ListBoxContextMenuItem}"/>
<MenuItem x:Name="DeleteContextMenuItem" Header="Delete" Click="DeleteListBoxItem" Style="{StaticResource ListBoxContextMenuItem}"/>
<MenuItem x:Name="NewContextMenuItem" Header="New" Click="NewListBoxItem" Style="{StaticResource ListBoxContextMenuItem}"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</DockPanel>
</Grid>
</Grid>
</UserControl>
AddReferencePage的代码隐藏:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Collections.Specialized
Imports System.Windows.Threading
Imports System.Windows.Media.Animation
Imports System.Globalization
Public Class AddReferencePage
Private SelectImage As Boolean = False
Private Sub ReturnToReferences(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
If PW IsNot Nothing Then
If ReferenceAuthor.Text <> "Author" Or ReferenceDate.Text <> "Date" Or ReferenceTitle.Text <> "Title" Then
Dim CN As String() = Nothing
Dim FN As String = Nothing
Dim LN As String = Nothing
Dim YR As String = Nothing
Dim TT As String = Nothing
If ReferenceAuthor.Text = "Author" Then
ReferenceAuthor.Text = "Unknown"
FN = Nothing
LN = "Unknown"
Else
If ReferenceAuthor.Text.Contains(" ") Then
CN = ReferenceAuthor.Text.Split(" ")
FN = CN.First
LN = CN.Last
Else
FN = Nothing
LN = ReferenceAuthor.Text
End If
End If
If ReferenceDate.Text = "Date" Then
ReferenceDate.Text = "Unknown"
YR = "Unknown"
Else
YR = ReferenceDate.Text
End If
If ReferenceTitle.Text = "Title" Then
ReferenceTitle.Text = "Unknown"
TT = "Unknown"
Else
TT = ReferenceTitle.Text
End If
If PW.NewItem = False Then
PW.ReferenceCollection.Remove(PW.EditItem)
PW.AddReferenceItem(FN, LN, TT, YR, "Book")
Else
PW.AddReferenceItem(FN, LN, TT, YR, "Book")
End If
End If
End If
PW.ChangeSlide(sender, 0)
End Sub
Private Sub ButtonsMouseEnter(sender As Button, e As RoutedEventArgs)
Select Case sender.Name
Case "ReturnReferences"
sender.Content = FindResource("BackArrowHover")
End Select
End Sub
Private Sub ButtonsMouseLeave(sender As Button, e As RoutedEventArgs)
Select Case sender.Name
Case "ReturnReferences"
sender.Content = FindResource("BackArrowNormal")
End Select
End Sub
Private Sub ButtonsMouseDown(sender As Button, e As RoutedEventArgs)
Select Case sender.Name
Case "ReturnReferences"
sender.Content = FindResource("BackArrowClick")
End Select
End Sub
Public Sub SaveReferenceSub(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
Dim CN As String() = ReferenceAuthor.Text.Split(" ")
Dim FN As String = CN.First()
Dim LN As String = CN.Last()
Dim YR As Integer = CInt(ReferenceDate.Text)
Dim TT As String = ReferenceTitle.Text
If PW.NewItem = False Then
PW.ReferenceCollection.Remove(PW.EditItem)
PW.AddReferenceItem(FN, LN, TT, YR, "Book")
Else
PW.AddReferenceItem(FN, LN, TT, YR, "Book")
End If
End Sub
Private Sub ReferenceDetails()
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
If PW.NewItem = False Then
ReferenceAuthor.Text = PW.EditItem.PropFirstName & " " & PW.EditItem.PropLastName
ReferenceTitle.Text = PW.EditItem.PropTitle
ReferenceDate.Text = PW.EditItem.PropYear.ToString
End If
End Sub
Private Sub AddReferencePage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
ReferenceDetails()
End Sub
Private Sub FieldEnter(sender As TextBox, e As RoutedEventArgs)
Select Case sender.Name
Case "ReferenceAuthor"
If sender.Text = "Author" Then
sender.Clear()
End If
Case "ReferenceTitle"
If sender.Text = "Title" Then
sender.Clear()
End If
Case "ReferenceDate"
If sender.Text = "Date" Then
sender.Clear()
End If
End Select
End Sub
Private Sub FieldLeave(sender As TextBox, e As RoutedEventArgs)
If sender.Text = "" Then
Select Case sender.Name
Case "ReferenceAuthor"
sender.Text = "Author"
Case "ReferenceTitle"
sender.Text = "Title"
Case "ReferenceDate"
sender.Text = "Date"
End Select
End If
End Sub
Private Sub FieldChanged(sender As TextBox, e As RoutedEventArgs)
If sender.Text <> "Author" And sender.Text <> "Date" And sender.Text <> "Title" Then
sender.Foreground = Brushes.Black
Else
sender.Foreground = Brushes.Gray
End If
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
If SelectImage = False Then
SelectImageBox.BeginStoryboard(DirectCast(Me.Resources("LeftToRight"), Storyboard))
SelectImage = True
Else
SelectImageBox.BeginStoryboard(DirectCast(Me.Resources("RightToLeft"), Storyboard))
SelectImage = False
End If
End Sub
Private Sub SelectImageBox_LostFocus(sender As Object, e As RoutedEventArgs) Handles SelectImageBox.LostFocus
'If SelectImage = True Then
' SelectImageBox.BeginStoryboard(DirectCast(Me.Resources("RightToLeft"), Storyboard))
' SelectImage = False
'End If
End Sub
End Class
AddReferencePage的XAML:
<UserControl x:Class="AddReferencePage" x:Name="AddReferencePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ARC"
mc:Ignorable="d"
VerticalAlignment="Stretch" SnapsToDevicePixels="True" HorizontalAlignment="Stretch" d:DesignWidth="400" d:DesignHeight="600">
<UserControl.Resources>
<Storyboard x:Key="RightToLeft"
AccelerationRatio=".5"
DecelerationRatio=".5">
<DoubleAnimation Storyboard.TargetName="ReferenceInfo" Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:0.3" To="0"/>
<DoubleAnimation Storyboard.TargetName="SelectImageBox" Storyboard.TargetProperty="Opacity" Duration="0:0:0.3" From="1.0" To="0.0"/>
</Storyboard>
<Storyboard x:Key="LeftToRight"
AccelerationRatio=".5"
DecelerationRatio=".5">
<DoubleAnimation Storyboard.TargetName="ReferenceInfo" Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:0.3" To="{Binding ActualWidth, ElementName=AnimInfo}"/>
<DoubleAnimation Storyboard.TargetName="SelectImageBox" Storyboard.TargetProperty="Opacity" Duration="0:0:0.3" From="0.0" To="1.0"/>
</Storyboard>
</UserControl.Resources>
<Grid Background="#FFF1F1F1">
<Button Style="{DynamicResource NoChromeButton}" x:Name="ReturnReferences" Click="ReturnToReferences" MouseDown="ButtonsMouseDown" MouseEnter="ButtonsMouseEnter" MouseLeave="ButtonsMouseLeave" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5,5,0,0" Height="48" Width="48">
<DynamicResource ResourceKey="BackArrowNormal"/>
</Button>
<TextBlock Text="Edit References" FontFamily="Segoe UI Light" FontSize="36" Margin="72,3,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<TextBlock x:Name="AnimInfo" FontFamily="Segoe UI Light" FontSize="36" Margin="72,3,60,0" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<ListView x:Name="SelectImageBox" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="72,59,60,20" Background="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderThickness="0" Opacity="0">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
</ListView>
<Canvas x:Name="InfoCanvas" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="72,60,0,0" Opacity="1">
<DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="ReferenceInfo" Canvas.Left="0" Canvas.Top="0" Width="{Binding ActualWidth, ElementName=InfoCanvas}" Height="{Binding ActualHeight, ElementName=InfoCanvas}">
<TextBox x:Name="ReferenceAuthor" GotFocus="FieldEnter" TextChanged="FieldChanged" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Author" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
<TextBox x:Name="ReferenceTitle" GotFocus="FieldEnter" TextChanged="FieldChanged" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Title" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
<TextBox x:Name="ReferenceDate" GotFocus="FieldEnter" TextChanged="FieldChanged" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Date" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
</DockPanel>
</Canvas>
<Button Style="{DynamicResource NoChromeButton}" Margin="5,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="48" Height="48" VerticalContentAlignment="Center" Click="Button_Click_1">
<DynamicResource ResourceKey="Book"/>
</Button>
</Grid>
</UserControl>
我知道很多代码:D但我真的需要解决这个问题!谢谢大家!如果您需要更多详细信息,请告诉我们。我的所有资源和模板都在Application.xaml中定义,由于不相关,我没有包含它。如果需要,我可以加入。
答案 0 :(得分:1)
根据错误堆栈跟踪,错误发生在子ReferenceDetails()
中,这是问题中倒数第二个代码块。我认为这些代码行中存在错误,
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
If PW.NewItem = False Then
我没有使用WPF,只有Winforms。但如果设计师相似,那么MainWindow
在设计时可能Nothing
。您可以尝试这样的事情,看看它是否修复了错误。
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
If PW IsNot Nothing AndAlso PW.NewItem = False Then
如果PW以Nothing
的形式返回,则不会导致NullReferenceException
。另外(我再次假设WPF设计人员的工作方式类似于Winfors),请确保在更改代码后重新构建,然后再尝试将其添加到主窗口。
答案 1 :(得分:0)
感谢Kratz如此迅速地回应。经过一段时间的游戏,我发现我需要将用户控件放在一个Frame中。 :d