C# UserControl抛出异常中的WPF mahapps找不到资源“style/colors.xaml”
如果我在应用程序中使用示例代码,一切正常,但我需要在UserControl中使用它,我将无法访问App.Xaml,因为它将进入其他人的框架:C# UserControl抛出异常中的WPF mahapps找不到资源“style/colors.xaml”,c#,wpf,xaml,user-controls,mahapps.metro,C#,Wpf,Xaml,User Controls,Mahapps.metro,如果我在应用程序中使用示例代码,一切正常,但我需要在UserControl中使用它,我将无法访问App.Xaml,因为它将进入其他人的框架: <UserControl x:Class="MyAssembly.Views.MyPanel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/
<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)
由于框架的所有者使用的是旧版本的mahapps 0.999,它的颜色拼写为颜色,因为他们的颜色是先加载的,所以我的框架崩溃了。加载旧版本只对一次编译有效,我选择不使用它只是为了滚动条的连续性尝试将您的xaml url添加到ResourceDictionary: ...