在代码后面更改/添加资源字典,请帮忙

发布于 2024-10-19 04:56:54 字数 1573 浏览 10 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葮薆情 2024-10-26 04:56:54

这行得通吗

<Application.Resources>
    <ResourceDictionary x:Key="resourcestyles">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary /> <!-- Dummy, this is the one we will replace -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

然后我们在 [0] 中放置一个 ResourceDictionary(我们的虚拟 ResourceDictionary 所在的位置)。

加载或替换样式字典(使用 app.xaml.cs 中的 Application_Startup 中的默认样式加载)

var rDictionary = new ResourceDictionary();
rDictionary.Source = new Uri(string.Format("/MyApp;component/Resources/Styles/{0}/Styles.xaml", style), UriKind.Relative);
this.Resources.MergedDictionaries[0] = rDictionary;

将 MyApp 替换为您的应用程序名称。

Does this work

<Application.Resources>
    <ResourceDictionary x:Key="resourcestyles">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary /> <!-- Dummy, this is the one we will replace -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

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)

var rDictionary = new ResourceDictionary();
rDictionary.Source = new Uri(string.Format("/MyApp;component/Resources/Styles/{0}/Styles.xaml", style), UriKind.Relative);
this.Resources.MergedDictionaries[0] = rDictionary;

Replace MyApp with your applications name.

我只土不豪 2024-10-26 04:56:54

首先按照下面 NateTheGreat 提到的步骤操作,并在 UriStirng 前面加上“/”。它应该看起来像:

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);

First follow the steps mentioned by NateTheGreat below and prepend your UriStirng with "/". It should look like:

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);
静若繁花 2024-10-26 04:56:54

我刚刚遇到这个问题。就我而言,解决方案是将资源字典 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文