【问题标题】:Opening a Page in a Frame from a separate Frame/Page从单独的框架/页面打开框架中的页面
【发布时间】:2019-09-17 04:28:33
【问题描述】:

我有一个带有 Stackpanel 按钮的 MainWindow。我点击一个按钮,一个 Frame(Frame1) 被一个 Page(page1) 填充。

Page1 有自己的带有按钮列表的堆栈面板。

我在 Frame1 的右侧也有 Frame2。 Frame1 和 Frame 2 都在 MainWindow 的 xaml 中。

我可以单击主窗口按钮并将页面加载到框架中。这工作正常 - 无论我加载 Frame1 还是 Frame2。

我需要做的是用 PAge1 加载 Frame1(这工作正常),然后当我在 Page1 中选择一个按钮时,我希望 Page2 加载到 Frame2 中。这就是我的问题所在。

我遇到的问题是,当我尝试在 frame2 中加载 page2 时出现空异常。

我的设计是否正确 - 为什么我看不到 page1 代码中对 frame2 的引用 - 我必须添加一个 Public Property CentralFrame As Frame 才能看到它,但我仍然得到一个空异常。

我已经搜索过,但找不到任何人设置它 - 我确定我错过了一些简单的东西,因为这对我来说似乎是一个标准的 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

请注意,如果我使用第二行代码,它将在第二帧中打开一个页面

在 Frame1 中加载的第 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 的按钮代码应该在 frame2 中打开 page2

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()

【问题讨论】:

    标签: wpf


    【解决方案1】:

    我必须承认我什至不知道视觉基础,我是一名 C# 程序员,但我想我已经解决了。

    我将 CentralFrame 的引用提供给 Page1 的“Tag”属性,并在需要时从 Page1 的“Tag”属性中获取 CentralFrame 的引用。

    主窗口

    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 的原因。

    注意:标签属性是一个“对象”。您可以根据需要设置此属性。

    【讨论】:

    • 感谢您的帮助 - 我现在可以继续前进了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多