RequestPermissions(LocationPermissions, RequestId) If (CheckSelfPermission() != Permission.Granted) Protected override void OnCreate(Bundle savedInstanceState) Public class MainActivity : global:.FormsAppCompatActivity In MainActivity.cs class implement code to Request for External storage on onStart method: Now in your Android Manifest file define the permissions used by our application like below: In your Android project create a new class file FileWriter.cs and edit it as below:Īndroid Project > FileWriter.cs: using XamFileWriter.Droid Now we will have to implement the interface for Android platform and also implement permissions. And we are also displaying the file path using Xamarin Alert Display dialog. For writing the text content to our text file we are using System.IO.File File class. System.IO.File.WriteAllText(filename, ()) Īwait DisplayAlert("File saved to:", System.IO.Path.Combine(filePath, ()).ToString()+".txt", "Ok") Īwait DisplayAlert("Error:", ex.Message.ToString(), "Ok") įor Android platform we will get the file path from interface and for iOS we will use Environment.GetFolderPath() folder to save our file. Var filename = System.IO.Path.Combine(filePath,()+".txt") If(Device.RuntimePlatform = Device.Android)įilePath = DependencyService.Get().getPath()+"/XamarinForms" įilePath = Environment.GetFolderPath() If(string.IsNullOrWhiteSpace(Convert.ToString(txtFileName.Text)))Īwait DisplayAlert("Error", "Please enter File Name","Ok") Įlse if (string.IsNullOrWhiteSpace(Convert.ToString(txtFileText.Text)))Īwait DisplayAlert("Error", "Please enter File Name", "Ok") Private async void BtnSave_Clicked(object sender, EventArgs e) NavigationPage.SetHasNavigationBar(this, true) Public partial class MainPage : ContentPage For iOS we will save it default Special folder. This interface class file will be only used for Android device as we will be saving our Text file to a custom folder in Android devices. Now create a interface class in your project with name IFileWriter.cs and edit it as below: These are for entering file name, its content and to save our Text file. First in your MainPage.xaml file create a Grid control layout and add Entry, Editor and button controls as below:
0 Comments
Leave a Reply. |