WPF Application-Wide Resources

WPF Application-Wide Resources

Önceki yazımızda XMAL Static Resources konusunda değinip butonun arka planını değiştirmiştik. Şimdi bu staticresourcesları App.xaml dosyasında hazırlayacağız. Böylece modüler hale getirmiş olacağız. Bu örnekte foreground rengini StaticResource ile vereceğiz.

MainWindow.xmal dosyasındaki hesapmakinesi kodumuz:

<Window x:Class="WPFGiris.D3.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         xmlns:local="clr-namespace:WPFGiris.D3"         mc:Ignorable="d"         Title="Hesap Makinesi" Height="525" Width="350">               <Grid Margin="10">                  <Grid.ColumnDefinitions>             <ColumnDefinition Width="*"/>             <ColumnDefinition Width="*"/>             <ColumnDefinition Width="*"/>             <ColumnDefinition Width="*"/>         </Grid.ColumnDefinitions>         <Grid.RowDefinitions>             <RowDefinition Height="2*"/>             <RowDefinition Height="*"/>             <RowDefinition Height="*"/>             <RowDefinition Height="*"/>             <RowDefinition Height="*"/>             <RowDefinition Height="*"/>         </Grid.RowDefinitions>         <Label x:Name="resultLabel" Content="0" Grid.ColumnSpan="4" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="60"/>         <Button x:Name="acButton" Click="AcButton_Click" Content="AC" Grid.Row="1" Margin="5"/>         <Button x:Name="negativeButton" Click="NegativeButton_Click" Content="+/1" Grid.Row="1" Grid.Column="1" Margin="5"/>         <Button x:Name="percentageButton" Click="PercentageButton_Click" Content="%" Grid.Row="1" Grid.Column="2" Margin="5"/>         <Button x:Name="divisionButton" Click="OperationButton_Click" Content="/" Grid.Row="1" Grid.Column="3" Margin="5"                 Background="{StaticResource operatorsColor}" Foreground="{StaticResource foregroundColor}"/>         <Button x:Name="sevenButton" Click="NumberButton_Click" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}" Content="7" Grid.Row="2" Grid.Column="0" Margin="5"/>         <Button x:Name="eightButton" Click="NumberButton_Click" Content="8" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.Row="2" Grid.Column="1" Margin="5"/>         <Button x:Name="nineButton" Click="NumberButton_Click" Content="9" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.Row="2" Grid.Column="2" Margin="5"/>         <Button x:Name="multiplicationButton" Click="OperationButton_Click"  Background="{StaticResource operatorsColor}" Foreground="{StaticResource foregroundColor}" Content="*" Grid.Row="2" Grid.Column="3" Margin="5"/>         <Button x:Name="fourButton" Click="NumberButton_Click" Content="4" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.Row="3" Grid.Column="0" Margin="5"/>         <Button x:Name="fiveButton" Click="NumberButton_Click" Content="5" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.Row="3" Grid.Column="1" Margin="5"/>         <Button x:Name="sixButton" Click="NumberButton_Click" Content="6" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.Row="3" Grid.Column="2" Margin="5"/>         <Button x:Name="substractionButton" Click="OperationButton_Click" Background="{StaticResource operatorsColor}" Foreground="{StaticResource foregroundColor}" Content="-" Grid.Row="3" Grid.Column="3" Margin="5"/>         <Button x:Name="oneButton" Click="NumberButton_Click" Content="1" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"   Grid.Row="4" Grid.Column="0" Margin="5"/>         <Button x:Name="twoButton" Click="NumberButton_Click"  Content="2" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.Row="4" Grid.Column="1" Margin="5"/>         <Button x:Name="threeButton" Click="NumberButton_Click"  Content="3" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.Row="4" Grid.Column="2" Margin="5"/>         <Button x:Name="plusButton" Click="OperationButton_Click" Background="{StaticResource operatorsColor}" Foreground="White" Content="+" Grid.Row="4" Grid.Column="3" Margin="5"/>         <Button x:Name="zeroButton" Click="NumberButton_Click" Content="0" Grid.Row="5" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.ColumnSpan="2" Margin="5"/>         <Button x:Name="dotButton" Click="DotButton_Click"  Content="." Grid.Row="5" Background="{StaticResource numbersColor}" Foreground="{StaticResource foregroundColor}"  Grid.Column="2" Margin="5"/>         <Button x:Name="equalButton" Click="EqualButton_Click" Background="{StaticResource operatorsColor}" Foreground="{StaticResource foregroundColor}" Content="=" Grid.Row="5" Grid.Column="3" Margin="5" Cursor="Hand"/>     </Grid> </Window> 

Önceki yazımızla kıyaslarsanız sayfa üstünde bulunan bilginin altına:

  1. <Window.Resources>
  2. <SolidColorBrush x:Key=“numbersColor” Color=“#000000”/>
  3. <SolidColorBrush x:Key=“operatorsColor” Color=“Green” />
  4. </Window.Resources>

Kodlarını girip x:Key değerini de Background=”{StaticResource numbersColor}” kısmında belirtip butonları aynı anda renklendirip, renklerini değiştirebiliyorduk.

Şimdi bu değerleri App.xmal içine yazıp belirttiğim kodu MainWindow.xmal içinden sileceğim böylece bu StaticResource tanımlarına tüm xmal dosyalarımdan erişebileceğim.

<Application x:Class="WPFGiris.D3.App"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:local="clr-namespace:WPFGiris.D3"              StartupUri="MainWindow.xaml">     <Application.Resources>         <SolidColorBrush x:Key="numbersColor" Color="#000000"/>         <SolidColorBrush x:Key="operatorsColor" Color="Green" />         <SolidColorBrush x:Key="foregroundColor" Color="White"/>     </Application.Resources> </Application> 

Bu konuyla ilgili önceki yazım: http://kodlasana.com/programlama/c-sharp/wpf/wpf-xmal-static-resources-kullanimi.html


Yayımlandı

kategorisi

yazarı:

Etiketler:

Yorumlar

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir