首页 > 解决方案 > WPF 需要找到一种方法来识别自定义 Page 类上的 TypeOf

问题描述

我有一个名为 MainWindow 的 Window 类,并在其构造函数中构建了一个我调用的默认 Page 类 MonitorPage,它在启动时使用此页面填充我的窗口。在我的 MainWindow 中,我有三个按钮充当页面选项卡或菜单按钮,单击它们将创建不同 Page 类的实例,在我的例子中,我有三个独特的页面。监视器页、数据库页、帮助页。在我的 MainWindow 中,我想在相应页面打开时将选项卡按钮“灰显”。我在 MainWindow 中有一个名为 PageState() 的方法,该方法试图识别当前要启用或禁用哪个页面并将选项卡变灰。我的问题是在第一次 IF 检查时我的方法中出现 NullReferenceException。

我得到的错误:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Windows.Controls.ContentControl.Content.get returned null.

C#:

using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Media;

namespace EyeInTheSky
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml + backend code for FileSystemWatcher project
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Fields
        private FileSystemWatcher _watcher = new FileSystemWatcher();
        private ObservableCollection<string[]> _eventList = new ObservableCollection<string[]>();
        #endregion

        public MainWindow()
        {
            InitializeComponent();
            Main.Content = new MonitorPage(ref _watcher, ref _eventList);
            PageState();
        }

        private void PageState()
        {
            if (Main.Content.GetType() == typeof(MonitorPage))
            {
                Menu_MonitorButton.IsEnabled = false;
                Menu_MonitorButton.Background = new SolidColorBrush(Color.FromRgb(88, 88, 95));
                Menu_DataBaseButton.IsEnabled = true;
                Menu_HelpButton.IsEnabled = true;
            }
            else if (Main.GetType() == typeof(DataBasePage))
            {
                Menu_MonitorButton.IsEnabled = true;
                Menu_DataBaseButton.IsEnabled = false;
                Menu_DataBaseButton.Background = new SolidColorBrush(Color.FromRgb(88, 88, 95));
                Menu_HelpButton.IsEnabled = true;
            }
            else
            {
                Menu_MonitorButton.IsEnabled = true;
                Menu_DataBaseButton.IsEnabled = true;
                Menu_HelpButton.IsEnabled = false;
                Menu_HelpButton.Background = new SolidColorBrush(Color.FromRgb(88, 88, 95));
            }
        }

        private void Menu_MonitorButton_Click(object sender, RoutedEventArgs e)
        {
            PageState();
            Main.Content = new MonitorPage(ref _watcher, ref _eventList);
        }

        private void MenuStrip_DataBaseButton_Click(object sender, RoutedEventArgs e)
        {
            PageState();
            Main.Content = new DataBasePage(ref _eventList);
        }

        private void MenuStrip_HelpButton_Click(object sender, RoutedEventArgs e)
        {
            PageState();
            Main.Content = new HelpPage();
        }
    }
}

XAML:

<Window x:Name="Home" x:Class="EyeInTheSky.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:EyeInTheSky"
    xmlns:System="clr-namespace:System;assembly=mscorlib" 
    mc:Ignorable="d"
    Title="Eye In The Sky - Windows File System Watcher" Height="450" Width="1105" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" TextOptions.TextFormattingMode="Ideal" Background="#FF39393E" Foreground="#FFE4E4E4" FontFamily="Roboto">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="27*"/>
            <RowDefinition Height="394*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="240*"/>
            <ColumnDefinition Width="859*"/>
        </Grid.ColumnDefinitions>
        <Frame x:Name="Main" Height="421" VerticalAlignment="Top" Grid.RowSpan="2" Grid.ColumnSpan="2" Panel.ZIndex="1"/>
        <DockPanel HorizontalAlignment="Left" Height="27" LastChildFill="False" VerticalAlignment="Top" Width="240" Panel.ZIndex="10000">
            <StackPanel x:Name="Menu" Orientation="Horizontal" HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="240" Background="#FF4E4E53">
                <Button x:Name="Menu_MonitorButton" Content="Monitor" Width="80" Click="Menu_MonitorButton_Click" Background="#FF4E4E53" BorderBrush="#FF585858" Foreground="LightGray" BorderThickness="1,0"/>
                <Button x:Name="Menu_DataBaseButton" Content="Data Base" Width="80" Click="MenuStrip_DataBaseButton_Click" Background="#FF4E4E53" BorderBrush="#FF585858" Foreground="LightGray" BorderThickness="1,0"/>
                <Button x:Name="Menu_HelpButton" Content="About" Width="80" Click="MenuStrip_HelpButton_Click" Background="#FF4E4E53" BorderBrush="#FF585858" Foreground="LightGray" Padding="0,1,1,1" BorderThickness="1,0,2,0"/>
            </StackPanel>
        </DockPanel>
    </Grid>
</Window>

标签: c#wpfxamlnullreferenceexceptiontypeof

解决方案


推荐阅读