如果你有Silverlight和C#开发经验,在Windows Phone 7上进行UI设计时,Windows Phone 7应用程序开发平台“Silverlight for Windows Phone”是Silverlight程序员必备的工具,熟悉Silverlight for Windows Phone之后同样还需要熟悉App.xaml和它的代码隐藏文件App.xaml.cs,C#代码增加了一些与手机相关的初始化代码,App类使用公共RootFrame属性对根框架提供了简单的访问。
- public PhoneApplicationFrame RootFrame { get; private set; }
PhoneApplicationFrame指的是MicrosoftPhone.Controls.PhoneApplicationFrame,App.xaml.cs使用Microsoft.Phone.Controls和Microsoft.Phone.Shell命名空间,App类构造函数调用IntializePhoneApplication方法,它增加了电话相关的代码以显示启动画面,下面的代码片段显示了经典的Silverlight初始化和新的与手机相关的方法:
- public App()
- {
- // Global handler for uncaught exceptions.
- // Note that exceptions thrown by ApplicationBarItem.Click
- // will not get caught here.
- UnhandledException += Application_UnhandledException;
- // Standard Silverlight initialization
- InitializeComponent();
- // Phone-specific initialization
- InitializePhoneApplication();
- }
下面的代码显示了InitializePhoneApplication和CompleteInitializePhoneApplication方法,InitializePhoneApplication方法创建新的框架,并在其中显示启动画面,直到应用程序启动完毕,CompleteInitializePhoneApplication方法将新框架设置为RootVisual,因为它要连接RootFrame.Navigated的事件处理程序,注意,不要修改这些方法:
- private bool phoneApplicationInitialized = false;
- // Do not add any additional code to this method
- private void InitializePhoneApplication()
- {
- if (phoneApplicationInitialized)
- return;
- // Create the frame but don't set it as RootVisual yet;
- // this allows the splash screen to remain active until the
- // application is ready to render.
- RootFrame = new PhoneApplicationFrame();
- RootFrame.Navigated += CompleteInitializePhoneApplication;
- // Handle navigation failures
- RootFrame.NavigationFailed += RootFrame_NavigationFailed;
- // Ensure we don't initialize again
- phoneApplicationInitialized = true;
- }
- // Do not add any additional code to this method
- private void CompleteInitializePhoneApplication(object sender,
- NavigationEventArgs e)
- {
- // Set the root visual to allow the application to render
- if (RootVisual != RootFrame)
- RootVisual = RootFrame;
- // Remove this handler since it is no longer needed
- RootFrame.Navigated -= CompleteInitializePhoneApplication;
- }
当应用程序加载时,模拟器和设备要显示启动画面,启动画面是一个包含在项目中的24位色深,800x480像素的位图文件 - SplashScreenImage.jpg,你可以用自己的jpeg文件替换它,但注意不能修改名字,只能是SplashScreenImage.jpg,图1和图2分别显示了默认和自定义启动画面。
图 1 默认的位图文件SplashScreenImage.jpg
图 2 自定义位图文件SplashScreenImage.jpg
启动画面必须是24位色深,分辨率为800x480像素的jpeg格式文件,如果使用PNG文件代替,将不会显示启动画面,设计自定义启动画面时要注意,用户是可以旋转设备的。