Step 1
Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.
Step 2
Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.
Step 3
From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.
Step 4
Once done select from the Menu, Project, then Add New Item…
Step 5
From the Add New Item window select Visual C# 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 6
Once in the Code View for Library.cs the following should be entered:
using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Xml.Linq; using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Provider; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; public class Library { public async Task<bool> Confirm(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 void Add(ref ListBox display, string value, KeyRoutedEventArgs args) { if (args.Key == Windows.System.VirtualKey.Enter) { try { CheckBox item = new CheckBox(); item.Content = value; item.Foreground = display.Foreground; if (display.SelectedIndex > -1) { // Insert after Selected display.Items.Insert(display.SelectedIndex, item); } else { // Add after End display.Items.Add(item); } } catch { } display.Focus(FocusState.Keyboard); } } public void Remove(ref ListBox display) { if (display.SelectedIndex > -1) { display.Items.RemoveAt(display.SelectedIndex); } } public async void New(ListBox display) { if (await Confirm("Create New List?", "Task Editor", "Yes", "No")) { display.Items.Clear(); } } private static async void write(StorageFile file, string value) { try { if (file != null) { CachedFileManager.DeferUpdates(file); await FileIO.WriteTextAsync(file, value); FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); } } catch { } } public async void Open(ListBox display) { try { FileOpenPicker picker = new FileOpenPicker(); picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; picker.FileTypeFilter.Add(".tsk"); StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { string value = await FileIO.ReadTextAsync(file); List<CheckBox> list = new List<CheckBox>(); XElement xml = XElement.Parse(value); if (xml.Name.LocalName == "tasklist") { display.Items.Clear(); foreach (XElement task in xml.Descendants("task")) { display.Items.Clear(); CheckBox item = new CheckBox(); item.IsChecked = task.FirstAttribute.Value.ToLower() == "checked"; item.Content = task.Value; display.Items.Add(item); } } } } catch { } } public async void Save(ListBox display) { try { XElement items = new XElement("tasklist"); foreach (CheckBox item in display.Items) { items.Add(new XElement("task", item.Content, new XAttribute("value", ((bool)item.IsChecked ? "checked" : "unchecked")))); } string value = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), items).ToString(); FileSavePicker picker = new FileSavePicker(); picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; picker.FileTypeChoices.Add("Task List", new List<string>() { ".tsk" }); picker.DefaultFileExtension = ".tsk"; picker.SuggestedFileName = "Document"; StorageFile file = await picker.PickSaveFileAsync(); if (file != null) { CachedFileManager.DeferUpdates(file); await FileIO.WriteTextAsync(file, value); FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); } } catch { } } }
It should then appear as such:
Step 7
From the Solution Explorer select MainPage.xaml
Step 8
Select from the Menu, View then Designer
Step 9
The Design View will be displayed along with the XAML View and in this above <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:
<Page.BottomAppBar> <AppBar IsOpen="True" IsSticky="True"> <StackPanel Orientation="Horizontal"> <AppBarButton Name="New" Icon="Page" Label="New" Click="New_Click"/> <AppBarButton Name="Open" Icon="Folder" Label="Open" Click="Open_Click"/> <AppBarButton Name="Save" Icon="Save" Label="Save" Click="Save_Click"/> <AppBarButton Name="Remove" Icon="Remove" Label="Remove" Click="Remove_Click"/> </StackPanel> </AppBar> </Page.BottomAppBar>
While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”>enter the following XAML:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBox Grid.Row="0" Margin="20" KeyDown="Add_KeyDown" Name="Value"/> <ListBox Grid.Row="1" VerticalAlignment="Stretch" FontFamily="Segoe UI" FontSize="16" Name="Display"/> </Grid>
It should appear as such:
Step 10
Select from the Menu, View then Code
Step 11
Once in the Code View below the public MainPage() { … } the following should be entered:
public Library Library = new Library(); private void New_Click(object sender, RoutedEventArgs e) { Library.New(Display); } private void Open_Click(object sender, RoutedEventArgs e) { Library.Open(Display); } private void Save_Click(object sender, RoutedEventArgs e) { Library.Save(Display); } private void Remove_Click(object sender, RoutedEventArgs e) { Library.Remove(ref Display); } private void Add_KeyDown(object sender, KeyRoutedEventArgs e) { Library.Add(ref Display, Value.Text, e); }
It should then appear as such:
Step 12
That completes the Windows Universal Application so Save the Project then select the Debug and Simulator option to run the Application
Step 13
Once the Simulator has started the Application should then appear
Step 14
After the Application has started running you can then type in some text in the upper textbox then press or tap enter to add a Task then you can Save the list to Open later or start a New list you can also tick the checkbox when the Task is done
Step 15
To Exit the application select Stop Debugging in Visual Studio Community 2015
Step 16
Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application
Step 17
Once the Emulator has started the Application should then appear
Step 18
After the Application has started running you can then type in some text in the upper textbox then press or tap enter to add a Task then you can Save the list to Open later or start a New list you can also tick the checkbox when the Task is done
Step 19
To Exit the application select Stop Debugging in Visual Studio Community 2015
there is a redundance code is “ display.Items.Clear();” in foreach in function “Open”;
LikeLike
Well spotted – that second one shouldn’t be there, thanks for pointing that out!
LikeLike