WPF需要找到一种在自定义Page类上标识TypeOf的方法

时间:2018-05-19 22:37:15

标签: c# wpf xaml nullreferenceexception typeof

我有一个名为MainWindow的Window类,并在其构造函数中构建一个默认的Page类I调用,MonitorPage在启动时使用此页面填充我的窗口。在我的MainWindow中,我有三个按钮作为页面标签或菜单按钮,单击它们将创建一个不同的Page类的实例,在我的例子中,我有三个独特的页面。 MonitorPage,DataBasePage,HelpPage。在我的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>

0 个答案:

没有答案