【问题标题】:Exception thrown: 'System.ArgumentNullException' in mscorlib.ni.dll抛出异常:mscorlib.ni.dll 中的“System.ArgumentNullException”
【发布时间】:2015-09-27 08:27:20
【问题描述】:

我是 c# 新手,正在尝试为我的 ObsorvableCollection 类实现 INotifyPropertyChanged

但是它给出了一个错误并且数据没有被绑定。有人请帮我解决这个问题

System.ArgumentNullException 类型的异常发生在 mscorlib.ni.dll 但未在用户代码中处理

Additional information: Value cannot be null.

提前致谢。

我的 Xaml 代码:

<Page 
x:Class="App2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Page.BottomAppBar>
    <CommandBar Background="Orange">
        <AppBarButton Icon="Sort" Label="Sort">
            <AppBarButton.Flyout>
                <MenuFlyout>
                    <MenuFlyoutItem Text="By Upvotes" Click="FilterItem_Click" Tag="name"/>
                    <MenuFlyoutItem Text="By OpenForms" Click="FilterItem_Click" Tag="lname"/>
                    <MenuFlyoutItem Text="By Ideas" Click="FilterItem_Click" Tag="ideas"/>
                </MenuFlyout>
            </AppBarButton.Flyout>
        </AppBarButton>
    </CommandBar>
</Page.BottomAppBar>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{x:Bind Path=person}" Margin="105,130,95,70">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:Person">
                <TextBlock Text="{x:Bind Name}"></TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
   </Grid>
</Page>

我的cs代码:

        using App2.WrittenLibraries;
        using System.Collections.Generic;
        using System.Collections.ObjectModel;
        using System.ComponentModel;
        using System.Linq;
        using System.Threading.Tasks;
        using Windows.UI.Xaml;
        using Windows.UI.Xaml.Controls;

        // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

        namespace App2
        {
            /// <summary>
            /// An empty page that can be used on its own or navigated to within a Frame.
            /// </summary>
            /// 
            public class Person : INotifyPropertyChanged
            {
                private string name;
                public string Name
                {
                    get { return name;  }
                    set
                    {
                        name = value;
                        OnPropertyChanged("Name");
                    }
                }

                private string lastname;
                private string v1;
                private string v2;

                public string Lastname
                {
                    get { return lastname; }
                    set
                    {
                        lastname = value;
                        OnPropertyChanged("Lastname");
                    }
                }

                public event PropertyChangedEventHandler PropertyChanged;

                private void OnPropertyChanged(string propertyName)
                {
                    if (propertyName != null)
                    {

                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    }
                }


                public Person(string v1, string v2)
                {
                    this.v1 = v1;
                    this.v2 = v2;
                }
            }
            public sealed partial class MainPage : Page
             {
                public ObservableCollection<Person> person = new ObservableCollection<Person>();

                public MainPage()
                {
                    this.InitializeComponent();

                    person.Add(new Person("F1", "L1"));
                    person.Add(new Person("F2", "L2"));
                }


                private void FilterItem_Click(object sender, RoutedEventArgs e)
                {
                    MenuFlyoutItem selectedItem = sender as MenuFlyoutItem;

                    if (selectedItem != null)
                    {
                        if (selectedItem.Tag.ToString() == "name")
                        {
                            Util.debugLog("FILTER BY NAME");
                            person = new ObservableCollection<Person>(person.OrderBy(i => i.Name));

                            //FilterByUpvotes()();
                        }
                        else if (selectedItem.Tag.ToString() == "lname")
                        {
                            Util.debugLog("FILTER BY L_NAME");
                            person = new ObservableCollection<Person>(person.OrderBy(i => i.Lastname));
                            //FilterByOpenForm();
                        }
                        else if (selectedItem.Tag.ToString() == "ideas")
                        {
                            Util.debugLog("FILTER BY IDEAS");
                            //person = new ObservableCollection<Person>(person.OrderBy(i => i));

                            //FilterByIdeas();
                        }
                    }
                }
            }
        }

【问题讨论】:

  • @javvaji kiran 添加了答案,请试用解决方案。

标签: c# xaml data-binding observablecollection inotifypropertychanged


【解决方案1】:

您必须弄清楚从哪里获取空值并将代码放在 try catch 块中,并为“System.ArgumentNullException”编写 catch 块。

try
{
   `enter code here` // your code enter code here
}

catch (ArgumentNullException ex)
{
   `enter code here` //code specifically for a ArgumentNullException
}

【讨论】:

    【解决方案2】:

    解决这个问题的第一件事是找出你的代码的哪一部分产生了这个空值,然后试着理解下面的 c# 代码,这对我通过检查检索代码来避免空值非常有用是否会读我为空..就像:

    if(cmd.ExecuteScalar() == null)
        MessageBox.Show("data is null");
    else
        //put ur code here 
    

    另外,不要忘记使用 try catch 块。

    【讨论】:

    • 我实际上有一个 try catch 块,并且 catch 是 catch(Exception ex) 并且 argumentNullException 由于某种原因没有被捕获
    【解决方案3】:

    您的问题在于使用 x:bind 绑定 name 属性。您的姓名将始终为空,因为您没有设置任何值。因此绑定失败。 你能做的是 编辑您的列表视图模板,如

           <ListView ItemsSource="{x:Bind Path=person}" Margin="105,130,95,70" Loaded="ListView_Loaded">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="local:Person">
                        <TextBlock Text="{Binding Name}" Foreground="Red"></TextBlock>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    并在创建人员时在代码中设置名称

     public Person(string v1, string v2)
        {
            this.v1 = v1;
            this.v2 = v2;
            this.name = v1;//set name as v1
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-22
      • 2016-10-23
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多