2013-07-07 136 views
2

我想在使用MahApps.Metro的WPF應用程序中設置窗口圖標。我不知道如何將我在窗口的XAML後定義的靜態資源設置爲窗口的圖標。使用靜態資源設置窗口圖標

下面是XAML,我要的資源是Icons.xaml

<controls:MetroWindow x:Class="Test1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
    Title="MainWindow" Height="350" Width="525" WindowState="Maximized"> 

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" /> 

      <ResourceDictionary Source="/Resources/Icons.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

下面是我如何使用上資源窗口的例子:

<Rectangle> 
    <Rectangle.Fill> 
     <VisualBrush Visual="{StaticResource appbar_alien}" /> 
    </Rectangle.Fill> 
</Rectangle> 

我如何設置appbar_alien作爲窗口的圖標?

+0

Xaml中的引用必須已經存在於對象圖中。據稱。你能不能(1)將RD推廣到app.xaml?或者(2)將RD移動到僅資源組件並從那裏拉出來? –

回答

5

我剛剛回答了有關MahApps MetroWindow(Application icon stretches to title bar height when using MahApps.Metro)中太大圖標的問題,同樣的技術可以爲您提供解決方案。

基本上,你必須設置MetroWindow.IconTemplate屬性:

<MahApps:MetroWindow.IconTemplate> 
    <DataTemplate> 
     <Grid Width="{TemplateBinding Width}" 
       Height="{TemplateBinding Height}" 
       Margin="4" 
       Background="Transparent" 
       RenderOptions.EdgeMode="Aliased" 
       RenderOptions.BitmapScalingMode="HighQuality"> 
       <Rectangle> 
        <Rectangle.Fill> 
         <VisualBrush Visual="{StaticResource appbar_alien}" /> 
        </Rectangle.Fill> 
       </Rectangle> 
     </Grid> 
    </DataTemplate> 
</MahApps:MetroWindow.IconTemplate> 

根據您的 「/Resources/Icons.xaml」 的,你可能有伸展/縮放。