Windows 8.1 and Windows Phone 8.1 Universal Application – Agent

Step 1

Download Visual Studio Community 2013 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2013 or if it has already been downloaded and installed then start the application.

Visual Studio Community 2013

Step 2

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

vs-file-new-project

Step 3

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

vs-new-project-agent

Step 4

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

vs-add-new-project

Step 5

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

vs-add-new-project-agent

Step 6

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

vs-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

vs-background-class-properties-agent

Step 8

Select from the Menu, View then Code

vs-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:

vs-background-code-agent

Step 10

Select from the Menu, Build, then Build Solution

vs-build-solution

Step 11

From the Solution Explorer select the MainPage.xaml from the Project ending with .Windows

vs-windows-mainpage

Step 12

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

vs-windows-add-reference-agent

Step 13

From the Reference Manager window select Solution then Projects from first list, and from the second make sure that Agent.Background from the list, then type in the Name Agent.Background is selected and ticked then tap select Ok button

vs-windows-reference-agent

Step 14

Then from Solution Explorer select the MainPage.xaml from the Project ending with .WindowsPhone

vs-phone-mainpage

Step 15

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

vs-phone-add-reference-agent

Step 16

From the Reference Manager window select Solution then Projects from first list, and from the second make sure that Agent.Background from the list, then type in the Name Agent.Background is selected and ticked then tap select Ok button

vs-phone-reference-agent

Step 17

Then from the Solution Explorer select the Project ending with .Shared

vs-shared

Step 18

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

vs-add-new-item

Step 19

From the Add New Item window select Visual C# from Installed then select Code File from the list, then type in the Name as Shared.cs before selecting Add to add the file to the Project

vs-shared-code-file

Step 20

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

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

public class Shared
{
    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:

vs-shared-agent

Step 21

From the Solution Explorer select the MainPage.xaml from the Project ending with .Windows

vs-windows-mainpage

Step 22

Select from the Menu, View then Designer

vs-designer

Step 23

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>
	<CommandBar IsOpen="True" IsSticky="True">
		<AppBarButton Icon="Save" Label="Save" Click="Save_Click"/>
		<AppBarButton Name="Toggle" Icon="Play" Label="Start Agent" Click="Toggle_Click"/>
	</CommandBar>
</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:

vs-windows-xaml-agent

Step 24

Select from the Menu, View then Code

vs-code

Step 25

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

public Shared Shared = new Shared();

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)
{
	Shared.Save(Value.Text);
}

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

Also inside the public MainPage() { … } method below this.InitializeComponent(); the following should be entered:

toggleButton(Shared.Init());

It should then appear as such:

vs-windows-code-agent

Step 26

From the Solution Explorer select the Package.appxmanifest from the Project ending with .Windows

vs-windows-package

Step 27

Select from the Menu, View then Designer

vs-designer

Step 28

The Design View will be displayed for Package.appxmanifest then select the Application tab and in the Notifications section set Toast Capable to Yes and Lock screen notifications to Badge

vs-windows-package-application-agent

Step 29

While still in the Design View for Package.appxmanifest select Visual Assets tab and select Badge Logo from All Image Assets section

vs-windows-package-assets-agent

Step 30

Download Badge Image to use in the Application

vs-badge

Step 31

With Visual Assets tab and Badge Logo displayed in the Package.appxmanifest select the browse to where Badge Image was downloaded and select Open when file has been found and selected

vs-windows-package-assets-badge-agent

Step 32

While still in the Package.appxmanifest select Declarations tab and select Background Tasks from Available Declarations section, select Add then in Properties section tick Push Notification and System event options and finally in Entry Point type in Agent.Background.Trigger

vs-windows-package-declarations-agent

Step 33

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

vs-windows-debug-local

Step 34

Once the Application has started the following should then appear

vs-windows-run-agent

Step 35

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 PC settings, then Time and Language and then change the Timezone this will cause the text entered from the Application to appear

vs-windows-ran-agent

Step 36

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

vs-stop

Step 37

Another part is the Windows Phone application so from the Solution Explorer select the MainPage.xaml from the Project ending with .WindowsPhone

vs-phone-mainpage

Step 38

Select from the Menu, Project then Set as StartUp Project

vs-startup-project

Step 39

Select from the Menu, View then Designer

vs-designer

Step 40

The Design View will be displayed along with the XAML View and in this above <Grid> enter the following XAML:

<Page.BottomAppBar>
	<CommandBar IsOpen="True" IsSticky="True">
		<AppBarButton Icon="Save" Label="Save" Click="Save_Click"/>
		<AppBarButton Name="Toggle" Icon="Play" Label="Start Agent" Click="Toggle_Click"/>
	</CommandBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid>enter the following XAML:

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

It should appear as such:

vs-phone-xaml-agent

Step 41

Once in the Code View below the protected override void OnNavigatedTo(NavigationEventArgs e) { … } the following should be entered:

public Shared Shared = new Shared();

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)
{
	Shared.Save(Value.Text);
}

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

While still in the Code View in the protected override void OnNavigatedTo(NavigationEventArgs e) method the following should be entered:

toggleButton(Shared.Init());

It should then appear as such:

vs-phone-code-agent

Step 42

From the Solution Explorer select the Package.appxmanifest from the Project ending with .WindowsPhone

vs-phone-package

Step 43

Select from the Menu, View then Designer

vs-designer

Step 44

The Design View will be displayed for Package.appxmanifest then select Application tab and in Notifications section set Toast Capable to Yes

vs-phone-package-application-agent

Step 45

While still in Package.appxmanifest select Declarations tab and select Background Tasks from Available Declarations section, select Add then in Properties section tick System event option and finally in Entry Point type in Agent.Background.Trigger

vs-phone-package-declarations-agent

Step 46

That completes the Windows Phone Application part so Save the Project then select Debug and select Emulator 8.1 WVGA 4 inch 512MB option to run the Application

vs-phone-debug

Step 47

Once the Emulator has started the Application should then appear

vs-phone-run-agent

Step 48

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 Date+Time and then change the Timezone this will cause the text entered from the Application to appear

vs-phone-ran-agent

Step 49

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

vs-stop

Creative Commons License

One thought on “Windows 8.1 and Windows Phone 8.1 Universal Application – Agent

Leave a comment