Windows 10 Universal Windows Platform – Agent

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.

10-home

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

10-file-new-project

Step 3

From the New Project window select Visual C#, Windows and Windows Universal from Installed, Templates then select Blank App (Windows Universal) from the list, then type in the Name Agent and select a Location to save to before selecting Ok to create the Project.

10-new-project-agent

Step 4

Once the Project is created select File, then Add, then New Project… from the Menu.

10-add-project

Step 5

From the Add New Project window select Visual C#, Windows and Windows Universal from Installed, Templates then select Windows Runtime Component (Windows Universal) from the list, then type in the Name Agent.Background then select Ok to add the Project.

10-add-project-agent

Step 6

Once the Project is created from the Solution Explorer select Class1.cs from the added Project ending with .Background

10-background-class-agent

Step 7

After the Class has been selected go to the Properties view and set the File Name to Trigger when changed select Yes when You are renaming a file. Would you also like to perform a rename in this project of all references to the code element ‘Class1’? is displayed

10-background-class-properties-agent

Step 8

Select from the Menu, View then Code

10-code

Step 9

Once in the Code View the following should be entered next to public sealed class Trigger:

: Windows.ApplicationModel.Background.IBackgroundTask

While still in the Code View the following should be entered inside the public sealed class Trigger : IBackgroundTask { … } class:

public void Run(Windows.ApplicationModel.Background.IBackgroundTaskInstance taskInstance)
{
	try
	{
		string value = Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey("value") ?
			(string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["value"] :
			string.Empty;
		StringBuilder template = new StringBuilder();
		template.Append("<toast><visual version='2'><binding template='ToastText02'>");
		template.Append("<text id='2'>TimeZoneChanged</text>");
		template.AppendFormat("<text id='1'>{0}</text>", value);
		template.Append("</binding></visual></toast>");
		Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument();
		xml.LoadXml(template.ToString());
		Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier().Show(
			new Windows.UI.Notifications.ToastNotification(xml));
	}
	catch
	{

	}
}

It should then appear as such:

10-background-code-agent

Step 10

Then select from the Menu, Build, then Build Solution

10-build

Step 11

From the Solution Explorer select MainPage.xaml in the Agent Project

10-mainpage-agent

Step 12

Once done select from the Menu, Project, then Add Reference…

10-add-reference-agent

Step 13

From the Reference Manager window select Projects then Solution from first list, and from the second make sure that Agent.Background is selected and ticked then select Ok to add the reference

10-reference-agent

Step 14

Once done select from the Menu, Project, then Add New Item…

10-project-add-new-item

Step 15

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

10-add-new-item-library

Step 16

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

using System;
using Windows.ApplicationModel.Background;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;

public class Library
{
    private IBackgroundTaskRegistration registration;

    private bool started
    {
        get
        {
            return BackgroundTaskRegistration.AllTasks.Count > 0;
        }
    }

    public bool Init()
    {
        if (started)
        {
            registration = BackgroundTaskRegistration.AllTasks.Values.First();
            return true;
        }
        else
        {
            return false;
        }
    }

    public void Save(string value)
    {
        ApplicationData.Current.LocalSettings.Values["value"] = value;
    }

    public async Task<bool> Toggle()
    {
        if (started)
        {
            registration.Unregister(true);
            registration = null;
            return false;
        }
        else
        {
            try
            {
                await BackgroundExecutionManager.RequestAccessAsync();
                BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                builder.Name = typeof(Agent.Background.Trigger).FullName;
                builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
                builder.TaskEntryPoint = builder.Name;
                builder.Register();
                registration = BackgroundTaskRegistration.AllTasks.Values.First();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

It should then appear as such:

10-library-agent

Step 17

From the Solution Explorer select MainPage.xaml

10-mainpage

Step 18

Select from the Menu, View then Designer

10-designer

Step 19

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 Icon="Save" Label="Save" Click="Save_Click"/>
			<AppBarButton Name="Toggle" Icon="Play" Label="Start Agent" Click="Toggle_Click"/>
		</StackPanel>
	</AppBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”>enter the following XAML:

<TextBox Margin="20" VerticalAlignment="Top" Name="Value"/>

It should appear as such:

10-xaml-agent

Step 20

Select from the Menu, View then Code

10-code

Step 21

Once in the Code View below the public MainPage() { … } the following should be entered:

public Library Library = new Library();

private void toggleButton(bool value)
{
	if (value)
	{
		Toggle.Icon = new SymbolIcon(Symbol.Stop);
		Toggle.Label = "Stop Agent";
	}
	else
	{
		Toggle.Icon = new SymbolIcon(Symbol.Play);
		Toggle.Label = "Start Agent";
	}
}

private void Save_Click(object sender, RoutedEventArgs e)
{
	Library.Save(Value.Text);
}

private async void Toggle_Click(object sender, RoutedEventArgs e)
{
	toggleButton(await Library.Toggle());
}

It should then appear as such:

10-code-agent

Step 22

Then from the Solution Explorer select Package.appxmanifest

10-package-select-agent

Step 23

Select from the Menu, View then Code

10-code

Step 24

The Code View will be displayed for the Package.appxmanifest and in this above </Application> enter the following XML:

<Extensions>
  <Extension Category="windows.backgroundTasks" EntryPoint="Agent.Background.Trigger">
    <BackgroundTasks>
	<Task Type="systemEvent" />
    </BackgroundTasks>
  </Extension>
</Extensions>

It should appear as such:

10-package-agent

Step 25

That completes the Windows Universal Application so Save the Project then select the Debug and Local Machine option to run the Application

10-localmachine

Step 26

Once the Application has started the following should then appear

10-localmachine-run-agent

Step 27

After the Application has started running you can then type some text then select Save then Start Agent, then after selecting Allow to let the Application run in the Background on the Local Machine then select the Start button then Settings, then Time & Language and then change the Time zone this will cause the text entered from the Application to appear

10-localmachine-ran-agent

Step 28

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Step 29

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

10-emulator

Step 30

Once the Emulator has started the Application should then appear

10-emulator-run-agent

Step 31

After the Application has started running you can then type some text then select Save then Start Agent, then in the Emulator select Start button then swipe left to view the list of items then go to Settings then Time & Language then Date & Time and then change the Time zone this will cause the text entered from the Application to appear

10-emulator-ran-agent

Step 32

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Creative Commons License

Advertisements

4 thoughts on “Windows 10 Universal Windows Platform – Agent

  1. I do not know why, but Visual Studio 2015 Community doesn’t show the template for Windows Universal apps. I’m using the RTM version, installed through vs_community.exe; what am I doing wrong?

    Like

    • When installing choose the Custom option and see if the option to install the Windows 10 tools is checked however the RTM tools aren’t supported in pre-release of Windows 10 from Wednesday you should find everything will work well as long as tools are there should be fine!

      Liked by 1 person

      • Hi there! I forgot to mention that I did this, but no Custom option to Windows 10 tools listed. And I’m using Build 10240. Anyway, I read somewhere that Windows 10 Build 10240 SDK will be released tomorrow, so I can wait.:)

        Like

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s