与众不同 windows phone (8) - Tile(磁贴)

简介: 原文:与众不同 windows phone (8) - Tile(磁贴)[索引页][源码下载] 与众不同 windows phone (8) - Tile(磁贴) 作者:webabcd介绍与众不同 windows phone 7.
原文: 与众不同 windows phone (8) - Tile(磁贴)

[索引页]
[源码下载]


与众不同 windows phone (8) - Tile(磁贴)



作者:webabcd


介绍
与众不同 windows phone 7.5 (sdk 7.1) 之磁贴

  • 概述
  • 演示如何创建、更新、删除磁贴
  • 演示如何按计划更新磁贴的正面背景图



示例
1、创建、更新、删除磁贴的 Demo
ShellTileDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.Tile.ShellTileDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <StackPanel Orientation="Vertical">
        <TextBlock Name="lblMsg" />
        <Button Name="btnUpdateApplicationTile" Content="更新 Application Tile" Click="btnUpdateApplicationTile_Click" />
        <Button Name="btnCreateSecondaryTile" Content="创建 Secondary Tile(可以创建多个)" Click="btnCreateSecondaryTile_Click" />
        <Button Name="btnDeleteSecondaryTile" Content="删除全部 Secondary Tile" Click="btnDeleteSecondaryTile_Click" />
    </StackPanel>

</phone:PhoneApplicationPage>

ShellTileDemo.xaml.cs

/*
 * Tile - 磁贴
 *     Tile 的大小是173 * 173;宽 Tile 的大小 356 * 173,需要把 manifest 中的 <TemplateType5> 修改为 <TemplateType6>,但是不会通过微软审核
 *     Tile 分为应用程序磁贴(Application Tile)和次要磁贴(Secondary Tile)
 *     程序无虑如何都有一个 Application Tile(无论它是否被固定到了开始屏幕),只能更新它(不能创建和删除);而 Secondary Tile 是可以创建、更新和删除的,Secondary Tile 如果存在一定是在开始屏幕上
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Microsoft.Phone.Shell;

namespace Demo.Tile
{
    public partial class ShellTileDemo : PhoneApplicationPage
    {
        public ShellTileDemo()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // 演示获取点击 Tile 后传递过来的参数数据
            if (NavigationContext.QueryString.Count > 0)
                lblMsg.Text = "参数 t 的值为:" + this.NavigationContext.QueryString["t"];

            base.OnNavigatedTo(e);
        }

        private void btnUpdateApplicationTile_Click(object sender, RoutedEventArgs e)
        {
            /*
             * StandardTileData - 用于描述 Tile 的数据
             *     Title - 正面标题
             *     BackgroundImage - 正面背景
             *     Count - 正面显示的 badge (徽章),范围 1 - 99
             *     BackTitle - 背面标题
             *     BackBackgroundImage - 背面背景
             *     BackContent - 背面内容
             *     
             * 
             * ShellTile - 用于管理应用程序Tile和次要Tile
             *     Update(StandardTileData data) - 使用指定的 Tile 数据更新已有的 Tile 信息
             *     Delete() - 删除此 Tile
             *     NavigationUri - 此 Tile 的导航地址
             *     
             *     ShellTile.ActiveTiles - 固定到开始屏幕的 Tile 集合。注意:其第一个元素必然是 application tile,无论其是否被固定到了首页
             *     ShellTile.Create(Uri navigationUri, ShellTileData initialData) - 创建一个新的 Secondary Tile(如果有 Secondary Tile,其必然是被固定到开始屏幕的)
             *         navigationUri - 点击 Tile 后所导航到的地址,此值相当于 key 值,不能重复
             *         initialData - 需要创建的 Tile 数据
             */

            /*
             * 注意:
             * 创建次要磁贴时背景图必须使用本地资源(程序包内或独立存储中,如果是独立存储则图像必须位于 Shared/ShellContent)
             * 更新应用程序磁贴或次要磁贴时,可以使用本地资源或远程资源来更新背景图像
             * 磁贴图像可以是 jpg 或 png 或 gif(只显示第一帧),png 或 gif 的透明区域的背景会呈现主题色
             * 当使用远程图像时,不能是https,要小于80KB,必须30秒内下载完
             */

            // 创建应用程序 Tile 的 Demo
            ShellTile applicationTile = ShellTile.ActiveTiles.First();

            StandardTileData newTile = new StandardTileData
            {
                Title = "App Fore Tile Title",
                BackgroundImage = new Uri("TileBackgroundBlue.png", UriKind.Relative),
                Count = 6,
                BackTitle = "App Back Tile Title",
                BackBackgroundImage = new Uri("TileBackgroundGreen.png", UriKind.Relative),
                BackContent = "App Back Tile Content" + Environment.NewLine + "OK"
            };

            applicationTile.Update(newTile);
        }

        private void btnCreateSecondaryTile_Click(object sender, RoutedEventArgs e)
        {
            // 创建次要 Tile 的 Demo
            StandardTileData newTile = new StandardTileData
            {
                Title = "Secondary Fore Tile Title",
                BackgroundImage = new Uri("TileBackgroundBlue.png", UriKind.Relative),
                Count = 6,
                BackTitle = "Secondary Back Tile Title",
                BackBackgroundImage = new Uri("TileBackgroundGreen.png", UriKind.Relative),
                BackContent = "Secondary Back Tile Content" + Environment.NewLine + "OK"
            };

            // uri 是 key 不能重复
            ShellTile.Create(new Uri("/Tile/ShellTileDemo.xaml?t=" + DateTime.Now.Ticks.ToString(), UriKind.Relative), newTile);
        }

        private void btnDeleteSecondaryTile_Click(object sender, RoutedEventArgs e)
        {
            // 删除全部次要磁贴
            if (ShellTile.ActiveTiles.Count() > 1)
            {
                ShellTile lastTile = ShellTile.ActiveTiles.Last();
                lastTile.Delete();
            }

            // xna 关闭应用程序:Microsoft.Xna.Framework.Game.Exit();
            // sl 关闭应用程序
            throw new Exception();
        }
    }
}


