首页 > Windows开发 > 详细

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

时间:2014-07-18 21:32:52      阅读:468      评论:0      收藏:0      [点我收藏+]
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; }

        }

}

bubuko.com,布布扣

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

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

Win8.1应用开发之动态磁贴,布布扣,bubuko.com

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

原文:http://blog.csdn.net/bluecloudmatrix/article/details/37889677

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!