编程创建 Windows Phone 的全景应用 (Panorama)
在这个教程中我们将学习如何创建一个包含动态全景控件的 Windows Phone 7 应用程序。关于什么是全景控件请看本文最后的运行截图。
首先打开 Visual Studip 2010 并创建一个新的 Sliverlight for Windows Phone 7 的项目:
开始编码之前,我们通过添加引用选项来添加 Microsoft.Phone.Controls 的引用,并在 XAML 代码中包含命名空间,并删除 xaml 代码中的默认内容:
现在让我们开始编码。全景空间包含不同的标题和条目:
01 |
private List< string > CreatePanoramaItems( string item) |
03 |
List<String> Panoramaitems = null ; |
07 |
Panoramaitems = new List< string > { "Page1Item1" , "Page1Item2" , "Page1Item3" }; |
10 |
Panoramaitems = new List< string > { "Page2Item1" , "Page2Item2" , "Page2Item3" }; |
13 |
Panoramaitems = new List< string > { "Page3Item1" , "Page3Item2" , "Page3Item3" }; |
19 |
private List< string > CreatePanoramaHeaders() |
21 |
return new List< string > { "Page1" , "Page2" , "Page3" }; |
接下来是添加装载事件,当页面加载时我们要装载动态的全景控件,并自定义标题和列表项:
01 |
private void MainPage_Loaded( object sender, RoutedEventArgs e) |
04 |
Panorama panoramactrl = new Panorama(); |
05 |
panoramactrl.Title = "F5Debug How To" ; |
06 |
panoramactrl.SelectionChanged += panoramaCtrl_SelectionChanged; |
09 |
PanoramaItem panoramaCtrlItem = new PanoramaItem(); |
10 |
panoramaCtrlItem.Header = "Dynamic Panorama" ; |
13 |
TextBlock textBlock = new TextBlock(); |
14 |
textBlock.TextWrapping = TextWrapping.Wrap; |
15 |
textBlock.Text = "F5debug.Net – Building and Debugging the Technology" ; |
16 |
textBlock.FontSize = 20; |
17 |
panoramaCtrlItem.Content = textBlock; |
19 |
panoramactrl.Items.Add(panoramaCtrlItem); |
21 |
foreach ( string Eachitems in CreatePanoramaHeaders()) |
23 |
panoramaCtrlItem = new PanoramaItem(); |
24 |
panoramaCtrlItem.Header = Eachitems; |
25 |
panoramactrl.Items.Add(panoramaCtrlItem); |
28 |
this .LayoutRoot.Children.Add(panoramactrl); |
31 |
private void panoramaCtrl_SelectionChanged( object sender, SelectionChangedEventArgs e) |
33 |
Panorama panoramactrl = (Panorama)sender; |
34 |
PanoramaItem panoramaItem = (PanoramaItem)(panoramactrl.SelectedItem); |
36 |
if (panoramaItem.Content == null ) |
38 |
ListBox listBox = new ListBox(); |
39 |
listBox.ItemsSource = CreatePanoramaItems(panoramaItem.Header.ToString()); |
40 |
panoramaItem.Content = listBox; |
完整代码列表:
02 |
using System.Collections.Generic; |
06 |
using System.Windows.Controls; |
07 |
using System.Windows.Documents; |
08 |
using System.Windows.Input; |
09 |
using System.Windows.Media; |
10 |
using System.Windows.Media.Animation; |
11 |
using System.Windows.Shapes; |
12 |
using Microsoft.Phone.Controls; |
14 |
namespace F5debugHowto43 |
16 |
public partial class MainPage : PhoneApplicationPage |
21 |
InitializeComponent(); |
22 |
this .Loaded += new RoutedEventHandler(MainPage_Loaded); |
25 |
private List< string > CreatePanoramaItems( string item) |
27 |
List<String> Panoramaitems = null ; |
31 |
Panoramaitems = new List< string > { "Page1Item1" , "Page1Item2" , "Page1Item3" }; |
34 |
Panoramaitems = new List< string > { "Page2Item1" , "Page2Item2" , "Page2Item3" }; |
37 |
Panoramaitems = new List< string > { "Page3Item1" , "Page3Item2" , "Page3Item3" }; |
43 |
private List< string > CreatePanoramaHeaders() |
45 |
return new List< string > { "Page1" , "Page2" , "Page3" }; |
48 |
private void MainPage_Loaded( object sender, RoutedEventArgs e) |
51 |
Panorama panoramactrl = new Panorama(); |
52 |
panoramactrl.Title = "F5Debug How To" ; |
53 |
panoramactrl.SelectionChanged += panoramaCtrl_SelectionChanged; |
56 |
PanoramaItem panoramaCtrlItem = new PanoramaItem(); |
57 |
panoramaCtrlItem.Header = "Dynamic Panorama" ; |
60 |
TextBlock textBlock = new TextBlock(); |
61 |
textBlock.TextWrapping = TextWrapping.Wrap; |
62 |
textBlock.Text = "F5debug.Net – Building and Debugging the Technology" ; |
63 |
textBlock.FontSize = 20; |
64 |
panoramaCtrlItem.Content = textBlock; |
66 |
panoramactrl.Items.Add(panoramaCtrlItem); |
68 |
foreach ( string Eachitems in CreatePanoramaHeaders()) |
70 |
panoramaCtrlItem = new PanoramaItem(); |
71 |
panoramaCtrlItem.Header = Eachitems; |
72 |
panoramactrl.Items.Add(panoramaCtrlItem); |
75 |
this .LayoutRoot.Children.Add(panoramactrl); |
78 |
private void panoramaCtrl_SelectionChanged( object sender, SelectionChangedEventArgs e) |
80 |
Panorama panoramactrl = (Panorama)sender; |
81 |
PanoramaItem panoramaItem = (PanoramaItem)(panoramactrl.SelectedItem); |
83 |
if (panoramaItem.Content == null ) |
85 |
ListBox listBox = new ListBox(); |
86 |
listBox.ItemsSource = CreatePanoramaItems(panoramaItem.Header.ToString()); |
87 |
panoramaItem.Content = listBox; |
现在我们已经完成了所有的编码工作,按 F5 直接运行看看效果,如果编译成功的话会打开 Windows Phone 模拟器,然后你可以看到如下运行结果:
Output Screen:
在这个教程中,我们学习如何编程加载动态的全景控件以及自定义标题和列表项。
Happy Programming!!!
评论暂时关闭