2、按计划更新磁贴的正面背景图的 Demo
ShellTileScheduleDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.Tile.ShellTileScheduleDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <StackPanel Orientation="Vertical">
        <Button Name="btnStart" Content="Start Schedule" Click="btnStart_Click" />
        <Button Name="btnStop" Content="Stop Schedule" Click="btnStop_Click" />
    </StackPanel>

</phone:PhoneApplicationPage>

ShellTileScheduleDemo.xaml.cs

/*
 * ShellTileSchedule - 用于按计划更新磁贴的正面背景图
 *     new ShellTileSchedule() - 更新 Application Tile
 *     new ShellTileSchedule(ShellTile tile) - 更新指定的 Secondary Tile
 *     
 *     Recurrence - 更新计划的模式
 *         UpdateRecurrence.Interval - 定时更新
 *         UpdateRecurrence.Onetime - 只更新一次
 *     Interval - 定时更新时的更新间隔(只能是 每小时/每天/每星期/每月)
 *     MaxUpdateCount - 最大的更新次数(默认值是 0,即无限次更新)
 *     StartTime - 开始更新的时间
 *     RemoteImageUri - 需要更新的图像的远程地址
 * 
 *     Start() - 启动计划
 *     Stop() - 停止计划
 *     
 * 
 * 注意:
 * 具体更新时间点是由系统统一调度的(系统每隔一段时间会批处理所有程序的更新计划,这么做是为了省电),也就是说即使你设置了 StartTime = DateTime.Now,也不会马上更新,但是一个小时内应该会更新
 * 如果更新计划失败(比如找不到远程图像,远程图像大于80KB,超过30秒还没下载完等)次数太多,则该更新计划会被系统自动取消
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Microsoft.Phone.Shell;

namespace Demo.Tile
{
    public partial class ShellTileScheduleDemo : PhoneApplicationPage
    {
        private ShellTileSchedule _schedule = new ShellTileSchedule();

        public ShellTileScheduleDemo()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            _schedule.Recurrence = UpdateRecurrence.Interval;
            _schedule.Interval = UpdateInterval.EveryHour;
            _schedule.StartTime = DateTime.Now;
            //_schedule.MaxUpdateCount = 100;
            _schedule.RemoteImageUri = new Uri(@"http://image.sinajs.cn/newchart/small/nsh000300.gif");
            _schedule.Start();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            _schedule.Stop();
        }
    }
}



OK
[源码下载]

目录
相关文章
|
Android开发 iOS开发 Windows
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
不久前,随着最后一家WP手机厂商惠普宣布取消今后Windows Phone的研发计划,以及微软官方声明对WP8.1系统今后所有升级维护的终止,WP手机,作为曾经和安卓手机、苹果手机并驾齐驱的三大智能手机之一,正式寿终正寝。
1247 0
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
|
XML 开发框架 前端开发
Windows Phone快速入门需掌握哪些能力
在此之前,先普及下Windows Phone的概念和开发工具的介绍。 Windows Phone是微软公司开发的手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验集成至手机中。2012年6月21日,微软正式发布Windows Phone 8,采用和Windows 8相同的Windows NT内核,同时也针对市场的Windows Phone 7.5发布Windows Phone 7.8。
133 0
Windows Phone快速入门需掌握哪些能力
|
编解码 前端开发 JavaScript
Windows Phone 下开发 LBS 应用
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
162 0
|
移动开发 Android开发 开发者
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
在Build 2014大会上,微软正式发布了传闻已久的Windows Phone 8.1系统,所有的Windows Phone 8手机都可以升级,微软这次可谓是十分厚道。虽然并非迭代升级,但WP 8.1还是拥有很多重大更新,对于微软进一步完善移动平台拥有积极的意义。下面,就一起来了解一下WP 8.1的主要新特性。
230 0
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
|
前端开发 Android开发 Windows
Android实现Windows 8磁贴(Tile)样式按钮
《Android实现Windows 8磁贴(Tile)样式按钮》 效果图如下: 我在网上流传的代码基础上精简、整理出一个简单的类,我暂时把它命名为:Windows8TileImageView,即Windows 8磁贴(Tile)样式按钮,Windows8TileImageView其实就是继承于标准Android ImageView,单击该Windows8TileImageView有收缩、侧边收缩等比较有趣的效果。
1046 0
|
1月前
|
安全 数据安全/隐私保护 Windows
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
|
1月前
|
存储 安全 网络安全
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
|
1月前
|
网络协议 数据安全/隐私保护 Windows
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
45 1