Question

What I have:

I currently have this class:

class Storico
{
    private string rigaStorico;

    public string Name
    {
        get { return rigaStorico; }
        set { rigaStorico = value; }
    }

    public Storico(string storic)
    {
        rigaStorico = storic;
    }       
}

This ObservableCollection:

ObservableCollection<Storico> rigaStorico = new ObservableCollection<Storico>();

And this DataTemplate, located into a LongListSelector:

<DataTemplate>
    <Grid>
        <TextBlock Text="●" Margin="0,8,0,0" Foreground="#EE7B00"/>
        <TextBlock Text="{Binding Name}" ManipulationStarted="TextBlock_ManipulationStarted" ManipulationCompleted="TextBlock_ManipulationCompleted">
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu Name="ContextMenu" >
                    <toolkit:MenuItem Name="DeleteItem" Header="Delete Item" Click="DeleteItem_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
        </TextBlock>
    </Grid>
</DataTemplate>

What I need to do:

I actually need take all the strings contained into the LongListSelector, that may be like:

String1
String2
String3

and write them to a file into IsolatedStorage, in inversed order. Something like this:

String3
String2
String1

Obviously enough, the ItemsSource of the Storico is rigaStorico itself:

Storico.ItemsSource = rigaStorico;

I hope my goal is clear and I gave all the stuff that's needed to solve it.

Was it helpful?

Solution 3

Thanks for the help. I solved my issue with the following code:


string[] contenutoStorico = rigaStorico.Select(x => x.Name).ToArray();

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("history.txt", FileMode.Create, FileAccess.Write, myIsolatedStorage)))
{
    for (int i = 0; i < contenutoStorico.Length; i++)
    {
        writeFile.WriteLine(contenutoStorico[contenutoStorico.Length-i-1]);                    
    }
    writeFile.Close();
}

OTHER TIPS

Try this:

IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();

IsolatedStorageFileStream fs = null;
using (fs = savegameStorage.CreateFile("FILENAME"))
{
    if (fs != null)
    {
        var index = 0;
        foreach(var rigaStor in rigaStorico.Reverse())
        {
            byte[] bytes = Encoding.UTF8.GetBytes(rigaStor.Name);
            fs.Write(bytes, index, bytes.Length);
            index = bytes.Length;
        }
    }
}

Not sure why you need them in reverse order, but you could use this FileStorage to write the data out.

FileStorage.WriteSharedData("MyItems.js", rigaStorico.Reverse());

The FileStorage uses json.net under the hood, if you want you could just do:

var storage = IsolatedStorageFile.GetUserStoreForApplication();
using (var fileStream = storage.CreateFile(fileName))
{
    //Write the data
    using (var isoFileWriter = new StreamWriter(fileStream))
    {
        var json = JsonConvert.SerializeObject(rigaStorico.Reverse());
        isoFileWriter.WriteLine(json);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top