我有一个MainWindow,其中的Stackpanel充满了按钮。我单击一个按钮,然后在Frame(Frame1)中填充Page(page1)。
Page1有自己的堆栈面板,其中包含按钮列表。
我在Frame1的右边也有Frame2。 Frame1和Frame 2都位于MainWindow的xaml中。
我可以单击o Mainwindow按钮,然后将页面加载到框架中。效果很好-无论我加载Frame1还是Frame2。
我需要做的是用PAge1加载Frame1(这很好),然后当我在Page1中选择一个按钮时,我希望Page2加载到Frame2中。这就是我的问题所在。
我遇到的问题是,当我尝试在frame2中加载page2时,它表示存在空异常。
我是否正确设计了这个-为什么我无法从page1代码中看到对frame2的引用-我必须添加一个Public Property CentralFrame As Frame以便可以看到它,但仍然会收到null异常。
我已经搜索过但找不到任何人进行设置-我确信我缺少一些简单的东西,因为这对我来说似乎是一个标准的UI设置。
MainWindow XAML在这里:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MPS_Documentation"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<DockPanel>
<TextBlock DockPanel.Dock="Top" Grid.Row="0" Text="Managed Print Services Documentation" FontWeight="Bold" FontFamily="HP Simplified" FontSize="20">
</TextBlock>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Content="General" Margin="5,0,0,0" Click="btnGeneralClick"/>
<Button Content="Copy/Print" Margin="5,0,0,0" Click="btnCopyPrintClick"/>
<Button Content="Scan/Digital Send" Margin="5,0,0,0"/>
<Button Content="Fax" Margin="5,0,0,0"/>
<Button Content="Supplies" Margin="5,0,0,0"/>
<Button Content="Toubleshooting" Margin="5,0,0,0"/>
<Button Content="Security" Margin="5,0,0,0"/>
<Button Content="HP Web Services" Margin="5,0,0,0"/>
<Button Content="Networking" Margin="5,0,0,0"/>
</StackPanel>
<Frame x:Name="LeftFrame" DockPanel.Dock="Left" NavigationUIVisibility="Hidden" Width="160"/>
<Frame x:Name="CentralFrame" NavigationUIVisibility="Hidden" />
</DockPanel>
</Grid>
</Window>
在单击按钮时打开左框架的代码
Private Sub btnGeneralClick(sender As Object, e As RoutedEventArgs)
LeftFrame.Content = New LGeneralPage()
'CentralFrame.Content = New CGeneralPage()
End Sub
请注意,如果我使用第二行代码,它将在第二帧中打开一个页面
在第1帧中加载的PAge 1的XAML
<Page x:Class="LGeneralPage"
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:MPS_Documentation"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="160"
Title="LGeneralPage">
<Grid>
<StackPanel Orientation="Vertical">
<Button Content="Quick Sets" Margin="5" Click="btnQuickSetsPageClick" />
<Button Content="Alerts" Margin="5"/>
<Button Content="Control Panel Settings App" Margin="5"/>
<Button Content="General Settings" Margin="5"/>
<Button Content="AutoSend" Margin="5"/>
<Button Content="Edit Other Links" Margin="5"/>
</StackPanel>
</Grid>
</Page>
在第1页上单击按钮的代码,应在第2帧中打开第2页
Class LGeneralPage
Public Property CentralFrame As Frame
Private Sub btnQuickSetsPageClick(sender As Object, e As RoutedEventArgs)
CentralFrame.Content = New CGeneralPage()
End Sub
End Class
我必须添加公共属性才能引用frame2(CentralFrame)
当我单击此按钮时,出现以下错误
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=MPS Documentation
StackTrace:
at MPS_Documentation.LGeneralPage.btnQuickSetsPageClick(Object sender, RoutedEventArgs e) in C:\Users\u492748\source\repos\MPS Documentation\MPS Documentation\LGeneralPage.xaml.vb:line 5
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.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
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.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at MPS_Documentation.Application.Main()
答案 0 :(得分:1)
我必须承认我什至不了解视觉基础,我是C#程序员,但我想我已经解决了。
我给CentralFrame引用了Page1的“标记”属性,并在需要时从Page1的“ Tag”属性中引用了CentralFrame。
MainWindow
Class MainWindow
Private Sub btnGeneralClick(sender As Object, e As RoutedEventArgs)
Dim Page1 As Page1 = New Page1()
Page1.Tag = CentralFrame
LeftFrame.Content = Page1
End Sub
End Class
第1页
XAML
<Page x:Class="Page1"
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:WpfApp21"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid Background="Pink">
<TextBlock Text="1" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="50"/>
<Button Name="btnPage1" Width="100" Height="25" Content="Click" />
</Grid>
</Page>
VB
Class Page1
Private Sub BtnPage1_Click(sender As Object, e As RoutedEventArgs) Handles btnPage1.Click
Dim CentralFrame As Frame = Me.Tag
CentralFrame.Content = New Page2()
End Sub
End Class
第2页
XAML
<Page x:Class="Page2"
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:WpfApp21"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page2">
<Grid Background="Aqua">
<TextBlock Text="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="50"/>
</Grid>
</Page>
我的项目上的GIF(MainWindow是您的副本)
您怎么了? 您将变量声明为:
Public Property CentralFrame As Frame
但是它为空,它没有保存CentralFrame的引用。我想这就是为什么您会得到NullReferenceException的原因。
注意:标记属性是一个“对象”。您可以根据需要设置此属性。