简体   繁体   English

Windows Phone C#长列表选择器

[英]Windows Phone C# Long List Selector

I am trying to add something from textblock but there's occurs a error that I can't handle with stackoverflow. 我正在尝试从textblock添加一些内容,但是出现了我无法使用stackoverflow处理的错误。

The code: 编码:

List<String> StringsList; 
private void Add_Click(object sender, RoutedEventArgs e)
{

    StringsList.Add(textBox.Text.ToString());
    longListSelector.ItemsSource = StringsList; 
}

That should be simple code that add from the list some string to the Long List selector. 那应该是简单的代码,从列表中向长列表选择器添加一些字符串。 Could you please give me a tip or something? 你能给我个小费吗? I was using the code from here: 我在这里使用代码:

https://code.msdn.microsoft.com/windowsapps/LongListSelector-Demo-45364cc9#content https://code.msdn.microsoft.com/windowsapps/LongListSelector-Demo-45364cc9#content

This is the error: 这是错误:

$exception {System.NullReferenceException: Object reference not set to an instance of an object. $ exception {System.NullReferenceException:对象引用未设置为对象的实例。 at page3.Add_Click(Object sender, RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.b__3()} System.Exception {System.NullReferenceException} 在System.Windows.Controls.Primitives.ButtonBase.OnClick()在System.Windows.Controls.Primitives.ButtonBase.b__3(在System.Windows.Controls.Primitives.ButtonBase.OnClick()处的page3.Add_Click() )} System.Exception {System.NullReferenceException}

Instead of using List use ObservableCollection . 不要使用List使用ObservableCollection Also make make sure its Public . 还要确保其Public

public ObservableCollection<String> StringsList { get; set; } 

    // Constructor 
    public MainPage() 
    { 
        InitializeComponent(); 

        StringsList = new ObservableCollection<string> { "First Text Item", "Second Text Item", "Third Text Item" }; 

        DataContext = StringsList; 
    } 

    private void Add_Click(object sender, RoutedEventArgs e) 
    { 
        StringsList.Add(textBox.Text); 
    } 

The ObservableCollection Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. ObservableCollection表示一个动态数据集合,该集合在添加,删除项或刷新整个列表时提供通知。

Please refer your attached sample carefully. 请仔细参考随附的样本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM