c# - 加载资源字典时发生错误

标签 c# wpf telerik

我看过并尝试过这些 other solutions我发现,甚至复制了 Telerik's Documentation .但是,我仍然无法找到可行的解决方案。

XAML:

    <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="NotifyIconResources.xaml" />
            <ResourceDictionary Source="pack://application:,,,/Telerik.Windows.Themes.Windows8;component/Themes/System.Windows.xaml" />
            <ResourceDictionary Source="pack://application:,,,/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Navigation.xaml" />
        </ResourceDictionary.MergedDictionaries>

这是 ReSharper 给出的错误图片: ReSharper Image

这是作为引用的程序集图像: Assembly as reference

这是程序集属性的图像: Assembly properties

最后,这是我尝试运行应用程序时的异常信息:

System.Windows.Markup.XamlParseException was unhandled
  HResult=-2146233087
  Message='Cannot create unknown type '{clr-namespace:Telerik.Windows.Controls.External}Windows8ThemeExternal'.' Line number '15' and line position '6'.
  Source=PresentationFramework
  LineNumber=15
  LinePosition=6
  StackTrace:
       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)
       at MyApp.App.InitializeComponent() in App.xaml: line 1
       at MyApp.App.Main() in c:\MyApp\obj\Debug\App.g.cs: line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Xaml.XamlObjectWriterException
       HResult=-2146233088
       Message='Cannot create unknown type '{clr-namespace:Telerik.Windows.Controls.External}Windows8ThemeExternal'.' Line number '15' and line position '6'.
       Source=System.Xaml
       LineNumber=15
       LinePosition=6
       StackTrace:
            at System.Xaml.XamlObjectWriter.WriteStartObject(XamlType xamlType)
            at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
            at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
            at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       InnerException: 

最佳答案

根据 Telerik 文档,我知道的第一件事是错误的,必须添加,

They can be merged in the resources of your application (in App.xaml) and as a result will be applied implicitly on any control that does not have a local Style set. For example, if you need to style a control from Telerik.Windows.Controls.dll, you need the Telerik.Windows.Controls.xaml resource dictionary.

这就是说您缺少 DataVisualizationResourceDictionary。如果这不能解决问题,请继续阅读。

最后我记得,当我查看使用 Telerik 为我的应用程序设置主题时,有几种不同的方法可以应用样式,一开始要正确使用可能有点棘手。有隐式样式 (NoXaml) 和标准样式,其中样式内置到控件库中。隐式样式是以“全局”方式对应用程序进行样式化的推荐方法。 IIRC,它的性能也更高,代码也更干净。

自从我们在 WPF 聊天中讨论以来,听起来您并不确定您尝试从源代码构建哪些二进制文件。隐式样式需要您构建 Binaries.NoXaml 源代码。说完这些之后,您必须确保添加对来自 ..\Binaries.NoXaml\WPF4x\ 目录的正确程序集的引用。

上次我从源代码构建时,构建说明包含在下载的 .ZIP 中。请务必仔细遵循这些说明。我没有使用内置的 Visual Studio Telerik 工具来构建/部署我引用的库,所以如果你是,那么也许手动是可行的方法。

还有一些其他的事情。我怀疑构建没有成功完成。您在 Telerik 引用中的版本号应该不是 0.0.0.0。其次,如果我没记错的话,我在定义源时遇到了问题,可能顺序很重要——所以试试我在下面的例子中使用的源,也许移动 NotifyIconResources.xaml 在 Telerik 声明下方。

这是我使用隐式样式技术的 XAML:

<ResourceDictionary Source="/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.xaml" />
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows7;component/Themes/System.Windows.xaml" />
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.Input.xaml" />
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.Navigation.xaml" />

一如既往,开始一个新项目并让像这样的基本东西工作起来可能会让你感到惊奇,它也会阻止你改变你当前代码库中的太多东西,以至于你最终会忘记你已经改变的东西并且在您不想要的地方引起问题。

关于c# - 加载资源字典时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31035427/

相关文章:

c# - 在 ASP.Net Core 中获取基本 URL

wpf - 从托盘动画窗口运动

javascript - Rad-grid 编辑模式启用通过 JavaScript 在复选框选择上禁用所需的字段验证

c# - 覆盖 Telerik Radgrid 数据绑定(bind)

c# - 在泛型类型定义中使用 'params' 的方法

c# - 使用异步创建隐式任务?

c# - WPF 实体无法创建 View 的 ObjectSet?

version-control - Sitefinity 开发环境和源代码控制

c# - 简化if条件?

c# - 使用 MahApps 保持样式扩展 WPF 中的 TextBox