UserControl中的WPF mahapps抛出异常无法找到资源'styles / colors.xaml'

时间:2014-07-18 20:02:39

标签: c# wpf xaml user-controls mahapps.metro

如果我在应用程序中使用示例代码但是我需要在UserControl中使用它,并且我将无法访问App.Xaml,因为它将进入某个elses框架,所以一切正常:

<UserControl x:Class="MyAssembly.Views.MyPanel"
         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="600" d:DesignWidth="300">
<UserControl.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.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.MergedDictionaries>
    </ResourceDictionary>

</UserControl.Resources>
<Grid>
    <ScrollViewer Height="90" Width="120" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <StackPanel>
            <TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor "/>
            <TextBlock Text="incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud "/>
            <TextBlock Text="exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure"/>
            <TextBlock Text="dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "/>
            <TextBlock Text="Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit"/>
            <TextBlock Text="Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit"/>

        </StackPanel>
    </ScrollViewer>
</Grid>
</UserControl>

上面代码抛出的异常(不是整个事情,但应该足够):

Set property 'System.Windows.ResourceDictionary.Source' threw an exception.
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,     IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)

******************************************
Cannot locate resource 'styles/colors.xaml'.
at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.get_ContentType()
at MS.Internal.WpfWebRequestHelper.GetContentType(WebResponse response)
at MS.Internal.WpfWebRequestHelper.GetResponseStream(WebRequest request, ContentType& contentType)
at System.Windows.ResourceDictionary.set_Source(Uri value)
at System.Windows.Baml2006.WpfSharedBamlSchemaContext.<Create_BamlProperty_ResourceDictionary_Source>b__1c4(Object target, Object value)
at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)

2 个答案:

答案 0 :(得分:0)

事实证明,框架的所有者正在使用旧版本的mahapps(0.999),其颜色拼写为颜色,因为它们首先被加载,我的崩溃。加载旧版本只适用于一次编译,我选择不再使用滚动条&#34;连续性&#34;

答案 1 :(得分:0)

尝试将您的xaml网址添加到ResourceDictionary:

 <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../styles/colors.xaml" />
                 (...)
  </ResourceDictionary.MergedDictionaries>