简体   繁体   中英

windows phone 8.1 history

I'm creating an windows phone 8.1 app. I am trying to save the clicked/tapped items in the listview but i dont know how.

The Code im trying todo:

List<string> History = new List<string>();

    private async Task SaveTextFile(string str)
    {
        StorageFolder Folder1 = ApplicationData.Current.LocalFolder;
        StorageFolder Folder2 = await Folder1.CreateFolderAsync("Sample Save", CreationCollisionOption.OpenIfExists);
        StorageFile File1 = await Folder2.CreateFileAsync("History.txt", CreationCollisionOption.ReplaceExisting);
        byte[] data = Encoding.Unicode.GetBytes(str);
        using (var f = await File1.OpenStreamForWriteAsync())
        {
            await f.WriteAsync(data, 0, data.Length);
        }
    }

    private async Task<string> GetTextFile()
    {
        StorageFolder Folder1 = ApplicationData.Current.LocalFolder;
        StorageFolder Folder2 = await Folder1.CreateFolderAsync("Sample Save", CreationCollisionOption.OpenIfExists);
        try
        {
            StorageFile File1 = await Folder2.GetFileAsync("History.txt");
            return (await FileIO.ReadTextAsync(File1));
        }
        catch (Exception)
        {
            return "";
        }
    }

    private async void Refresh_It()
    {
        try
        {
            string Str1 = await GetTextFile();
            TextBox1.Text = Str1;
            string[] Str2 = Str1.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < Str2.Count(); i++)
            {
                History.Add(Str2[i]);
            }

            for (int i = 0; i < ListView1.Items.Count; i++)
            {
                if (History.IndexOf(ListView1.Items[i].ToString()) != -1)
                {
                    ListView1.Items[i] = ListView1.Items[i].ToString() + " Clicked";
                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }


    }

    private async void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 40; i++)
        {
            ListView1.Items.Add(i.ToString());
        }
        Refresh_It();
    }

    private async void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            string selecteds = e.AddedItems.First().ToString();
            History.Add(selecteds);
            string str1 = "";
            for (int i = 0; i < History.Count; i++)
            {
                str1 +=  History[i];
                if (i != (History.Count -1)) { str1 += "/"; }
            }
            await SaveTextFile(str1);
            History.Clear();

        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }

        Refresh_It();

    }

XML:

       <Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ListView x:Name="ListView1" Grid.Row="1" Background="White" Foreground="Black" SelectionChanged="ListView1_SelectionChanged"/>
    <ScrollViewer Height="Auto" Grid.Row="0">
        <TextBlock x:Name="Label1"/>
    </ScrollViewer>

</Grid>

In my Code im trying to Get The History File text and splitting it to put it on "History(a list of string)". It add a string in "History(list)" always when selection is changed. But for some reason my code isnt working it seems the problem is in when splitting and checking if the listitem is in the "History(list of string)".

I want my application to add a ex."24 Clicked" in the ListView if there is a "24" in History. Im loading the history in the pageLoad and when an item is clicked.

Help Please. sorry for bad english.

Why don't you use SQLite ? SQLite will be more helpful and much easier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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