Mahapps,图标不起作用

时间:2015-11-05 14:38:26

标签: c# wpf xaml mahapps.metro

与GitHub一起发布此消息可能是来自这里的人遇到了同样的问题。我今天尝试了各种选项,但无法使其正常工作。

我已经从nugget安装了MahApps.Metro。之后我也安装了MahApps.Metro.Resources。这在我的项目中创建了一个名为resources的文件夹。我在窗口资源中使用icons.xaml文件。我在usercontrol的按钮中使用了一些图标(嵌入在内容控件中的主窗口中)。但是当我运行时,图标不会出现。只有灰色圆圈可见。

我打开了icons.xaml,看到Fill = {DynamicResource BlackBrush}错误输出的Fill属性。如果我将填充更改为Fill =" Black"然后我可以看到图标。但是如果Accent是Dark,它会产生一个问题,因为图标颜色不会动态变化。

根据GitHub中的反馈,我尝试创建一个小型WPF示例。我添加了金块Mahapps.metro和Mahapps.metro.resources我创建了一个窗口,用资源xaml修复了所有MahApps.Metro xaml。然后我在项目中添加了一个具有相同响应的用户控件。在用户控件中我添加了一个按钮,我在窗口中也添加了一个按钮。当我运行示例时,我没有看到任何图标(在窗口中直接以及在用户控件内)

当我与演示应用程序进行比较时,除了在demo中你有一个单独的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>

我在这里遗漏了什么吗? •吉里贾

1 个答案:

答案 0 :(得分:1)

您有两种方法可以解决您的问题。

第一个选项是使用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>