You will need to add an interface to the PLC project.
Interface example:
public interface IFileWorker { Task<bool> ExistsAsync(string filename); // проверка существования файла Task SaveTextAsync(string filename, string text); // сохранение текста в файл Task<string> LoadTextAsync(string filename); // загрузка текста из файла Task<IEnumerable<string>> GetFilesAsync(); // получение файлов из определнного каталога Task DeleteAsync(string filename); // удаление файла }
Then for each of the projects (Android, iOS, etc.) to implement this interface.
An example implementation for Andriod:
using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Xamarin.Forms; using System.Linq; [assembly: Dependency(typeof(FileApp.Droid.FileWorker))] namespace FileApp.Droid { public class FileWorker : IFileWorker { public Task DeleteAsync(string filename) { // удаляем файл File.Delete(GetFilePath(filename)); return Task.FromResult(true); } public Task<bool> ExistsAsync(string filename) { // получаем путь к файлу string filepath = GetFilePath(filename); // существует ли файл bool exists = File.Exists(filepath); return Task<bool>.FromResult(exists); } public Task<IEnumerable<string>> GetFilesAsync() { // получаем все все файлы из папки IEnumerable<string> filenames = from filepath in Directory.EnumerateFiles(GetDocsPath()) select Path.GetFileName(filepath); return Task<IEnumerable<string>>.FromResult(filenames); } public async Task<string> LoadTextAsync(string filename) { string filepath = GetFilePath(filename); using (StreamReader reader = File.OpenText(filepath)) { return await reader.ReadToEndAsync(); } } public async Task SaveTextAsync(string filename, string text) { string filepath = GetFilePath(filename); using (StreamWriter writer = File.CreateText(filepath)) { await writer.WriteAsync(text); } } // вспомогательный метод для построения пути к файлу string GetFilePath(string filename) { return Path.Combine(GetDocsPath(), filename); } // получаем путь к папке MyDocuments string GetDocsPath() { return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } } }
For iOS project:
using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Xamarin.Forms; using System.Linq; [assembly: Dependency(typeof(FileApp.iOS.FileWorker))] namespace FileApp.iOS { public class FileWorker : IFileWorker { public Task DeleteAsync(string filename) { File.Delete(GetFilePath(filename)); return Task.FromResult(true); } public Task<bool> ExistsAsync(string filename) { string filepath = GetFilePath(filename); bool exists = File.Exists(filepath); return Task<bool>.FromResult(exists); } public Task<IEnumerable<string>> GetFilesAsync() { IEnumerable<string> filenames = from filepath in Directory.EnumerateFiles(GetDocsPath()) select Path.GetFileName(filepath); return Task<IEnumerable<string>>.FromResult(filenames); } public async Task<string> LoadTextAsync(string filename) { string filepath = GetFilePath(filename); using (StreamReader reader = File.OpenText(filepath)) { return await reader.ReadToEndAsync(); } } public async Task SaveTextAsync(string filename, string text) { string filepath = GetFilePath(filename); using (StreamWriter writer = File.CreateText(filepath)) { await writer.WriteAsync(text); } } string GetFilePath(string filename) { return Path.Combine(GetDocsPath(), filename); } string GetDocsPath() { return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } } }
To call the methods defined in the interface, use the DependencyService
For example, to read from a file, in the PLC project it is enough to add the following line of code:
string Text = await DependencyService.Get<IFileWorker>().LoadTextAsync("Example.txt");
Material is taken from here.