首页 > 代码库 > Win8.1应用开发之动态磁贴

Win8.1应用开发之动态磁贴

using demo02.Common;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.ApplicationSettings;
using System.Threading.Tasks;
using Windows.UI.Notifications;
using demo02.DataStructure;
using Windows.Data.Xml.Dom;
using Windows.Storage.Streams;
using Windows.Storage;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage.Pickers;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using demo02.Helper;
using WhereWeGo.Helper;
using System.Net.Http;
using System.Net;
using Windows.Networking.PushNotifications;
using Microsoft.WindowsAzure.Messaging;


//“中心应用程序”模板在 http://go.microsoft.com/fwlink/?LinkId=321221 上有介绍

namespace demo02
{

    sealed partial class App : Application
    {

        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        protected override async void OnLaunched(LaunchActivatedEventArgs e)
        {
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)
            {
                rootFrame = new Frame();    
                SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
                rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    try
                    {
                        await SuspensionManager.RestoreAsync();
                    }
                    catch (SuspensionManagerException)
                    {

                    }
                }

                SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
                Window.Current.Content = rootFrame;
            }
          

            if (rootFrame.Content == null)
            {

                rootFrame.Navigate(typeof(HubPage), e.Arguments);
            }

            await ConfigureNotifications(); //动态磁贴的调用

            Window.Current.Activate();
        }

		
        private async static Task ConfigureNotifications()
        {
            // Send local notifications
            //TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueueForSquare310x310(true);

            var topRated = await demo02.DataModel.WWGDataSource.GetTileUAsync(); //获得动态磁贴的数据来源
			
            foreach (var recipe in topRated.Universities) //topRated.Universities是个ObservableCollection<OfflinePicUri>
            {
                var templateContent = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare310x310BlockAndText02); //设定动态磁贴采用的模板

				//将图片绑定到动态磁贴上
				//特别注意:图片不能太大,否则不能显示
                var imageAttributes = templateContent.GetElementsByTagName("image");
                ((XmlElement)imageAttributes[0]).SetAttribute("src", "ms-appx:///" + recipe.TileImagePath);
                //System.Diagnostics.Debug.WriteLine(recipe.TileImagePath);
                ((XmlElement)imageAttributes[0]).SetAttribute("alt", recipe.Name);

				//给动态磁贴添加文字
                var tileTextAttributes = templateContent.GetElementsByTagName("text");
                tileTextAttributes[1].InnerText = recipe.Name;
                //tileTextAttributes[3].InnerText = "Brief Info";
                tileTextAttributes[4].InnerText = recipe.Summary;
                //tileTextAttributes[5].InnerText = recipe.Summary;
                tileTextAttributes[6].InnerText = recipe.Stars + " stars";

				//这里要特别注意:要使得各Id互不相同,否则会显示不出
                var tileNotification = new TileNotification(templateContent);
                tileNotification.Tag = recipe.Id;

                TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);

            }
            
        }

    }

}

/*********************************************************************************/

//附上:上面topRated.Universities集合中元素University的类

/*********************************************************************************/
using demo02.Helper;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo02.DataModel
{

        public class University : Base
        {
          
            public University(String id, String name, String summary, String imagePath, String category, double stars, String tileImagePath)
                : base(id, name, summary, imagePath)
            {
                this.Category = category;
                this.Stars = stars;
                this.Projects = new ObservableCollection<Project>();
                this.Images = new ImageHelper();
                this.TileImagePath = tileImagePath;
            }

            public string TileImagePath { get; set; }
            public string Category { get; set; }
            public double Stars { get; set; }
            public ObservableCollection<Project> Projects { get; set; }

            public int ClickTimes { get; set; }
            //兼容
            public ImageHelper Images { get; set; }

        }

}

要注意,同台磁贴用到的namespace有:

using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using System.Threading.Tasks;