Windows Phone development and learning-tile

Source: Internet
Author: User


After changing to a company, I developed Windows Phone. Fortunately, I used C #, so it was not very difficult to get started. After a week, I thought it was a bit interesting. This week I learned the tile Technology on Windows Phone.



On the tile in fact, msdn written above very detailed, specific can see here: http://msdn.microsoft.com/zh-cn/library/hh202960 (V = vs.92)






For meProgramTo do the following:



1. implement reverse and Reverse flip of the tile



2. Manual updates for tile users



3. implement automatic update of the tile under the background task



In fact, the first positive and negative flip is very simple. You have set the foreground and background for the tile, and you can flip the tile yourself for all kinds of content. So let's talk about the next two points.






Implement Manual updates for tile users:



After reading msdn, we will find that there are two types of magnetic stickers: one is the only one by default in the system, and the other is user-defined and there can be multiple. Let's talk about modifying the system tile,CodeAs follows:

// The relative path of the picture to be used
        private const string FORE_PIC = "/Images/Background.png";
        private const string BACK_PIC = "/Images/1.png";

        public static void Create (string title, string count, string backtitle, string backcontent)
        {
            ShellTile TileToFind = ShellTile.ActiveTiles.First ();

            
                int Counter = 0;
                StandardTileData data = new StandardTileData ();

                if (! string.IsNullOrEmpty (title))
                {
                    data.Title = title;
                }
                if (int.TryParse (count, out Counter))
                {
                    data.Count = Counter;
                }
                if (! string.IsNullOrEmpty (backtitle))
                {
                    data.BackTitle = backtitle;
                }
                if (! string.IsNullOrEmpty (backcontent))
                {
                    data.BackContent = backcontent;
                }
                data.BackgroundImage = new Uri (FORE_PIC, UriKind.Relative);
                data.BackBackgroundImage = new Uri (BACK_PIC, UriKind.Relative);
                TileToFind.Update (data);

            
The above function can modify the system tile, whether or not the tile is pinned to the home page, this tile will be modified. In fact, although you can't see the system tile on the homepage, the system tile always exists. It turns out to be hidden.

When the user creates a custom tile, only need to modify the code

  // Create new tile
                int Counter = 0;

                // StandardTileData is used to pass ShellTitle's attribute parameters,
                // Such as URI, title, counter, etc. of the front background image.
                StandardTileData myData = new StandardTileData ()
                {
                    Title = string.IsNullOrEmpty (title) == true? String.Empty: title,
                    Count = int.TryParse (count, out Counter) == true? Counter: 0,
                    BackTitle = string.IsNullOrEmpty (backtitle) == true? String.Empty: backtitle,
                    BackContent = string.IsNullOrEmpty (backcontent) == true? String.Empty: backcontent,
                    BackgroundImage = new Uri (FORE_PIC, UriKind.Relative),
                    BackBackgroundImage = new Uri (BACK_PIC, UriKind.Relative)
                };

                ShellTile.Create (new Uri ("/ MainPage.xaml", UriKind.Relative), myData);
Of course, here the user may need to create multiple tiles, in order to indicate the difference, you can modify in uri

ShellTile.Create (new Uri ("/ MainPage.xaml? S = 1", UriKind.Relative), myData);
But before you need to determine whether the tile exists

   ShellTile myTitle = ShellTile.ActiveTiles.FirstOrDefault (m => m.NavigationUri.ToString (). Contains ("s = 1"));
See this article for details: http://blog.csdn.net/tcjiaan/article/details/7313866

Well, user-defined creation, update tile is completed. Let's talk about the system background automatically modify the tile.

Obviously, windows phone supports background programs, that is, although the current program is closed, there are still processes running in the background. When our program is opened for the first time, the background process starts. The maximum running period is 14 days, that is, if the main program does not start again within 14 days, the background program will be closed. The current best practice is to modify the running cycle of the background process every time the main program starts to ensure that it can be updated every time.

The first is background process control:

  public static void SetBackgroundAgent ()
        {
            CancelBackgroundAgent ();
            PeriodicTask periodicTask = new PeriodicTask (GlobalAttributes.REFRESH_AGENT_NAME);
            periodicTask.Description = "refresh reminders";
            periodicTask.ExpirationTime = DateTime.Today.AddDays (10);


            try
            {
                ScheduledActionService.Add (periodicTask);
                ScheduledActionService.LaunchForTest (periodicTask.Name, TimeSpan.FromSeconds (15));
            }
            catch (InvalidOperationException exception)
            {
                if (exception.Message.Contains ("BNS Error: The action is disabled"))
                {
                    MessageBox.Show ("Background agents for this application have been disabled by the user.");
                    // agentsAreEnabled = false;
                }
                if (exception.Message.Contains ("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
                {
                    // No user action required. The system prompts the user when the hard limit of periodic tasks has been reached.
                }
            }
            catch {}
        }
public static void CancelBackgroundAgent ()
        {
            try
            {
                ScheduledActionService.Remove (GlobalAttributes.REFRESH_AGENT_NAME);
            }
            catch {}
        }
Add the following code in ScheduledSyncTaskAgent, you can refer to this article: http://www.cnblogs.com/WilsonWu/archive/2011/12/27/2303340.html

protected override void OnInvoke (ScheduledTask task)
{


if (task.Name == GlobalAttributes.REFRESH_AGENT_NAME)
{
The
                RefreshPlaster (); // Update system tile
}
            else
            {
                NotifyComplete ();
            }

}
Just update the tile code and call the previous code

      public void RefreshPlaster ()
        {
            SetPlaster.Create ("123", "22", "fsdfd", "dsfd"); // The parameters can actually be set by themselves, for example to obtain the system local time
            NotifyComplete ();
        }
Only one background task can be added to windows phone at a time, so if you need to perform multiple tasks, you can only put them into one task, which is like this

if (task.Name == GlobalAttributes.REFRESH_AGENT_NAME)
{
RefreshReminder ();
                RefreshPlaster ();
// ...
// ...
}
The above is some of my own experience, in fact, you can also update the tile through the message push mechanism, but I do n’t need this program, there are specific MSDN above, and finally have to say that MSDN is too powerful.

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.