列表中没有重复的值 (C#) [英] No repetition of values in list (C#)

查看:21
本文介绍了列表中没有重复的值 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个收藏夹列表框,用户可以在其中保存来自 MainPage.xaml

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

<StackPanel Grid.Row="0" Grid.Column= "0" HorizontalAlignment= "Left" VerticalAlignment= "Top" >
    < Button x:Name= "FavoriteButton" FontFamily= "Segoe MDL2 Assets"
            Content= "&#xE006;" BorderBrush= "Transparent" FontSize= "28"
            Foreground= "{StaticResource PhoneForegroundBrush}"
            Style= "{StaticResource ButtonStyle1}" Click= "FavoriteButton_Click" />
</ StackPanel >

<StackPanel Grid.Row="0" Grid.Column= "2" HorizontalAlignment= "Left" VerticalAlignment= "Top" >
    < Button x:Name= "FavoriteListButton" FontFamily= "Segoe MDL2 Assets"
            Content= "&#xEA55;" BorderBrush= "Transparent" FontSize= "28"
            Foreground= "{StaticResource PhoneForegroundBrush}"
            Style= "{StaticResource ButtonStyle1}" Click= "FavoriteListButton_Click" />
</ StackPanel >

C#

    private void FavoriteButton_Click(object sender, RoutedEventArgs e)
    {

        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);

        }
    }

    private void FavoriteListButton_Click(object sender, RoutedEventArgs e)
    {
        if(FavoriteListBox.Visibility.Equals(Visibility.Collapsed))
        {
            FavoriteListBox.Visibility = Visibility.Visible;
        }
        else if(FavoriteListBox.Visibility.Equals(Visibility.Visible))
        {
            FavoriteListBox.Visibility = Visibility.Collapsed;
        }           
    }

和列表框

xml

        <StackPanel Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Top">                
            <ListBox x:Name="FavoriteListBox" Visibility="Collapsed" 
                     SelectionChanged="FavoriteListBox_SelectionChanged"
                     HorizontalAlignment="Stretch"
                     VerticalAlignment="Top" Opacity="1"
                     Background="{StaticResource PhoneBackgroundBrush}" Foreground="{StaticResource PhoneForegroundBrush}"
                     Height="300" Width="250">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Visibility="Visible" x:Name="FavoriteListBoxTextBlock"  
                                   FontSize="35" Foreground="Black" Text="{Binding AnswerName}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>

C#

public partial class MainPage : PhoneApplicationPage
{
    IsolatedStorageFile Settings1 = IsolatedStorageFile.GetUserStoreForApplication();
    MyDataList listobj = new MyDataList();

    public MainPage()

    {
        InitializeComponent();

        this.Loaded += MainPage_Loaded;

        if (Settings1.FileExists("MyStoreItems"))
        {
            using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
                listobj = (MyDataList)serializer.ReadObject(fileStream);

            }
        }


        FavoriteListBox.ItemsSource = listobj;//binding isolated storage list data



    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (Settings1.FileExists("MyStoreItems"))//loaded previous items into list
        {
            using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
                listobj = (MyDataList)serializer.ReadObject(fileStream);

            }
        }
    }

    public class MyData
    {
        public string AnswerName { get; set; }            
    }
    public class MyDataList : ObservableCollection<MyData> //for storing mydata class items with type of list
    {

    }

显示一些文本的 Textbox 的所有文本在点击收藏夹按钮时进入隔离存储.但即使该文本已存在于列表中,它也会再次复制相同的文本,所以我希望当用户单击收藏夹按钮时,它应该首先检查列表中是否存在文本,如果存在,则应替换或不应复制再次.那么如何做到这一点.

All text of Textbox which show some text go to isolated storage on favorite button click. but it copies same text again even if that text already exist in the list, so I want when ever user click on favorite button it should check first if text already exist in the list or not, If exist then it should replace or should not copy again. so how to do this.

推荐答案

您可以使用 FavoriteButton_Click 方法开头的 LINQ Any 查询轻松检查重复项:

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 );

    }
}

这篇关于列表中没有重复的值 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