Windows Phone开发(12):认识一下独具个性的磁贴

移动开发
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验整合至手机中。

对“磁贴”的理解是一点也不抽象的,为什么呢?只要你愿意启动WP系统,无论你是在模拟器中还是在真机中,是的,桌面上那一块块像地板的玩意儿,就是磁贴了。
(图:磁贴)

在上图中,我们很直观地看到磁贴图块的“真”面目,呵呵,其实很明显,这个设计真的很有个性,老实说,WP刚推出的时候,很多人说它这个那个的,其实这些人只过是起起哄罢了。
我是挺喜欢桌面上那一块块的图块的,这也是WP第一次给我留下的深刻印象,毕竟在安卓和IOS上,我们看到的都是传统手机的图标显示方式,而WP总让我觉得很有个性。

好的,看完了整体的,我们来看看局部的,好吗?别小看这些正方形的图块,里面可是大有文章的哦。不信?一起去瞧瞧。

磁贴的一个图块基本上由三个元素组成——背景图,标题文字以及计数器,嗯,当然了,图块最有意思的地方,就是它有正反两面。
下图为图块正反两面的元素组成结构图。


磁贴的分类。

磁贴分为应用程序磁贴和次要磁贴。
好不好理解呢?好,首先我们看看应用程序磁贴,它是指通过用户应用程序列表中长按应用程序来把应用程序固定到“开始”屏幕。

那么,如何删除呢?在“开始”屏幕上找到你要移除的图块,长按,图块左上角会出现一个小图标,我们点击这小图标即可移除该磁贴。当然了,如果你想把移动到其它位置,在此时,你只需把图标拖到对应的位置,然后在桌面上随便点一下即可完成移动操作。


次要磁贴就是相对于刚才的上面的应用程序磁贴而言的,它是由应用程序通过特定参数创建的,说直接一点嘛,就是通过我们开发者,用代码来创建的。

这个好比我们Windows桌面上的快捷方式,有的是直接指向可执行程序的,而有的是应用程序创建的,它后面带了命令行参数。举个例子吧,最典型的 要数IE了,我经常上新浪微博,我希望在桌面上创建一个IE的快捷方式图标,双击运行IE的时候新打开新浪微博主页,那怎么做呢?请看下图。


这样一来,你双击快捷方式启动IE就自动打开新浪微博首页。呵呵,次要磁贴也和这相类似。

动手实战。

下面,我们一起来动手做一个稍微综合一点的例子,嗯,现在,你可以坐下来,先喝几口奶茶,然后启动VS,新建一个WP项目。 界面布局大致如下,你可以自由发挥。

准备好两张美图,图片内容你喜欢,但要键康哦,尺寸为173*173像素,.jpg或.png都行,一张作为磁贴的正面背景,另一张作为磁贴的背面背景。
注意:把图片的生成操作改为“内容”。

别走开,下页为您提供具体代码

#p#

