UWP 設置控件樣式四種方法

1.隱式方法,經過僅指定 Style 的 TargetType。(設置所有的Button樣式)shell

1 <Page.Resources >
2         <Style TargetType="Button">
3             <Setter Property="BorderBrush" Value="Lime"/>
4             <Setter Property="BorderThickness" Value="4"/>
5         </Style>
6     </Page.Resources>

 

2.顯式方法,經過指定 Style 的 TargetType 和 x:Key 特性這一特性,而後經過使用顯式鍵的 {StaticResource} 標記擴展引用設置目標控件的 Style 屬性spa

1 <Page.Resources >
2         <Style x:Key="btnStyle" TargetType="Button">
3             <Setter Property="BorderBrush" Value="Lime"/>
4             <Setter Property="BorderThickness" Value="4"/>
5         </Style>
6  </Page.Resources>
7 
8 //調用
9 <Button Content="跳轉方法" x:Name="btnTest" Style="{StaticResource btnStyle}"/>

3.單個樣式表示code

 1  //1.App.xaml配置文件中
 2 <Application.Resources>
 3      <SolidColorBrush x:Key="BlueBrush" Color="#FF1C90D1"/>
 4 </Application.Resources>
 5 
 6 //2.頁面中綁定值MainPage.xaml
 7 <Rectangle Height="2" Width="18" Fill="{StaticResource EggshellBrush}"/>
 8 
 9 //3.獲取值MainPage.xaml.cs
10 App.Current.Resources["EggshellBrush"] as SolidColorBrush

4.使用樣式文件進行調整樣式orm

1) 建立文件夾Themes右鍵添加新建項visual C# àxamlà資源字典 style.xamlblog

2) 在style.xaml寫樣式例如資源

 

1 <Style TargetType="Button" x:Key="gft_FormBtm">
2         <Setter Property="Background" Value="OrangeRed"></Setter>
3         <Setter Property="Height" Value="50"></Setter>
4         <Setter Property="FontSize" Value="16"></Setter>
5         <Setter Property="Foreground" Value="White"></Setter>
6         <Setter Property="HorizontalAlignment" Value="Center"></Setter>
7         <Setter Property="MinWidth" Value="300"></Setter>
8  </Style>

 

3) 在App.xaml文件中指定資源get

1 <!--4.使用樣式文件-->
2     <Application.Resources>
3         <ResourceDictionary>
4             <ResourceDictionary.MergedDictionaries>
5                 <ResourceDictionary Source="Themes/style.xaml"></ResourceDictionary>
6             </ResourceDictionary.MergedDictionaries>
7         </ResourceDictionary>
8 </Application.Resources>

4) 在xaml界面中使用樣式文件it

 

1 <Button x:Name="btnSubmit"  Content="贊成以上協議並註冊" HorizontalAlignment="Center" Click="btnSubmit_Click" Style="{StaticResource gft_FormBtm}" />
相關文章
相關標籤/搜索