No repetition of values in list (C#)

前端 未结 1 1645
Happy的楠姐
Happy的楠姐 2021-01-27 05:47

I created a favorite list box where the user can save text from Textblock in MainPage.xaml



        
相关标签:
1条回答
  • 2021-01-27 06:04

    You can check for duplicates quite easily using a LINQ Any query at the beginning of the FavoriteButton_Click method:

    private void FavoriteButton_Click( object sender, RoutedEventArgs e )
    {
        //check if there is any item with the same text
        //in which case do not continue
        if ( listobj.Any( l => l.AnswerName == AnswerTextBlock.Text ) ) return;
    
        listobj.Add( new MyData { AnswerName = AnswerTextBlock.Text } );
    
    
        using ( IsolatedStorageFileStream fileStream = Settings1.OpenFile( "MyStoreItems", FileMode.Create ) )
        {
            DataContractSerializer serializer = new DataContractSerializer( typeof( MyDataList ) );
            serializer.WriteObject( fileStream, listobj );
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题