当然,为了方便大家练习参考,我把XAML贴出来,希望大家不要直接复制,而是认认真真的在VS里面输一遍,要多写代码多练习才会找到感觉的哦。

  1. <phone:PhoneApplicationPage  
  2.     x:Class="ShellTitleApp.MainPage" 
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  11.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  12.     Foreground="{StaticResource PhoneForegroundBrush}" 
  13.     SupportedOrientations="Portrait" Orientation="Portrait" 
  14.     shell:SystemTray.IsVisible="True"
  15.      
  16.     <phone:PhoneApplicationPage.Resources> 
  17.         <Style x:Key="textblStyle" TargetType="TextBlock"
  18.             <Setter Property="FontSize" Value="28"/> 
  19.             <Setter Property="Margin" Value="0,12,5,6"/> 
  20.             <Setter Property="HorizontalAlignment" Value="Right"/> 
  21.         </Style> 
  22.         <Style x:Key="textboxStyle" TargetType="TextBox"
  23.             <Setter Property="FontSize" Value="28"/> 
  24.             <Setter Property="Width" Value="300"/> 
  25.             <Setter Property="Height" Value="auto"/> 
  26.             <Setter Property="HorizontalAlignment" Value="Left"/> 
  27.         </Style> 
  28.     </phone:PhoneApplicationPage.Resources> 
  29.  
  30.     <!--LayoutRoot 是包含所有页面内容的根网格--> 
  31.     <Grid x:Name="LayoutRoot" Background="Transparent"
  32.         <Grid.RowDefinitions> 
  33.             <RowDefinition Height="Auto"/> 
  34.             <RowDefinition Height="*"/> 
  35.         </Grid.RowDefinitions> 
  36.  
  37.         <!--TitlePanel 包含应用程序的名称和页标题--> 
  38.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"
  39.             <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/> 
  40.             <TextBlock x:Name="PageTitle" Text="示例程序" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  41.         </StackPanel> 
  42.  
  43.         <!--ContentPanel - 在此处放置其他内容--> 
  44.         <Grid x:Name="ContentPanel" Margin="0" Grid.Row="1"
  45.             <Grid.RowDefinitions> 
  46.                 <RowDefinition Height="*" /> 
  47.                 <RowDefinition Height="auto" /> 
  48.             </Grid.RowDefinitions> 
  49.             <ScrollViewer HorizontalScrollBarVisibility="Auto" Grid.Row="0"
  50.                 <Grid Margin="1"
  51.                     <Grid.RowDefinitions> 
  52.                         <RowDefinition Height="auto"/> 
  53.                         <RowDefinition Height="auto"/> 
  54.                         <RowDefinition Height="auto"/> 
  55.                         <RowDefinition Height="auto"/> 
  56.                         <RowDefinition Height="auto"/> 
  57.                     </Grid.RowDefinitions> 
  58.                     <Grid.ColumnDefinitions> 
  59.                         <ColumnDefinition Width="auto"/> 
  60.                         <ColumnDefinition Width="*"/> 
  61.                     </Grid.ColumnDefinitions> 
  62.                     <TextBlock Grid.Row="0" 
  63.                                Grid.Column="0" 
  64.                                Text="正面标题:" Style="{StaticResource textblStyle}" /> 
  65.                     <TextBox x:Name="txtForeTitle" 
  66.                              Style="{StaticResource textboxStyle}" 
  67.                              Grid.Row="0" 
  68.                              Grid.Column="1"/> 
  69.                     <TextBlock Text="计数器:" Style="{StaticResource textblStyle}" 
  70.                                Grid.Row="1" 
  71.                                Grid.Column="0"/> 
  72.                     <TextBox x:Name="txtCount" 
  73.                              Grid.Column="1" 
  74.                              Grid.Row="1" Style="{StaticResource textboxStyle}" > 
  75.                         <!--只允许输入数字--> 
  76.                         <TextBox.InputScope> 
  77.                             <InputScope > 
  78.                                 <InputScopeName NameValue="Number"/> 
  79.                             </InputScope> 
  80.                         </TextBox.InputScope> 
  81.                     </TextBox> 
  82.                     <TextBlock Text="背面标题:" 
  83.                                Grid.Row="2" 
  84.                                Grid.Column="0" Style="{StaticResource textblStyle}" /> 
  85.                     <TextBox x:Name="txtBackTitle" 
  86.                              Grid.Row="2" 
  87.                              Grid.Column="1" Style="{StaticResource textboxStyle}" /> 
  88.                     <TextBlock Text="背景内容:" 
  89.                                Grid.Row="3" 
  90.                                Grid.Column="0" Style="{StaticResource textblStyle}" /> 
  91.                     <TextBox x:Name="txtBackContent" 
  92.                              Grid.Row="3" 
  93.                              Grid.Column="1" Style="{StaticResource textboxStyle}" /> 
  94.                     <!--提示是以何种方式启动--> 
  95.                     <CheckBox x:Name="chkStartType" 
  96.                               IsChecked="False" 
  97.                               Grid.Row="4" 
  98.                               Grid.Column="0" 
  99.                               Grid.ColumnSpan="2" 
  100.                               FontSize="30" 
  101.                               Content="通过应用程序创建的磁贴启动" IsEnabled="False" /> 
  102.                 </Grid> 
  103.             </ScrollViewer> 
  104.             <StackPanel Grid.Row="1" Orientation="Horizontal"
  105.                 <Button x:Name="btnAddToShellTitle" 
  106.                         Content="添加磁贴" Click="btnAddToShellTitle_Click" /> 
  107.                 <Button x:Name="btnUpdateShellTitle" 
  108.                         Content="更新" Click="btnUpdateShellTitle_Click" /> 
  109.                 <Button x:Name="btnDeleteShellTitle" 
  110.                         Content="删除" Click="btnDeleteShellTitle_Click" /> 
  111.             </StackPanel> 
  112.         </Grid> 
  113.     </Grid> 
  114.  
  115. </phone:PhoneApplicationPage> 

