关于非空的属性的空值异常

时间:2012-10-05 16:09:54

标签: c# xaml

在非空的属性

上获取NullValueException

这是ViewModel开头的代码,以及创建ViewModel对象并打开我正在使用ViewModelt的Window的方法。在SwitchName属性上抛出异常。 _ciscoswitch.SwitchName将显示为null,因为SwitchVewModel中的_ciscoswitch为null。抛出SwitchBrowser构造函数中的InitializeComponent()会抛出异常。查看调试器中的SwitchVewModel实例,_ciscoswitch不为null。我尝试更改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

InnerException:

  

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#       [外部代码]

1 个答案:

答案 0 :(得分: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 = svm放在InitizlizeComponent()之前

<强> CODE:

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

XAML中移除以下代码。

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

希望它应该有用。