Witam serdecznie. Tworzę aplikację do sklepu windows 8.1.
<ListBox x:Name="listBox1" Height="475" Canvas.Left="10" Canvas.Top="229" Width="580" Background="White" BorderBrush="{x:Null}" SelectionChanged="listBox1_SelectionChanged" Loaded="listBox1_Loaded" Unloaded="listBox1_Unloaded">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel x:Name="Panel" Orientation="Horizontal" Width="505" Height="130" VerticalAlignment="Top" ToolTipService.ToolTip="{Binding Note}">
<Border Height="60" Width="60" Margin="10,10,0,10">
<Image Source="{Binding Image}" Stretch="UniformToFill" Margin="0" Height="50" Width="50"/>
</Border>
<StackPanel x:Name="Panel1" Orientation="Vertical" VerticalAlignment="Center" Margin="0,0,0,0" HorizontalAlignment="Center">
<TextBlock x:Name="textboxName" Text="{Binding Name}" Margin="10,0,0,0" Width="100" Height="50" TextTrimming="WordEllipsis" FontSize="22"/>
</StackPanel>
<StackPanel x:Name="Panel2" Orientation="Vertical" VerticalAlignment="Center" Margin="0,0,0,0" HorizontalAlignment="Center">
<TextBlock x:Name="textboxKW" Text="{Binding KW}" Margin="10,0,0,0" Width="100" Height="50" TextTrimming="WordEllipsis" FontSize="22" Foreground="Red" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public class DataSource
{
public string Name { get; set; }
public string Image { get; set; }
public string Note { get; set; }
public string KW { get; set; }
public string RO { get; set; }
public DataSource(string _Name, string _Image, string _Note, string _KW, string _RO)
{
Name = _Name;
Image = _Image;
Note = _Note;
KW = _KW;
RO = _RO;
}
}
}
Chciałbym muc zapisać stan listBoxa1 np. do pliku tekstowego. a dokładniej wartości KW, Name, Note.
Kiedyś stworzyłem taką metodę do zapisu:
class File
{
public File() { }
public async Task Delete(string deletePath)
{
bool fileExists1;
string DeletePath = deletePath;
StorageFolder local = ApplicationData.Current.LocalFolder;
try // Sprawdzanie czy pliki istnieją:
{
StorageFile _File1Check = local.GetFileAsync(DeletePath).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
fileExists1 = _File1Check != null;
}
catch
{
fileExists1 = false;
}
if (fileExists1 == false) //Jeżeli nie istnieje tworzymy plik
{
byte[] fileBytes1 = System.Text.Encoding.UTF8.GetBytes("0".ToCharArray());
var file1 = await local.CreateFileAsync(DeletePath,
CreationCollisionOption.ReplaceExisting);
using (var s = await file1.OpenStreamForWriteAsync())
{
s.Write(fileBytes1, 0, fileBytes1.Length);
}
}
else //Jeżeli istnieje usuwamy
{
var _File1 = await local.GetFileAsync(DeletePath);
await _File1.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
}
public async Task<string> Load(string loadPath)
{
string LoadPath = loadPath;
bool fileExists1;
StorageFolder local = ApplicationData.Current.LocalFolder;
try // Sprawdzanie czy pliki istnieją:
{
StorageFile _File1Check = local.GetFileAsync(LoadPath).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
fileExists1 = _File1Check != null;
}
catch
{
fileExists1 = false;
}
if (fileExists1 == false) //Jeżeli nie istnieje tworzymy plik
{
byte[] fileBytes1 = System.Text.Encoding.UTF8.GetBytes("0".ToCharArray());
var file1 = await local.CreateFileAsync(LoadPath,
CreationCollisionOption.ReplaceExisting);
using (var s = await file1.OpenStreamForWriteAsync())
{
s.Write(fileBytes1, 0, fileBytes1.Length);
return null;
}
}
else //Jeżeli istnieje odczytujemy
{
var _File18 = await local.OpenStreamForReadAsync(LoadPath);
using (StreamReader streamReader1 = new StreamReader(_File18))
{
string Save = streamReader1.ReadToEnd();
return Save;
}
}
}
public async Task Save(string savePath, string save)
{
string SavePath = savePath;
string Save = save;
StorageFolder local = ApplicationData.Current.LocalFolder;
byte[] fileBytes = Encoding.UTF8.GetBytes(Save.ToCharArray());
var file = await local.CreateFileAsync(SavePath,
CreationCollisionOption.ReplaceExisting);
using (var s = await file.OpenStreamForWriteAsync())
{
s.Write(fileBytes, 0, fileBytes.Length);
}
}
}
}
Czy istnieje możliwość wykorzystania jej do zapisu, oraz odczytu stanu listBoxa1?