Universal Windows Platform – Data Input

Data Input demonstrates how to use InputScope for on-screen Keyboards where supported, plus saving data with ApplicationData and loading this later.

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.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-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
vs2017-new-project-window

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.

vs2017-target-platform

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…

vs2017-project-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

vs2017-add-new-item-library

Step 7

Once in the Code View for Library.cs the following should be entered:

using Windows.Storage;

public class Library
{
    public string LoadSetting(string key)
    {
        if (ApplicationData.Current.LocalSettings.Values[key] != null)
        {
            return ApplicationData.Current.LocalSettings.Values[key].ToString();
        }
        else
        {
            return string.Empty;
        }
    }

    public void SaveSetting(string key, string value)
    {
        ApplicationData.Current.LocalSettings.Values[key] = value;
    }
}

In the Library.cs there’s a using statement – this is there to include the necessary functionality from Windows.Storage. The LoadSetting Method takes a Parameter of key this is used first to check if there is an entry in ApplicationData.Current.LocalSettings.Values with the key if this is not null then the value will be Returned, otherwise an Empty string will be Returned instead. The SaveSetting Method takes the Parameters of key and value to store this in the ApplicationData.Current.LocalSettings to be Returned later

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 9

From the Menu choose View and then Designer

vs2017-view-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:

<StackPanel>
	<TextBox Name="Email" PlaceholderText="Email" InputScope="EmailSmtpAddress" Margin="20"/>
	<TextBox Name="Website" PlaceholderText="Website" InputScope="Url" Margin="20"/>
	<TextBox Name="Telephone" PlaceholderText="Telephone" InputScope="TelephoneNumber" Margin="20"/>
</StackPanel>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Name="Open" Icon="OpenLocal" Label="Open" Click="Open_Click"/>
	<AppBarButton Name="Save" Icon="Save" Label="Save" Click="Save_Click"/>
</CommandBar>

The first block of XAML is the main user interface of the Application – which is three TextBox Controls – each of which is for a different piece of information, and if supported will show the relevent On-screen Keyboard InputScope. The second block of XAML is the CommandBar which contains the main operations of the Application

Step 11

From the Menu choose View and then Code

vs2017-view-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)
{
	Email.Text = string.Empty;
	Website.Text = string.Empty;
	Telephone.Text = string.Empty;
}

private void Open_Click(object sender, RoutedEventArgs e)
{
	Email.Text = library.LoadSetting("Email");
	Website.Text = library.LoadSetting("Website");
	Telephone.Text = library.LoadSetting("Telephone");
}

private void Save_Click(object sender, RoutedEventArgs e)
{
	library.SaveSetting("Email", Email.Text);
	library.SaveSetting("Website", Website.Text);
	library.SaveSetting("Telephone", Telephone.Text);
}

Below the MainPage() Method an instance of the Library Class is created, then in the New_Click Event the TextBox Controls have their Text property set to an Empty String, the Open_Click Event will use the LoadSetting Method to Load a Value by Key that has been previously Saved and the Save_Click Event will use the SaveSetting to Save a Value by Key to be Loaded later

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

vs2017-local-machine

Step 14

After the Application has started running you can then input some data such as an Email Address, Website and Telephone Number then store using the Save button and recall the data with the Open button

ran-data-input

Step 15

To Exit the Application select the Close button in the top right of the Application

vs2017-close

Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s