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.
Step 2
Once Visual Studio Community 2013 has started select File, then New, then Project… from the Menu.
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.
Step 4
Once the Project is created from the Menu select File, then Add, then 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.
Step 6
Once the Project is created from the Solution Explorer select Class1.cs from the added Project ending with .Background
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
Step 8
Select from the Menu, View then 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:
Step 10
Select from the Menu, Build, then Build Solution
Step 11
From the Solution Explorer select the MainPage.xaml from the Project ending with .Windows
Step 12
Once done select from the Menu, Project, then Add Reference…
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
Step 14
Then from Solution Explorer select the MainPage.xaml from the Project ending with .WindowsPhone
Step 15
Once done select from the Menu, Project, then Add Reference…
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
Step 17
Then from the Solution Explorer select the Project ending with .Shared
Step 18
Once done select from the Menu, Project, then 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
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:
Step 21
From the Solution Explorer select the MainPage.xaml from the Project ending with .Windows
Step 22
Select from the Menu, View then 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:
Step 24
Select from the Menu, View then 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:
Step 26
From the Solution Explorer select the Package.appxmanifest from the Project ending with .Windows
Step 27
Select from the Menu, View then 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
Step 29
While still in the Design View for Package.appxmanifest select Visual Assets tab and select Badge Logo from All Image Assets section
Step 30
Download Badge Image to use in the Application
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
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
Step 33
That completes the Windows Application part so Save the Project then select the Debug and Local Machine option to run the Application
Step 34
Once the Application has started the following should then appear
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
Step 36
To Exit the application select Stop Debugging in Visual Studio Community 2013
Step 37
Another part is the Windows Phone application so from the Solution Explorer select the MainPage.xaml from the Project ending with .WindowsPhone
Step 38
Select from the Menu, Project then Set as StartUp Project
Step 39
Select from the Menu, View then 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:
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:
Step 42
From the Solution Explorer select the Package.appxmanifest from the Project ending with .WindowsPhone
Step 43
Select from the Menu, View then 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
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
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
Step 47
Once the Emulator has started the Application should then appear
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
Step 49
To Exit the application select Stop Debugging in Visual Studio Community 2013
One thought on “Windows 8.1 and Windows Phone 8.1 Universal Application – Agent”