Text Editor demonstrates how to use a TextBox to open and save plain text documents
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using System.Collections.Generic; using System.Threading.Tasks; using Windows.Storage; using Windows.Storage.Pickers; using Windows.UI.Popups; using Windows.UI.Xaml.Controls; public class Library { private const string app_title = "Text Editor"; private const string file_extension = ".txt"; private async Task<bool> ConfirmAsync(string content, string title, string ok, string cancel) { bool result = false; MessageDialog dialog = new MessageDialog(content, title); dialog.Commands.Add(new UICommand(ok, new UICommandInvokedHandler((cmd) => result = true))); dialog.Commands.Add(new UICommand(cancel, new UICommandInvokedHandler((cmd) => result = false))); await dialog.ShowAsync(); return result; } public async void NewAsync(TextBox display) { if (await ConfirmAsync("Create New Document?", app_title, "Yes", "No")) { display.Text = string.Empty; } } public async void OpenAsync(TextBox display) { try { FileOpenPicker picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.DocumentsLibrary }; picker.FileTypeFilter.Add(file_extension); StorageFile file = await picker.PickSingleFileAsync(); display.Text = await FileIO.ReadTextAsync(file); } catch { } } public async void SaveAsync(TextBox display) { try { FileSavePicker picker = new FileSavePicker { SuggestedStartLocation = PickerLocationId.DocumentsLibrary, DefaultFileExtension = file_extension, SuggestedFileName = "Document" }; picker.FileTypeChoices.Add("Plain Text", new List<string>() { file_extension }); StorageFile file = await picker.PickSaveFileAsync(); if (file != null) { await FileIO.WriteTextAsync(file, display.Text); } } catch { } } }
In the Library.cs there are using statements to include the necessary functionality. The ConfirmAsync Method is used to create a dialog with two buttons, this is used by the NewAsync method to confirm if a new document is to be created, if the answer is “Yes” then the TextBox will have it’s contents set to an empty string. OpenAsync is used to set the TextBox contents to the contents of a file – this is opened with a FileOpenPicker using ReadTextAsync, SaveAsync is used to get the contents of the TextBox and then this is saved to a file, using the FileSavePicker and WriteTextAsync.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<TextBox Name="Display" AcceptsReturn="True" TextWrapping="Wrap" VerticalAlignment="Stretch"/> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/> <AppBarButton Name="Open" Icon="OpenFile" Label="Open" Click="Open_Click"/> <AppBarButton Name="Save" Icon="Save" Label="Save" Click="Save_Click"/> </CommandBar>
The first block of XAML the main user interface of the Application, this features a Grid with a top row to display a TextBox which is where the RSS feed address can be typed in, followed by Enter to navigate and then there’s the ItemsControl itself in the second row. The second block of XAML is is the CommandBar which contains the New, Open and Save commands to perform the actions of the Text Editor using the TextBox.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); private void New_Click(object sender, RoutedEventArgs e) { library.NewAsync(Display); } private void Open_Click(object sender, RoutedEventArgs e) { library.OpenAsync(Display); } private void Save_Click(object sender, RoutedEventArgs e) { library.SaveAsync(Display); }
Below the MainPage() Method an instance of the Library Class is created. The New_Click, Open_Click and Save_Click Methods call the related methods in the Library Class.
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
After the Application has started running you can then type in any text then you can Save the document to Open later or start a New document
Step 15
To Exit the Application select the Close button in the top right of the Application