好的,最后,当然是把C#代码也写完。

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using Microsoft.Phone.Controls; 
  13. using Microsoft.Phone.Shell; 
  14. using System.Windows.Threading; 
  15. namespace ShellTitleApp 
  16.     public partial class MainPage : PhoneApplicationPage 
  17.     { 
  18.         // 要用的图片的相对路径 
  19.         private const string FORE_PIC = "images/a.png"
  20.         private const string BACK_PIC = "images/b.png"
  21.         // 构造函数 
  22.         public MainPage() 
  23.         { 
  24.             InitializeComponent(); 
  25.         } 
  26.         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
  27.         { 
  28.             base.OnNavigatedTo(e); 
  29.             // 创建的磁贴的启动地址其实是指向 /MainPage.xaml?s=1 
  30.             // s=1参数是为了区别用户是不是通过程序所创建的磁贴来进入启动当前程序的, 
  31.             // 就像面前例子中讲到的把IE桌面快捷方式的参数指定为新浪微博的主页一个道理。 
  32.  
  33.             // 检测是否存在s=1来判断是否通过程序创建的磁贴来启动。 
  34.             ShellTile myTitle = ShellTile.ActiveTiles.FirstOrDefault(n => n.NavigationUri.ToString().Contains("s=1")); 
  35.             if (myTitle != null
  36.             { 
  37.                 this.chkStartType.IsChecked = true
  38.             } 
  39.             else 
  40.             { 
  41.                 this.chkStartType.IsChecked = false
  42.             } 
  43.         } 
  44.         // 创建图块 
  45.         private void btnAddToShellTitle_Click(object sender, RoutedEventArgs e) 
  46.         { 
  47.             // 不管我们的程序是否创建“开始”屏幕磁贴,ActiveTiles的第一个元素必是当前正在前台运行的 
  48.             // 应用程序。所以,在取出第一个ShellTile时,一定要通过判断是否存在s=1。 
  49.             // 这个s=1参数是随便取的。 
  50.             ShellTile myTitle = ShellTile.ActiveTiles.FirstOrDefault(m => m.NavigationUri.ToString().Contains("s=1")); 
  51.             // 如果已经创建就不建了。 
  52.             if (myTitle != null
  53.             { 
  54.                 MessageBox.Show("此应用程序的磁贴已经存在。"); 
  55.             } 
  56.             else 
  57.             { 
  58.                 // 创建新磁贴 
  59.                 int Counter = 0; 
  60.                 // StandardTileData就是用来传递ShellTitle的属性参数的, 
  61.                 // 如正面背景图的URI,标题,计数器等。 
  62.                 StandardTileData myData = new StandardTileData() 
  63.                 { 
  64.                     Title = string.IsNullOrEmpty(txtForeTitle.Text) == true ? string.Empty : txtForeTitle.Text, 
  65.                     Count = int.TryParse(txtCount.Text, out Counter) == true ? Counter : 0, 
  66.                     BackTitle = string.IsNullOrEmpty(txtBackTitle.Text) == true ? string.Empty : txtBackTitle.Text, 
  67.                     BackContent = string.IsNullOrEmpty(txtBackContent.Text) == true ? string.Empty : txtBackContent.Text, 
  68.                     BackgroundImage = new Uri(FORE_PIC, UriKind.Relative), 
  69.                     BackBackgroundImage = new Uri(BACK_PIC, UriKind.Relative) 
  70.                 }; 
  71.  
  72.                 // ShellTile.Create方法的第一个参数是我们启动应用程序时应该导航到哪里。 
  73.                 // 因为本示例主有一个页面,当然是导航到主页面, 
  74.                 // 因为是从我们创建的磁贴来启动的,所以不要忘了带上s=1参数。 
  75.                 ShellTile.Create(new Uri("/MainPage.xaml?s=1", UriKind.Relative), myData); 
  76.                 MessageBox.Show("磁贴图块创建成功。"); 
  77.             } 
  78.         } 
  79.         // 更新磁贴信息 
  80.         private void btnUpdateShellTitle_Click(object sender, RoutedEventArgs e) 
  81.         { 
  82.             // 同理,先要判断是否从我们创建的磁贴启动 
  83.             ShellTile myTitle = ShellTile.ActiveTiles.FirstOrDefault(m => m.NavigationUri.ToString().Contains("s=1")); 
  84.             if (myTitle != null
  85.             { 
  86.                 int Counter = 0; 
  87.                 StandardTileData data = new StandardTileData(); 
  88.                 if (!string.IsNullOrEmpty(txtForeTitle.Text)) 
  89.                 { 
  90.                     data.Title = txtForeTitle.Text; 
  91.                 } 
  92.                 if (int.TryParse(txtCount.Text,out Counter)) 
  93.                 { 
  94.                     data.Count = Counter; 
  95.                 } 
  96.                 if (!string.IsNullOrEmpty(txtBackTitle.Text)) 
  97.                 { 
  98.                     data.BackTitle = txtBackTitle.Text; 
  99.                 } 
  100.                 if (!string.IsNullOrEmpty(txtBackContent.Text)) 
  101.                 { 
  102.                     data.BackContent = txtBackContent.Text; 
  103.                 } 
  104.                 myTitle.Update(data); 
  105.                 MessageBox.Show("磁贴数据更新完成。"); 
  106.             } 
  107.         } 
  108.         // 删除磁贴 
  109.         // 注意:我们使用代码只可删除次要磁贴,也就是我们用代码创建的, 
  110.         // 不要去删除应用程序磁贴,即通过在应用程序项上长按创建的。 
  111.         private void btnDeleteShellTitle_Click(object sender, RoutedEventArgs e) 
  112.         { 
  113.             // 记着,要先判断是否找到通过我们代码创建的磁贴。 
  114.             ShellTile title = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("s=1")); 
  115.             if (title != null
  116.             { 
  117.                 title.Delete(); 
  118.                 MessageBox.Show("磁贴图块删除成功。"); 
  119.             } 
  120.         } 
  121.     } 

示例中我们重点是使用了ShellTile类,它并不复杂,成员不多,大家多玩几次就熟了。

运行之后,我们在“开始”屏幕中创建我们的磁贴,然后回到桌面,我们盯着它别动,你会发现,它的正面与反面会每隔几秒钟自动轮回切换。

责任编辑:闫佳明 来源: oschina
相关推荐

2022-12-07 08:13:55

CNI抽象接口

2013-02-19 09:04:32

Windows 8开发

2019-11-28 10:40:45

Kafka架构KafkaConsum

2023-05-03 09:09:28

Golang数组

2022-09-08 13:58:39

Spring高并发异步

2023-05-29 08:32:40

JAVA重写重载

2013-04-25 14:15:53

Windows PhoWindows PhoWindows Pho

2018-12-24 09:51:22

CPU天梯图Inter

2018-04-02 09:07:36

CIO

2020-03-27 18:00:37

微软Windows 10操作系统

2020-10-15 07:13:53

算法监控数据

2021-06-16 09:49:14

Windows 11微软动态磁贴

2010-04-12 17:32:59

Windows Pho

2013-04-17 11:10:02

Windows PhoWindows Pho

2020-09-25 19:53:39

数据

2020-04-26 09:59:00

黑客机器学习网络安全

2015-12-21 10:49:57

Windows 10开始菜单磁贴

2022-01-19 09:37:29

微软Windows11Windows

2013-07-30 12:37:56

Windows PhoWindows Pho

2010-04-21 17:07:54

Windows Pho
点赞
收藏

51CTO技术栈公众号