Windows Phone 8.1 Apps can add multiple background tasks to help the app complete some tasks.
(1) New foreground application
Background tasks are based on the foreground application, so you must have a foreground app.
The foreground app has a simple function of reading the text saved in the file, and the background task is to write the current time to the file.
Front Desk Interface:
<Grid> <grid.rowdefinitions> <RowDefinitionHeight="*"/> <RowDefinitionHeight= "Auto"/> </grid.rowdefinitions> <ViewboxMargin= "20,0"> <TextBlockx:name= "Timetextblock"Text= "Time"/> </Viewbox> <GridGrid.Row= "1"Margin= "20,0"> <grid.columndefinitions> <ColumnDefinitionWidth="*"/> <ColumnDefinitionWidth="*"/> </grid.columndefinitions> <Buttonx:name= "Registerbutton"Content= "Register Task"Margin= "0,0,5,0"HorizontalAlignment= "Stretch"Click= "Registerbutton_click"/> <Buttonx:name= "Unregisterbutton"Grid.column= "1"Content= "Unregister Task"Margin= "5,0,0,0"HorizontalAlignment= "Stretch"Click= "Unregisterbutton_click"/> </Grid></Grid>
Read text information when you open the app:
protected Override Async voidonnavigatedto (NavigationEventArgs e) {awaitshowfiletext ();}Private AsyncTask Showfiletext () {file=awaitApplicationData.Current.LocalFolder.CreateFileAsync ("Time.txt", creationcollisionoption.openifexists); Timetextblock.text=awaitfileio.readtextasync (file);}
(2) Add a Windows Runtime Component project
The background task must be Windows Runtime Component.
(3) Writing a class that inherits from the Ibackgroundtask interface
Create a new class in the project for the background task and inherit the Ibackgroundtask interface, implementing the Run method, which must also be sealed:
Public Sealed classwritingtask:ibackgroundtask{ Public Async voidRun (ibackgroundtaskinstance taskinstance) {varDeferral =taskinstance.getdeferral (); awaitWritetimetofile ("Time.txt"); Deferral.complete (); }Private AsyncTask Writetimetofile (stringpath) { varFile =awaitApplicationData.Current.LocalFolder.CreateFileAsync (path, creationcollisionoption.replaceexisting); awaitfileio.writetextasync (file, DateTimeOffset.Now.ToString ()); }}
Run method is the method that the background task executes.
(4) Add background task in foreground app Manifest
You can set the type of trigger and remember to set the entry point for the background task.
The foreground app then adds a reference to the background task item.
(5) Registration and cancellation of background tasks by foreground application
The final step is to register the background task in the foreground app:
Private Async voidRegisterbutton_click (Objectsender, RoutedEventArgs e) {backgroundexecutionmanager.removeaccess (); awaitBackgroundexecutionmanager.requestaccessasync (); Registertask ();}Private Static voidRegistertask () {Systemtrigger Triger=NewSystemtrigger (Systemtriggertype.timezonechange,false); Backgroundtaskbuilder Taskbuilder=NewBackgroundtaskbuilder (); Taskbuilder.name="Writingtask"; Taskbuilder.settrigger (Triger); Taskbuilder.taskentrypoint=typeof(Zmybackgroundtasks.writingtask). FullName; Taskbuilder.register ();}
The trigger chosen here is "when the zone changes", just for the convenience of testing, you can choose according to your own needs.
The method for de-registering is:
private void Unregisterbutton_click (object sender, RoutedEventArgs e) { var task = BackgroundTaskRegistration.AllTasks.Values.First (); Task. Unregister ( true ); Backgroundexecutionmanager.removeaccess ();}