在代码后面更改/添加资源字典,请帮忙
我目前正在开发一个 silverlight 应用程序,我仍然是一个初学者。 我想知道是否可以在后面的代码中更改资源字典的源代码(C#) 在 App.xaml 中? 我已尝试下面的代码,但出现异常,我从 WCF 服务获取样式文件夹名称,该变量称为 Style(这包含文件夹的名称)
ResourceDictionary rDictionary = this.Resources.MergedDictionaries[0];
rDictionary.Source = new Uri(string.Format("Resources/Styles/{0}/Styles.xaml", style), UriKind.Relative);
this.Resources.MergedDictionaries.Add(rDictionary);
我收到错误,
rDictionary.Source = new Uri(string.Format("Resources/{0}/Styles.xaml", "Default"), UriKind.RelativeOrAbsolute);
其中读取
System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, String s)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
at System.Windows.ResourceDictionary.set_Source(Uri value)
at FCStarFish.App..ctor()
I'm am currently developing a silverlight application, I am still a beginner with this.
I am wondering if it is possible to change the resource dictionary's source in code behind(C#)
within the App.xaml?
I have tried the code below, but get an exception, i am getting the style folder name from a WCF Service, the variable is called Style(this contains the name of the folder)
ResourceDictionary rDictionary = this.Resources.MergedDictionaries[0];
rDictionary.Source = new Uri(string.Format("Resources/Styles/{0}/Styles.xaml", style), UriKind.Relative);
this.Resources.MergedDictionaries.Add(rDictionary);
I'm getting an error at
rDictionary.Source = new Uri(string.Format("Resources/{0}/Styles.xaml", "Default"), UriKind.RelativeOrAbsolute);
Which reads
System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, String s)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
at System.Windows.ResourceDictionary.set_Source(Uri value)
at FCStarFish.App..ctor()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这行得通吗
然后我们在 [0] 中放置一个 ResourceDictionary(我们的虚拟 ResourceDictionary 所在的位置)。
加载或替换样式字典(使用 app.xaml.cs 中的 Application_Startup 中的默认样式加载)
将 MyApp 替换为您的应用程序名称。
Does this work
Then we place a ResourceDictionary in [0] (where our dummy ResourceDictionary is).
Load or replace the style dictionary (load it with the default style in Application_Startup in app.xaml.cs)
Replace MyApp with your applications name.
首先按照下面 NateTheGreat 提到的步骤操作,并在 UriStirng 前面加上“/”。它应该看起来像:
First follow the steps mentioned by NateTheGreat below and prepend your UriStirng with "/". It should look like:
我刚刚遇到这个问题。就我而言,解决方案是将资源字典 XAML 文件的构建操作更改为“内容”,将“复制到输出目录”更改为“如果较新则复制”,并将自定义工具更改为空字符串。默认值分别设置为“Page/Do not copy/MSBuild:Compile”。
I just encountered this problem. In my case, the solution was to change the resource dictionary XAML file's build action to "Content", Copy to Output Directory to "Copy if newer", and Custom Tool to an empty string. The defaults were set to Page/Do not copy/MSBuild:Compile, respectively.