Opening a Page in a Frame from a separate Frame/Page

0

I have a MainWindow with a Stackpanel full of buttons. I click on a button and a Frame(Frame1) is populated with a Page(page1).

Page1 has its own stackpanel with a list of buttons.

I also have Frame2 to the right of Frame1. Both Frame1 and Frame 2 are in the xaml of the MainWindow.

I can click o the Mainwindow buttons and load the Pages into the Frames. This works fine - whether I load Frame1 or Frame2.

What I need to do is to load Frame1 with PAge1 (this works fine) and then when I select a button in Page1 I want Page2 to load in Frame2. This is where my problem is.

The issue I am getting is that it says there is a null exception when I try to load the page2 in frame2.

Am I designing this correctly - why can't I see the reference to frame2 from page1 code - I had to add a Public Property CentralFrame As Frame so I could see it but I still get a null exception.

I have searched but cannot find anyone setting this up - I am sure I am missing something simple as this to me seems like a standard UI setup.

MainWindow XAML here:

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

Code to open the leftframe on button click

Private Sub btnGeneralClick(sender As Object, e As RoutedEventArgs)
        LeftFrame.Content = New LGeneralPage()
        'CentralFrame.Content = New CGeneralPage()

    End Sub

Note that if I use the 2nd line of code it will open a page in the 2nd frame

XAML for PAge 1 that loads in Frame1

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

Code for the button click on page 1 that should open the page2 in frame2

Class LGeneralPage
    Public Property CentralFrame As Frame

    Private Sub btnQuickSetsPageClick(sender As Object, e As RoutedEventArgs)
        CentralFrame.Content = New CGeneralPage()
    End Sub


End Class

I had to add in the Public Property to be able to reference the frame2 (CentralFrame)

When I click on this button I get the following error

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
asked on Stack Overflow Sep 17, 2019 by Gargooglala

1 Answer

1

I must confess i don't even know visual-basic, i am a C# programmer but i guess i've solved.

I gave my CentralFrame's reference to "Tag" property of Page1 and i took CentralFrame's refence from "Tag" property of Page1 when i need.

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

Page 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

Page2

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 on my project (MainWindow is copy of yours)

enter image description here

What was your fault? You declared a variable as :

Public Property CentralFrame As Frame

But it is empty, it is not holding the reference of CentralFrame. I guess that is why you get NullReferenceException.

NOTE: Tag property is an "object". You can set this property as what ever you want.

answered on Stack Overflow Sep 17, 2019 by Bulutay SaraƧ • edited Sep 17, 2019 by Bulutay SaraƧ

User contributions licensed under CC BY-SA 3.0