0

在不为空的属性上获取 NullValueException

这是 ViewModel 开头的代码,以及创建 ViewModel 对象并打开我正在使用 ViewModelt 的窗口的方法。在 SwitchName 属性上引发了异常。_ciscoswitch.SwitchName 出现为空,因为 SwitchVewModel 中的 _ciscoswitch 为空。在 SwitchBrowser 构造函数中的 InitializeComponent() 处引发异常。查看调试器中的 SwitchVewModel 实例,_ciscoswitch 不为空。我尝试将 SwitchName 访问器更改为使用 CiscoSwitch.switchName 而不是 _ciscoswitch.SwitchName,但仍然失败。

class SwitchViewModel : INotifyPropertyChanged
{
      #region Construction
    /// Constructs the default instance of a SwitchViewModel
    public SwitchViewModel()
    {

    }


    public SwitchViewModel(CiscoSwitch cs)
    {
       _ciscoswitch = cs;
    }
      #endregion
    #region Members

    CiscoSwitch _ciscoswitch;
    #endregion

    #region Properties
    public CiscoSwitch CiscoSwitch
    {
        get
        {
            return _ciscoswitch;
        }
        set
        {
            _ciscoswitch = value;
        }
    }

    public string SwitchName
    {
        get { return _ciscoswitch.switchName; }
        set
        {
            if (_ciscoswitch.switchName != value)
            {
               _ciscoswitch.switchName = value;
                RaisePropertyChanged("switchName");
            }
        }
    }
    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Methods

    private void RaisePropertyChanged(string propertyName)
    {
        // take a copy to prevent thread issues
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion

}
}

SwitchBrowserWindow 的 XAML 我现在使用的唯一属性是 SwitchName 来尝试使其正常工作

<Window x:Class="CiscoDecodeWPF.SwitchBrowser"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:CiscoDecodeWPF="clr-namespace:CiscoDecodeWPF" IsEnabled="{Binding Enabled}"
    Title="SwitchBrowser" Height="500" Width="500" Background="GhostWhite">

<Window.Resources>
    <Style TargetType="{x:Type TreeViewItem}" x:Key="ModuleStyle">
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>

    <Style TargetType="{x:Type TreeViewItem}" x:Key="RedModuleStyle" BasedOn="{StaticResource ModuleStyle}">
        <Setter Property="Foreground" Value="Red"/>
    </Style>
</Window.Resources>

<Window.DataContext>
    <CiscoDecodeWPF:SwitchViewModel/>
</Window.DataContext>
<Grid Margin="0,0,-211.4,-168">
    <StackPanel HorizontalAlignment="Stretch" Name="StackPanel1" VerticalAlignment="Stretch" Width="Auto" Margin="0,0,188.6,114">

        <StackPanel.Resources> 
            <Style TargetType="{x:Type Label}" x:Key="LabelStyle">
                <Setter Property="Foreground" Value="Blue"/>
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </Style>
        </StackPanel.Resources>
        <Label Content="Switch Name:" Name="Label1" Height="25" HorizontalAlignment="Left"/>
        <Label Content="Software Version:" Name="Label2" HorizontalAlignment="Left" />
        <Label Content="Model Number:" Name="Label3" HorizontalAlignment="left"/>
        <Label Content="IP Address:" Name="Label4" HorizontalAlignment="left"></Label>
        <Label Content="Serial Number:" Name="Label5" HorizontalAlignment="Left"></Label>
        <Label Content="Show Tech Taken:" Name="Label6" HorizontalAlignment="left"/>
    </StackPanel>
    <StackPanel Margin="105,0,218,489">
        <StackPanel.Resources>
            <Style TargetType="{x:Type Label}" x:Key="LabelStyle">
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </Style>
        </StackPanel.Resources>
        <Label Content="{Binding Path=SwitchName}" Name="SwitchNameLabel" HorizontalAlignment="left"/>
        <Label Content="Version" Name="VersionLabel" HorizontalAlignment="left"/>
        <Label Content="Model" Name="ModelNumberLabel" HorizontalAlignment="Left"/>
        <Label Content="IP" Name="IPAddressLabel" HorizontalAlignment="Left"/>
        <Label Content="Serial" Name="SerialLabel" HorizontalAlignment="Left"/>
            <Label Content="ST" Name="ShowTechLabel" HorizontalAlignment="Left"/>

    </StackPanel>

异常、堆栈跟踪和调用堆栈

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
 Message=Object reference not set to an instance of an object.
  Source=CiscoDecodeWPF
StackTrace:
   at CiscoDecodeWPF.SwitchViewModel.get_SwitchName() in 

d:\Projects\CiscoDecodeWPF\CiscoDecodeWPF\SwitchViewModel.cs:line 50

内部异常:

CiscoDecodeWPF.exe!CiscoDecodeWPF.SwitchViewModel.SwitchName.get() 第 50 行 + 0xf 字节 C# [外部代码] CiscoDecodeWPF.exe!CiscoDecodeWPF.SwitchBrowser.SwitchBrowser(CiscoDecodeWPF.CiscoSwitch cs) 第 35 行 + 0x8 字节 C# CiscoDecodeWPF.exe!CiscoDecodeWPF。 MainWindow.BrowseSwitchMenuItem_Click(object sender, System.Windows.RoutedEventArgs e) Line 1050 + 0x34 bytes C# [外部代码]

4

1 回答 1

0

试试下面的代码SwitchName

 public string SwitchName
{
    get {
         if (_ciscoswitch != null)
         {   
           return _ciscoswitch.switchName;
         }
         else
         {
            return string.empty;
         }
      }
    set
    {
      if (_ciscoswitch != null)
      {
        if (_ciscoswitch.switchName != value)
        {
           _ciscoswitch.switchName = value;
            RaisePropertyChanged("switchName");
        }
      }
    }
}

如果您不想签_ciscoswitch != null入,请SwitchName放在DataContext = svmInitizlizeComponent() 之前

代码:

public SwitchBrowser(CiscoSwitch cs)
{
    SwitchViewModel svm = new SwitchViewModel(cs);
    DataContext = svm;
    InitializeComponent();
 }

并从中删除以下代码XAML

<Window.DataContext>
<CiscoDecodeWPF:SwitchViewModel/>
</Window.DataContext>

希望它应该工作。

于 2012-10-05T16:17:08.660 回答