编程创建 Windows Phone 的全景应用 (Panorama)

红薯 发布于 2012/06/28 08:11
阅读 2K+
收藏 13

在这个教程中我们将学习如何创建一个包含动态全景控件的 Windows Phone 7 应用程序。关于什么是全景控件请看本文最后的运行截图。

首先打开 Visual Studip 2010 并创建一个新的 Sliverlight for Windows Phone 7 的项目:

开始编码之前,我们通过添加引用选项来添加 Microsoft.Phone.Controls 的引用,并在 XAML 代码中包含命名空间,并删除 xaml 代码中的默认内容:

现在让我们开始编码。全景空间包含不同的标题和条目:

private List<string> CreatePanoramaItems(string item)
{
    List<String> Panoramaitems = null;
    switch (item)
    {
	case "Page1":
	    Panoramaitems = new List<string> { "Page1Item1", "Page1Item2", "Page1Item3"};
	    break;
	case "Page2":
	    Panoramaitems = new List<string> { "Page2Item1", "Page2Item2", "Page2Item3" };
	    break;
	case "Page3":
	    Panoramaitems = new List<string> { "Page3Item1", "Page3Item2", "Page3Item3" };
	    break;
    }
    return Panoramaitems;
}

private List<string> CreatePanoramaHeaders()
{
    return new List<string> { "Page1", "Page2", "Page3" };
}

接下来是添加装载事件,当页面加载时我们要装载动态的全景控件,并自定义标题和列表项:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
   //Initializing the Panorama Control and Assigning base values
   Panorama panoramactrl = new Panorama();
   panoramactrl.Title = "F5Debug How To";
   panoramactrl.SelectionChanged += panoramaCtrl_SelectionChanged;
  
   //Initializing the Panorama Control Items
   PanoramaItem panoramaCtrlItem = new PanoramaItem();
   panoramaCtrlItem.Header = "Dynamic Panorama";

   //Initializing Textblock to display some text
   TextBlock textBlock = new TextBlock();
   textBlock.TextWrapping = TextWrapping.Wrap;
   textBlock.Text = "F5debug.Net – Building and Debugging the Technology";
   textBlock.FontSize = 20;
   panoramaCtrlItem.Content = textBlock;

   panoramactrl.Items.Add(panoramaCtrlItem);

   foreach (string Eachitems in CreatePanoramaHeaders())
   {
       panoramaCtrlItem = new PanoramaItem();
       panoramaCtrlItem.Header = Eachitems;
       panoramactrl.Items.Add(panoramaCtrlItem);
   }

   this.LayoutRoot.Children.Add(panoramactrl);
}

private void panoramaCtrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   Panorama panoramactrl = (Panorama)sender;
   PanoramaItem panoramaItem = (PanoramaItem)(panoramactrl.SelectedItem);

   if (panoramaItem.Content == null)
   {
       ListBox listBox = new ListBox();
       listBox.ItemsSource = CreatePanoramaItems(panoramaItem.Header.ToString());
       panoramaItem.Content = listBox;
   }
} 

完整代码列表:

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;

namespace F5debugHowto43
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        private List<string> CreatePanoramaItems(string item)
        {
            List<String> Panoramaitems = null;
            switch (item)
            {
                case "Page1":
                    Panoramaitems = new List<string> { "Page1Item1", "Page1Item2", "Page1Item3"};
                    break;
                case "Page2":
                    Panoramaitems = new List<string> { "Page2Item1", "Page2Item2", "Page2Item3" };
                    break;
                case "Page3":
                    Panoramaitems = new List<string> { "Page3Item1", "Page3Item2", "Page3Item3" };
                    break;
            }
            return Panoramaitems;
        }

        private List<string> CreatePanoramaHeaders()
        {
            return new List<string> { "Page1", "Page2", "Page3" };
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //Initializing the Panorama Control and Assigning base values
            Panorama panoramactrl = new Panorama();
            panoramactrl.Title = "F5Debug How To";
            panoramactrl.SelectionChanged += panoramaCtrl_SelectionChanged;
           
            //Initializing the Panorama Control Items
            PanoramaItem panoramaCtrlItem = new PanoramaItem();
            panoramaCtrlItem.Header = "Dynamic Panorama";

            //Initializing Textblock to display some text
            TextBlock textBlock = new TextBlock();
            textBlock.TextWrapping = TextWrapping.Wrap;
            textBlock.Text = "F5debug.Net – Building and Debugging the Technology";
            textBlock.FontSize = 20;
            panoramaCtrlItem.Content = textBlock;

            panoramactrl.Items.Add(panoramaCtrlItem);

            foreach (string Eachitems in CreatePanoramaHeaders())
            {
                panoramaCtrlItem = new PanoramaItem();
                panoramaCtrlItem.Header = Eachitems;
                panoramactrl.Items.Add(panoramaCtrlItem);
            }

            this.LayoutRoot.Children.Add(panoramactrl);
        }

        private void panoramaCtrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Panorama panoramactrl = (Panorama)sender;
            PanoramaItem panoramaItem = (PanoramaItem)(panoramactrl.SelectedItem);

            if (panoramaItem.Content == null)
            {
                ListBox listBox = new ListBox();
                listBox.ItemsSource = CreatePanoramaItems(panoramaItem.Header.ToString());
                panoramaItem.Content = listBox;
            }
        }

    }
}

现在我们已经完成了所有的编码工作,按 F5 直接运行看看效果,如果编译成功的话会打开 Windows Phone 模拟器,然后你可以看到如下运行结果:

Output Screen:

在这个教程中,我们学习如何编程加载动态的全景控件以及自定义标题和列表项。

Happy Programming!!!

英文原文OSCHINA原创翻译

加载中
0
iiiiiiiii
iiiiiiiii
没有图,再好的戏也出不来。
红薯
红薯
回复 @文其 : 这里不是提问的地方
文其
文其
@红薯 同样的代码在java工程运行正常
文其
文其
@红薯 导包正常
文其
文其
@红薯 求指导
文其
文其
@红薯 使用javaemail写发送邮件,报"no class deffound error"javax.mail.session错
下一页
0
Beyond-Bit
Beyond-Bit
厉害!虽然不喜欢C#的编程风格!但是为了让自己写的app跑在自己的手机上!还是想试试!
0
回去干活
回去干活

windows神一样的开发环境,超越任何一款开发工具...

哎.可惜他就是windows系统,如果为linux而生,我会马上使用.

0
钛元素
钛元素
这么简单?
0
rick_liao
rick_liao

可惜我用的是xp和linux,要是xp能安装就好了

0
OSC首席过客
OSC首席过客
红署全能了!什么都会
0
寻找爱玩Tom
寻找爱玩Tom
红薯,貌似这个是在微软MSDN里面的视频中有说道过,我貌似看过哈,,,不过写的不错,一起学习
寻找爱玩Tom
寻找爱玩Tom
@红薯 要是红薯大哥,你能多翻译下就好了,O(∩_∩)O~
红薯
红薯
是的,翻译成了中文
0
寻找爱玩Tom
寻找爱玩Tom
呵呵,最近也在学习window Phone,是啊,英文很重要
返回顶部
顶部