静态资源无法解析

3
我正在尝试在我的会计系统中实现MahApps样式,我按照这里所需的步骤进行操作,链接如下:MahApps
以下是我的代码,我遇到了一个错误,资源appbar_cupcake无法解析,以下是代码:
<Controls:MetroWindow x:Class="Hassab_Accounting_System.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>

</Grid>
<Controls:MetroWindow.RightWindowCommands>
<Controls:WindowCommands>
<Button Content="settings" />
<Button>
  <StackPanel Orientation="Horizontal">
    <Rectangle Width="20" Height="20"
               Fill="{Binding RelativeSource={RelativeSource AncestorType=Button},    Path=Foreground}">
      <Rectangle.OpacityMask>
        <VisualBrush Stretch="Fill"
                     Visual="{StaticResource appbar_cupcake}" />
      </Rectangle.OpacityMask>
    </Rectangle>
    <TextBlock Margin="4 0 0 0"
               VerticalAlignment="Center"
               Text="deploy cupcakes" />
  </StackPanel>
  </Button>
  </Controls:WindowCommands>
  </Controls:MetroWindow.RightWindowCommands>
  </Controls:MetroWindow>
            `
   ![here is the error ][2]

Exception


请确保您也安装了资源,网址为http://mahapps.com/guides/icons-and-resources.html。 - Nitin
你是否已经包含了页面中显示的ResourceDictionary? - Nitin
这个异常在传达什么信息? - Nitin
2
这个异常应该有一些相关的信息或内部异常。你能检查一下吗? - Nitin
1
请查看您的“异常”详细信息(最好包括内部“异常”),并将详细信息添加到您的问题中。 - Sheridan
显示剩余10条评论
1个回答

3
假设您尚未将资源文件包含在问题中,因为它没有被包含在问题中,请确保在您的文件顶部的ResourceDictionary中包含资源文件,例如icon.xaml。
类似于以下内容:
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resource/icon.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

或者添加到您的App.xaml中,如下所示:
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="/Resource/icon.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

实际上我尝试了你的方法,但是它给了我另一个错误,即无法访问或识别windows.resources... - Dexter90
你在写一个窗口吗?根元素是什么?如果是 <Page>,那么将 <Window.Resource> 替换为 <Page.Resource>。对于 <UserControl>、<CustomControl> 等也是同样的方式。 - FishySwede
实际上我正在使用Controls:MetroWindow/,我将它变成了 Controls:MetroWindow.Resources,但是它还是给我同样的错误。 - Dexter90
你能否在问题中发布你的整个XAML代码? - FishySwede
1
请注意,在最新的MahApps.Metro.Resources包中,路径为/Resources/Icons.xaml。 - mfanto
我使用的App.xaml语法是<ResourceDictionary Source="pack://application:,,,/Resources/Icons.xaml" /> - CAD bloke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接