2015-11-05 55 views
1

與GitHub一起發佈,可能會有人從這裏面臨同樣的問題。我今天嘗試了各種選擇,但無法實現這個工作。Mahapps,圖標不能正常工作

我已經從金塊安裝了MahApps.Metro。之後,我也安裝了MahApps.Metro.Resources。這在我的項目中創建了一個名爲resources的文件夾。我在我的窗口資源中使用了icons.xaml文件。我在我的用戶控件的按鈕中使用了一些圖標(嵌入在內容控件的主窗口中)。但是當我運行的圖標不會出現。只有灰色的圓圈可見。

我打開了icons.xaml,看到Fill = {DynamicResource BlackBrush}出錯的Fill屬性。如果我將填充更改爲填充=「黑色」,則可以看到圖標。但是,如果重音是黑暗的,它會產生一個問題,因爲圖標顏色不會動態變化。

基於GitHub的反饋,我嘗試創建一個小的WPF示例。我添加了塊金Mahapps.metro和Mahapps.metro.resources,我創建了一個窗口,用資源xaml引用了所有MahApps.Metro xaml。然後我在項目中添加了一個用戶控件,並有相同的響應。在用戶控件中,我添加了一個按鈕,並在窗口中添加了一個按鈕。當我運行示例時,我看不到任何圖標(在窗口直接以及用戶控制內)

當我與演示應用程序進行比較時,除了在演示中有一個事實單獨的類庫爲icons.xaml。

以下是我的代碼。

主窗口:

<Window x:Class="TestApp.Demo.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" 
     xmlns:local="clr-namespace:TestApp.Demo" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.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="pack://application:,,,/TestApp.Demo;component/Resources/Icons.xaml" />--> 
       <ResourceDictionary Source="/Resources/Icons.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 

     </ResourceDictionary> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Button Grid.Row="0" Width="50" 
Height="50" 
Style="{DynamicResource MetroCircleButtonStyle}"> 
      <Rectangle Width="20" 
Height="20" 
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"> 
       <Rectangle.OpacityMask> 
        <VisualBrush Stretch="Fill" 
        Visual="{DynamicResource appbar_city}" /> 
       </Rectangle.OpacityMask> 
      </Rectangle> 
     </Button> 
     <local:UCTest Grid.Row="1"/> 
    </Grid> 
</Window> 

下面是用戶控件代碼:

<UserControl x:Class="TestApp.Demo.UCTest" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 
     <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries> 
         <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
         <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
         <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.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="pack://application:,,,/TestApp.Demo;component/Resources/Icons.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
       </ResourceDictionary> 
    </UserControl.Resources> 
    <Grid> 
     <Grid> 
      <Button Width="50" 
Height="50" 
Style="{DynamicResource MetroCircleButtonStyle}"> 
       <Rectangle Width="20" 
Height="20" 
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"> 
        <Rectangle.OpacityMask> 
         <VisualBrush Stretch="Fill" 
        Visual="{DynamicResource appbar_city}" /> 
        </Rectangle.OpacityMask> 
       </Rectangle> 
      </Button> 

     </Grid> 
    </Grid> 
</UserControl> 

我失去了一些東西在這裏? •Girija

+0

Resources.xaml的路徑是什麼? –

+0

當我安裝了金塊時,它在我的項目中創建了一個文件夾「Resources」,並在其中創建了Icons.xaml文件。我嘗試了所有這些可能的方法,但都沒有成功:1. 2. Shankar

回答

1

您有2個選項來解決您的問題。

第一種選擇是使用StaticResource代替DynamicResource

<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_city}" /> 

第二個選項是把你的資源,以App.xaml,所以你仍然可以使用DynamicResource

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.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> 
</Application.Resources> 
+0

Works感謝....我使用靜態資源的方法,因爲我加載我的WPF窗口另一個應用程序(託管),我沒有app.xaml – Shankar