Windows Phone 8.1 Background tasks

Source: Internet
Author: User

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 ();}  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.