应用主题资源
主題概述
當(dāng)用戶在手機(jī)的“設(shè)置”菜單中啟用系統(tǒng)范圍的主題時,僅應(yīng)用程序中與主題相關(guān)的顏色會發(fā)生更改。字體和控件大小不會發(fā)生更改。例如,如果用戶在主題設(shè)置頁面上將強(qiáng)調(diào)色設(shè)置為紫色,則在應(yīng)用程序中,只有使用相應(yīng)資源畫筆配置的 UI 元素會更改為紫色。
| 如果在選擇其他主題時,您的應(yīng)用程序處于休眠狀態(tài),則在下次啟動主題之后,應(yīng)用程序才會適應(yīng)新主題。 |
下面的圖像顯示在本主題中創(chuàng)建的應(yīng)用程序如何適應(yīng)主題更改。
?
從左到右,此圖像顯示背景為“深”并且強(qiáng)調(diào)色為“紅”的應(yīng)用程序。在“設(shè)置”頁上,選擇“主題”以打開“主題”頁。在“主題”頁上,將主題更改為“淺”背景和“mango”強(qiáng)調(diào)色。當(dāng)再次啟動該應(yīng)用程序時,它會反映新的主題設(shè)置。
在此步驟中,創(chuàng)建該應(yīng)用程序,設(shè)置該應(yīng)用程序和頁面標(biāo)題,并添加一個矩形和兩個 texBlock 控件。每個控件都演示一種使用主題資源的不同技術(shù)。
準(zhǔn)備應(yīng)用程序
在 Visual Studio 中,通過選擇“文件” | “新建項(xiàng)目”菜單命令創(chuàng)建新的項(xiàng)目。
將顯示“新建項(xiàng)目”窗口。展開“Visual C#”模板,然后選擇“Windows Phone”模板。
選擇 Windows Phone 應(yīng)用 模板。用您選擇的名稱填寫“名稱”框。
單擊“確定”。將顯示 Windows Phone 平臺選擇對話框。
在“Windows Phone 目標(biāo)版本”菜單中,確保已選擇 Windows Phone OS 7.1。
單擊“確定”。將創(chuàng)建一個新的項(xiàng)目,并且“MainPage.xaml”將在 Visual Studio 設(shè)計器窗口中打開。
在 MainPage.xaml 中,使用以下代碼替換名為 LayoutRoot 的網(wǎng)格。
XAML 復(fù)制 <!--LayoutRoot is the root grid where all page content is placed--><Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="THEME RESOURCES" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="example" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> </Grid> </Grid>應(yīng)用主題資源
在 MainPage.xaml 上,將以下代碼名添加在名為 ContentPanel 的 Grid 中。
XAML 復(fù)制 <Rectangle Height="100" HorizontalAlignment="Left" Margin="12,33,0,0" Name="rectangle1" Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="1" VerticalAlignment="Top" Width="200" Fill="{StaticResource PhoneAccentBrush}" />此代碼會在該頁面的左上角部分創(chuàng)建一個矩形。注意 Fill 屬性以及它如何使用 StaticResource 標(biāo)記來指定名為 PhoneAccentBrush 的資源畫筆。此特殊畫筆允許矩形顏色響應(yīng)系統(tǒng)范圍的主題更改。例如,如果用戶在手機(jī)設(shè)置菜單中修改強(qiáng)調(diào)色,則矩形將更改為該顏色。
在 MainPage.xaml 上,將以下代碼添加在名為 ContentPanel 的 Grid 內(nèi)的矩形下。
XAML 復(fù)制 <TextBlock Height="45" HorizontalAlignment="Left" Margin="20,154,0,0" Name="textBlock1" Text="background =" VerticalAlignment="Top" Width="400" FontFamily="{StaticResource PhoneFontFamilySemiLight}" FontSize="{StaticResource PhoneFontSizeLarge}"/>此代碼會創(chuàng)建一個 TextBlock 控件(名為 textBlock1),該控件位于矩形下方。已將 StaticResource 標(biāo)記應(yīng)用于 FontFamily 和 FontSize 屬性。已將 TextBlock 文本設(shè)置為字體 PhoneFontFamilySemiLight (Segoe WP SemiLight) 和字體大小 PhoneFontSizeLarge (32)。有關(guān)資源定義的更多信息,請參見 Windows Phone 主題資源。
在 MainPage.xaml 上,將以下代碼添加在名為 ContentPanel 的 Grid 內(nèi)的 textBlock1 下。
C# 復(fù)制 <TextBlock Height="35" HorizontalAlignment="Left" Margin="21,205,0,0" Name="textBlock2" Text="accent color = " VerticalAlignment="Top" Width="400" Style="{StaticResource PhoneTextAccentStyle}"/>此代碼會創(chuàng)建第二個 TextBlock(名為 textBlock2),并應(yīng)用樣式資源 (PhoneTextAccentStyle)。此主題資源應(yīng)用字體 (PhoneFontFamilySemiBold)、字體大小 (PhoneFontSizeNormal) 和前景 (PhoneAccentBrush)。當(dāng)啟動系統(tǒng)范圍主題時,手機(jī)強(qiáng)調(diào)畫筆將導(dǎo)致 TextBlock 文本顏色發(fā)生更改。
主題背景和強(qiáng)調(diào)色可用作當(dāng)前應(yīng)用程序資源。在本節(jié)中,PhoneDarkThemeVisibility 和 PhoneAccentColor 資源分別用于確定主題背景和強(qiáng)調(diào)色。
| 通過訪問本節(jié)演示的當(dāng)前應(yīng)用程序?qū)ο?#xff0c;基于 XNA Framework 的 Windows Phone 應(yīng)用程序可以使用主題資源。有關(guān)可用主題資源的更多信息,請參見 Windows Phone 主題資源。 |
確定主題背景
-
在主頁的代碼隱藏文件 MainPage.xaml 中,在 MainPage 構(gòu)造函數(shù)的 InitializeComponent 調(diào)用下添加以下代碼。
C# VB 復(fù)制 // Determine the visibility of the dark background.Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];// Write the theme background value.if (darkBackgroundVisibility == Visibility.Visible){textBlock1.Text = "background = dark";}else { textBlock1.Text = "background = light"; }此代碼演示如何從應(yīng)用程序資源設(shè)置中確定當(dāng)前主題背景。它確定當(dāng)前主題使用的背景類型并將其寫入到 textBlock1 的 Text 屬性。
確定主題強(qiáng)調(diào)色
-
在 MainPage.xaml 中,在 MainPage 構(gòu)造函數(shù)中剛剛添加的代碼下添加以下代碼。
C# VB 復(fù)制 // Determine the accent color.Color currentAccentColorHex = (Color)Application.Current.Resources["PhoneAccentColor"];string currentAccentColor = "";switch (currentAccentColorHex.ToString()){case "#FF1BA1E2": currentAccentColor = "blue"; break; case "#FFA05000": currentAccentColor = "brown"; break; case "#FF339933": currentAccentColor = "green"; break; case "#FFE671B8": currentAccentColor = "pink"; break; case "#FFA200FF": currentAccentColor = "purple"; break; case "#FFE51400": currentAccentColor = "red"; break; case "#FF00ABA9": currentAccentColor = "teal (viridian)"; break; // Lime changed to #FFA2C139 in Windows Phone OS 7.1. case "#FF8CBF26": case "#FFA2C139": currentAccentColor = "lime"; break; // Magenta changed to # FFD80073 in Windows Phone OS 7.1. case "#FFFF0097": case "#FFD80073": currentAccentColor = "magenta"; break; // #FFF9609 (previously orange) is named mango in Windows Phone OS 7.1. case "#FFF09609": currentAccentColor = "mango (orange)"; break; // Mobile operator or hardware manufacturer color default: currentAccentColor = "custom eleventh color"; break; } // Write the current accent color. textBlock2.Text = "accent color = " + currentAccentColor;此代碼演示如何從應(yīng)用程序主題資源中確定強(qiáng)調(diào)色。它確定強(qiáng)調(diào)色值,然后使用 switch/case 語句向 textBlock2 的 Text 屬性分配友好名稱。
在本節(jié)中,應(yīng)用程序在主題背景和強(qiáng)調(diào)色更改之前和之后運(yùn)行。
測試應(yīng)用程序
通過選擇“調(diào)試 | 啟動調(diào)試”菜單命令運(yùn)行應(yīng)用程序。這將打開模擬器窗口并啟動該應(yīng)用程序。在屏幕上,應(yīng)會看到 Rectangle 和兩個 TextBlock 控件。Rectangle 的顏色應(yīng)該與當(dāng)前系統(tǒng)主題相匹配,兩個文本塊應(yīng)該應(yīng)用不同的字體和字體大小。此外,請注意第二個 TextBlock 的文本顏色與當(dāng)前系統(tǒng)主題相匹配,因?yàn)閷η熬皯?yīng)用了 PhoneAccentBrush。
更改主題背景和強(qiáng)調(diào)色。在“開始”屏幕中,滑動至“應(yīng)用程序”列表,然后點(diǎn)按“設(shè)置”。從“設(shè)置”屏幕中,點(diǎn)按“主題”,然后選擇其他背景和強(qiáng)調(diào)色。
再次啟動該應(yīng)用程序并查看它如何適應(yīng)新的主題。下面的圖像演示本節(jié)中執(zhí)行的步驟。
轉(zhuǎn)載于:https://www.cnblogs.com/zgqys1980/p/4022604.html
總結(jié)
- 上一篇: R语言学习笔记 (入门知识)
- 下一篇: 进口红酒为什么便宜吗?