Windows Phone 7 UI設(shè)計(jì):代碼隱藏文件和啟動畫面
原創(chuàng)【51CTO譯文】如果你有Silverlight和C#開發(fā)經(jīng)驗(yàn),在Windows Phone 7上進(jìn)行UI設(shè)計(jì)時,Windows Phone 7應(yīng)用程序開發(fā)平臺“Silverlight for Windows Phone”是Silverlight程序員必備的工具,熟悉Silverlight for Windows Phone之后同樣還需要熟悉App.xaml和它的代碼隱藏文件App.xaml.cs,C#代碼增加了一些與手機(jī)相關(guān)的初始化代碼,App類使用公共RootFrame屬性對根框架提供了簡單的訪問。
- public PhoneApplicationFrame RootFrame { get; private set; }
PhoneApplicationFrame指的是MicrosoftPhone.Controls.PhoneApplicationFrame,App.xaml.cs使用Microsoft.Phone.Controls和Microsoft.Phone.Shell命名空間,App類構(gòu)造函數(shù)調(diào)用IntializePhoneApplication方法,它增加了電話相關(guān)的代碼以顯示啟動畫面,下面的代碼片段顯示了經(jīng)典的Silverlight初始化和新的與手機(jī)相關(guān)的方法:
- 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方法創(chuàng)建新的框架,并在其中顯示啟動畫面,直到應(yīng)用程序啟動完畢,CompleteInitializePhoneApplication方法將新框架設(shè)置為RootVisual,因?yàn)樗B接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;
- }
當(dāng)應(yīng)用程序加載時,模擬器和設(shè)備要顯示啟動畫面,啟動畫面是一個包含在項(xiàng)目中的24位色深,800x480像素的位圖文件 - SplashScreenImage.jpg,你可以用自己的jpeg文件替換它,但注意不能修改名字,只能是SplashScreenImage.jpg,圖1和圖2分別顯示了默認(rèn)和自定義啟動畫面。
圖 1 默認(rèn)的位圖文件SplashScreenImage.jpg
圖 2 自定義位圖文件SplashScreenImage.jpg
啟動畫面必須是24位色深,分辨率為800x480像素的jpeg格式文件,如果使用PNG文件代替,將不會顯示啟動畫面,設(shè)計(jì)自定義啟動畫面時要注意,用戶是可以旋轉(zhuǎn)設(shè)備的。
原文出處:http://www.drdobbs.com/windows/227701092;jsessionid=0LPPSGFA3UDNBQE1GHPSKH4ATMY32JVN
原文名:Developing a Silverlight UI for Windows Phone 7
作者:Gaston Hillar
【編輯推薦】