This commit is contained in:
2025-03-25 08:35:27 +01:00
committed by GitHub
parent ada830f4e1
commit 4cc1f09032
8 changed files with 102 additions and 26 deletions
+1
View File
@@ -57,6 +57,7 @@
<!-- Raw Assets (also remove the "Resources\Raw" prefix) --> <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" /> <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
<MauiAsset Include="Resources\Raw\data.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+9 -2
View File
@@ -7,11 +7,18 @@ public class FileService
public FileService() public FileService()
{ {
// Plik, który będzie przechowywał zawartość naszej aplikacji // Plik, który będzie przechowywał zawartość naszej aplikacji
_filePath = Path.Combine(FileSystem.AppDataDirectory, "data.txt"); _filePath = Path.Combine(FileSystem.AppDataDirectory, "tekst.txt");
} }
public async Task SaveToFile(){ public async Task SaveToFile(string content){
await File.WriteAllTextAsync(_filePath, content);
}
public async Task<string> LoadFromFile(){
if (!File.Exists(_filePath))
return "Nie znaleziono pliku!";
return await File.ReadAllTextAsync(_filePath);
} }
public async Task<string> LoadAsset(string asset){ public async Task<string> LoadAsset(string asset){
+4 -1
View File
@@ -8,7 +8,10 @@
<VerticalStackLayout <VerticalStackLayout
Padding="30,0" Padding="30,0"
Spacing="25"> Spacing="25">
<Label Text="Zawartość, która jest zapisywana w plikach aplikacji:" />
<Entry Text="{Binding AppDataFileContent}" />
<Button Text="Załaduj dane" Command="{Binding LoadAppDataFileCmd}" />
<Button Text="Zapisz dane" Command="{Binding SaveAppDataFileCmd}" />
</VerticalStackLayout> </VerticalStackLayout>
</ScrollView> </ScrollView>
+5 -1
View File
@@ -1,10 +1,14 @@
using FileReadAndWrite.Service;
using FileReadAndWrite.ViewModel;
namespace FileReadAndWrite.View namespace FileReadAndWrite.View
{ {
public partial class AppDataFilePage : ContentPage public partial class AppDataFilePage : ContentPage
{ {
public AppDataFilePage() public AppDataFilePage(FileService fileService)
{ {
InitializeComponent(); InitializeComponent();
BindingContext = new AppDataFilePageVM(fileService);
} }
} }
} }
+2 -21
View File
@@ -1,31 +1,12 @@
using System.ComponentModel;
using System.Windows.Input;
using FileReadAndWrite.Service; using FileReadAndWrite.Service;
using FileReadAndWrite.ViewModel;
namespace FileReadAndWrite.View; namespace FileReadAndWrite.View;
public partial class RawFilePage : ContentPage { public partial class RawFilePage : ContentPage {
private readonly FileService _fileService;
private string _rawFileContent = "";
public string RawFileContent {
get => _rawFileContent;
set {
_rawFileContent = value;
OnPropertyChanged(nameof(RawFileContent));
}
}
public ICommand LoadRawFileCmd { get; }
public RawFilePage(FileService fileService) { public RawFilePage(FileService fileService) {
InitializeComponent(); InitializeComponent();
_fileService = fileService;
BindingContext = this; BindingContext = new RawFilePageVM(fileService);
LoadRawFileCmd = new Command(async () => await LoadRawFile());
}
public async Task LoadRawFile(){
RawFileContent = await _fileService.LoadAsset("data.txt");
} }
} }
+43
View File
@@ -0,0 +1,43 @@
using System.ComponentModel;
using System.Windows.Input;
using FileReadAndWrite.Model;
using FileReadAndWrite.Service;
using FileReadAndWrite.View;
namespace FileReadAndWrite.ViewModel;
public class AppDataFilePageVM : INotifyPropertyChanged {
private readonly FileService _fileService;
private string _appDataFileContent = "";
public string AppDataFileContent {
get => _appDataFileContent;
set {
_appDataFileContent = value;
OnPropertyChanged(nameof(AppDataFileContent));
}
}
public ICommand LoadAppDataFileCmd { get; }
public ICommand SaveAppDataFileCmd { get; }
public AppDataFilePageVM(FileService fileService) {
_fileService = fileService;
LoadAppDataFileCmd = new Command(async () => await LoadAppDataFile());
SaveAppDataFileCmd = new Command(async () => await SaveAppDataFile());
}
public async Task LoadAppDataFile(){
AppDataFileContent = await _fileService.LoadFromFile();
}
public async Task SaveAppDataFile(){
await _fileService.SaveToFile(AppDataFileContent);
}
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged(string propertyName) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ public class MainViewModel : INotifyPropertyChanged {
} }
private async Task EnterAppDataFile(){ private async Task EnterAppDataFile(){
await _navigation.PushAsync(new AppDataFilePage()); await _navigation.PushAsync(new AppDataFilePage(_fileService));
} }
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
+37
View File
@@ -0,0 +1,37 @@
using System.ComponentModel;
using System.Windows.Input;
using FileReadAndWrite.Model;
using FileReadAndWrite.Service;
using FileReadAndWrite.View;
namespace FileReadAndWrite.ViewModel;
public class RawFilePageVM : INotifyPropertyChanged {
private readonly FileService _fileService;
private string _rawFileContent = "";
public string RawFileContent {
get => _rawFileContent;
set {
_rawFileContent = value;
OnPropertyChanged(nameof(RawFileContent));
}
}
public ICommand LoadRawFileCmd { get; }
public RawFilePageVM(FileService fileService) {
_fileService = fileService;
LoadRawFileCmd = new Command(async () => await LoadRawFile());
}
public async Task LoadRawFile(){
RawFileContent = await _fileService.LoadAsset("data.txt");
}
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged(string propertyName) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}