顯示具有 Mango 標籤的文章。 顯示所有文章
顯示具有 Mango 標籤的文章。 顯示所有文章

2012年1月30日 星期一

SETTING VISIBILITY BASED ON WP7 THEMES


INTRO

The Technical Certification Requirements for Windows Phone 7 applications state the following:
5.5.2 – Content and Themes
Application content, such as text and visual elements, must be visible and legible regardless of the phone theme. For example, if the phone theme changes from black background to white background, the text and visual elements of your application must be visible or legible.
This means that everything in you application, including images, should be well visible in the dark and light theming of the phone. Handling dark/light support is very easy.

DARK AND LIGHT

Often companies have a special version of their logos for different situations (Bing for example). For this demo I’ve created two logos, a black and a white one. The white logo should be used when the theme is set to dark, the other when a light theme has been selected.
Logo-WhiteLogo-Black
I’ve dropped these images into Expression Blend and grouped them together in a Grid control (Ctrl+G ). This grid is placed in the title panel, which resulted in the following XAML:
<StackPanel x:Name="TitlePanel" Grid.Row="0">
    <Grid>
        <Image Source="Logo-Black.png" />
        <Image Source="Logo-White.png" />
    </Grid>
</StackPanel> 


To get the white logo to be only visible when the dark theme is set, select the image of the white logo. With the image selected click on the “Advanced options”-peg:
image
Now, go to “System Resource” and select the “PhoneDarkThemeVisibility” resource.
image
At this point a green rectangle is placed around the visibility property of the images letting you know a resource is set on that property.
To get the same results for the dark logo, repeat the process on that images. But select the “PhoneLightThemeVisibility” instead.

TESTING

To test the results right in Expression Blend, got to the “Device” tab. On this tab you can set different device settings, including the Accent Color and the Light or Dark themes.
image
Switching from dark to light and back should result in the images below.
image

WRAP-UP

Setting the visibility like this can be done on every element in your XAML. And whenever you need to use the visibility for other purposes, there’s a similar resource for the Opacity.

2011年10月27日 星期四


不得不說,WP7開發的資料真的是太少了,國內有句話叫「天下文章一大抄」,查Application.GetResourceStream的用法,找遍了整個網絡,無非就那一兩篇,而且寫得還不完整,包括微軟官方的例子。在花了近半天的時間後,終於解決問題。
我們可以預先把程序中用到的資源,如圖片,音樂等放入項目中,打包進XAP文檔,需要的時候再從中取用。下面就說說具體實現方法。

第一步,把資料存進項目。
1、右鍵點擊項目名稱-新增-新建資料夾,這裡文件夾名以 Images 為例,把需要的圖片拖進來,當然你也可以不建,直接把圖片拖到根目錄底下。
2、選中剛剛拖進去的圖片,看右下角的屬性標籤,如果沒有,右鍵點圖片,選屬性,確認圖片屬性中的建置動作為 Resource。

第二步,程式呼叫。
1
System.IO.Stream src = Application.GetResourceStream(new Uri("/PhoneAppX;component/Images/abc.png", UriKind.Relative)).Stream;
關鍵在 Uri 的格式,PhoneAppX 是項目名稱,component 是固定路徑,Images/abc.jpg 才是圖片資源相對路徑,得到的是 Stream,在微軟官方的例子中,使用下面的方法來轉換成圖片:
1
2
3
4
            BitmapImage bi = new BitmapImage();
            bi.SetSource(src);
            Image img = new Image();
            img.Source = bi;
網路上的文章基本上都沒寫第一步,常常就卡在這裡,新增圖片還好,但是屬性的建置動作沒設好或改成其他屬性,導致程式在 Run 的時候常常丟出 Exception!


Reference To: http://www.pocketdigi.com/20110925/498.html