Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 非Null属性上的Null值异常_C#_Xaml - Fatal编程技术网

C# 非Null属性上的Null值异常

C# 非Null属性上的Null值异常,c#,xaml,C#,Xaml,在非null的属性上获取NullValueException 以下是ViewModel的开始代码,以及创建ViewModel对象并打开使用ViewModelt的窗口的方法。正在SwitchName属性上引发异常。由于SwitchVewModel中的_ciscoswitch为null,因此_ciscoswitch.SwitchName显示为null。在SwitchBrowser构造函数中的InitializeComponent()处引发异常。查看调试器中的SwitchVewModel实例,\ u

在非null的属性上获取NullValueException

以下是ViewModel的开始代码,以及创建ViewModel对象并打开使用ViewModelt的窗口的方法。正在SwitchName属性上引发异常。由于SwitchVewModel中的_ciscoswitch为null,因此_ciscoswitch.SwitchName显示为null。在SwitchBrowser构造函数中的InitializeComponent()处引发异常。查看调试器中的SwitchVewModel实例,\ u 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>
内部异常:

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\u单击(对象发送器,System.Windows.RoutedEventTargets e)行1050+0x34字节C# [外部代码]


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");
        }
      }
    }
}
如果您不想检查
\u ciscoswitch!=在
SwitchName
中为null
,然后将
DataContext=svm
放在InitizlizeComponent()之前

代码:

public SwitchBrowser(CiscoSwitch cs)
{
    SwitchViewModel svm = new SwitchViewModel(cs);
    DataContext = svm;
    InitializeComponent();
 }
并从
XAML
中删除以下代码

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


希望它能起作用。

几乎可以肯定,您的XAML有问题。请发布(并删除部分C#),同时发布整个堆栈跟踪。使用
FirstOrDefault
中的
new SwitchBrowser(cs.FirstOrDefault())
可以轻松地将
null
作为
CiscoSwitch
传递。。。如果您的代码使用默认的
public switchview model()
constructor
\u ciscoswitch
也将为
null
..@ChrisF,则为SwitchBrowser窗口发布XAML,或者至少是使用SwitchName@Kirk,发布了Exeption和调试器为堆栈tracethanks Jignesh提供的内容,_ciscoswitch不应为空。我有一个我正试图作为模型传入的CiscoSwitch对象的实例。@DavidGreen此行试图从viewmodel获取Switchname,但当时_CiscoSwitch为空。使用viewmodel的CiscoSwitch参数New(),您将在codebehind创建它,并在初始化()后分配DataContext值。但在设计时,您刚刚使用default New()创建了Viewmodel的新对象。试试我的答案吧,行了。问题是有几个属性,显然我必须测试它们。如果有办法的话,我宁愿找到一种让XAML调用接受参数的构造函数的方法。“到目前为止,我还没有找到答案。@DavidGreen在InitializeComponent()分配DataContext值之前,请查看我更新的答案。”。
<Window.DataContext>
<CiscoDecodeWPF:SwitchViewModel/>
</Window.DataContext>