问题标签 [dynamicresource]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
6324 浏览

wpf - DynamicResource 而不是 StaticResource 是否有显着的性能成本?

我们的设计师正在使用 Blend 来设计我们的 WPF 应用程序。当他为属性选择本地资源时,Blend 会将它们应用为 a{DynamicResource}而不是 a {StaticResource}。我的猜测是 Blend 这样做是因为它使应用程序能够在运行时重新设置主题,而无需重新启动它。

我的问题是:这种额外的查找是否有显着的性能成本?我们是否应该要求设计师返回并手动将那些 Dynamics 更改为 Statics?

这是一个很好的 SO 问题,解释了类型之间的区别:WPF 中的 StaticResource 和 DynamicResource 有什么区别?

0 投票
1 回答
3824 浏览

c# - DynamicResources 是如何构建的以及它们在上下文菜单中的使用

动态资源真的是动态的吗?如果我定义了一个 DynamicResource,我意识到创建了一个表达式(在哪里?),直到运行时才转换为资源,但是,我不明白的是这个动态资源,一旦构建,现在是否是“静态的”

例如,如果我通过动态资源创建上下文菜单,那么在访问时创建的菜单项是否是静态的,即使它们是绑定的?

如果是这样,我如何在 XAML 中创建动态上下文菜单?

0 投票
1 回答
2672 浏览

wpf - 为什么 Storyboard 价值不会从资源或绑定中获得?

我在下面有结构,它可以工作:

如果我尝试这样的事情为什么它不起作用:

是的,我在故事板之前定义了资源。像下一个这样的声明也不起作用:

是的,它是背后代码的公共属性,并且this.DataContext设置为this

0 投票
3 回答
9086 浏览

wpf - WPF StaticResource 有效,DynamicResource 无效

我已经尝试了一天,但无济于事,在主题中创建一堆画笔,然后在自定义控件中将它们与 DynamicResource 一起使用。我所做的是这样的:

  • 创建包含样式(作品)的主题 generic.xaml
  • 添加字典以合并到 generic.xaml 以包含应用程序中使用的画笔(作品)
  • 使画笔具有 ComponentResourceKey 键(有效)
  • 使控件使用画笔作为静态资源(作品)
  • 使控件使用画笔作为动态资源(不工作,资源跟踪源说了很多:System.Windows.ResourceDictionary Warning: 9 : Resource not found;)
  • 在 App.Resources 中动态添加具有相同键的画笔(适用于动态资源,它会更改颜色,不适用于静态资源,如预期的那样)

所以我的问题是我找不到任何方法来定义主题中的默认值,以便我可以在应用程序中以编程方式更改它们。StaticResource怎么能找到刷子而DynamicResource找不到?!

我必须补充一点,我创建了一个静态类,将组件资源键作为属性保存,然后我在 xaml 中将其用作 {x:Static UI:ResourceScheme.ControlBackgroundKey} 例如。我的问题似乎与此类似:ComponentResourceKey as DynamicResource 问题只是如果我将静态属性键替换为组件资源键的 XAML 标记,它仍然不起作用。

有人可以帮我吗?:(

0 投票
2 回答
23675 浏览

visual-studio - 如何绑定动态资源并指定路径

我想绑定到资源(DynamicResource)并访问该资源的属性,但是有没有办法做到这一点?

(我想在 Visual Studio 的 xaml 编辑器中可视化构造函数的默认值。通过 DataContext 引用对象或通过我的 Window 类上添加的属性时看不到这些值...)

不工作 xaml :( 在作曲家工作但在运行时不工作......)

与类(可能需要实现 INotifyPropertyChanged):

0 投票
1 回答
1594 浏览

wpf - 将“动态资源”替换为“静态资源”

为了实现我的应用程序,我使用了很多 Blend3。当 Blend3 想要将一个资源链接到另一个资源时,它会多次使用链接类型“DynamicResource”。据我了解(但我可能不太了解),“动态”链接只有在我想在运行时修改链接时才有意义。在其他情况下,他们会徒劳地使用更多内存。我不想在运行时修改任何东西,那么问题是:在我的所有应用程序中用“StaticResource”替换“DynamicResource”是否有意义?谢谢!皮莱吉

0 投票
2 回答
2654 浏览

c# - Sql、Wpf、Xaml、C#、绑定数据、动态资源、访问非静态数据、获取对对象的引用

好的,我对 WPF 和 XAML 还是很陌生,尽管我进行了搜索,但我找不到简单的解决方案,而且在我看来,我很快就无法找到答案。

问题很简单,我创建了一个 WPF 项目并在 SelectList.xaml 中有一个数据网格一旦选择了一行,我将选定的行保存在一个对象中,比如这个对象称为“类别”。到目前为止一切正常,但我不知道如何从其他地方 temp.xaml 获取对该对象的引用?

非常感谢任何帮助将不胜感激干杯

0 投票
1 回答
2562 浏览

wpf - WPF staticresource references to Logical Resources in DataTemplates not resolving at runtime

Have I missed something in the step up from .net 3.5 to .net 4 because I'm seeing seemingly buggy behaviour that seems contrary to the goal of the system.

I am trying to drum up a simple MVVM library for work using a few examples. I am consuming it within a Twitter client application for some additional learning and have hit a big stumbling block.

The scenario is this. My root ViewModel (TwitterClientViewModel) object is given an instance of a DialogViewModel object for display. The DialogViewModel is added to a collection and a bool HasDialogs is set to true. PropertyChanged events are invoked for the collection and the flag if necessary. This part works fabulously.

The view for TwitterClientViewModel is called TwitterClientTemplate and makes Visible an overlay for DialogViewTemplate (DialogViewModel's view) hosting. The hosting ContentControl's template references DialogViewTemplate with a DynamicResource extension. This displays great in the designer and at runtime.

This is where things get strange. The 'body' of DialogViewTemplate hosts dialog content with a further content control bound to DialogViewModel.Content (type object). The hope was that with use of a TemplateSelector (of which I wrote a nice declarative one, but have commented out for testing purposes) I could display both text and interactive elements. For example, requesting details from the user when authenticating a Twitter account. In this case, a PIN number.

At this point I have a two nested contentcontrols for the dialog implementation. For testing purposes, the contentcontrol in the body of DialogViewTemplate uses a staticresource extension to retrieve EnterPINDialogTemplate (view for EnterPINDialogViewModel). Both EnterPINDialogTemplate and DialogViewTemplate are in the same file (the former is defined first of course) although originally they were separate.

At runtime the staticresource extension throws a XamlParseException with the message; 'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.'

and an inner exception message;

'Cannot find resource named 'EnterPINDialogTemplate'. Resource names are case sensitive'

Using a dynamicresource returns null and displays the Fullname of the EnterPINDialogViewModel type in the contentcontrol - as expected when the resource is not resolved. Breaking into my custom TemplateSelector as calling FrameWorkElement.FindResource() throws a similar exception (TryFindResource returns null).

My first thought was that the logical tree is split when the datatemplates are constructed and I remembered an issue in that area from an earlier project. I tried using the MergeDictionaries property of ResourceDictionary to make the resource dictionaries avaliable from within the DataTemplate but the designer did not like that one bit, and the error is described here: http://connect.microsoft.com/VisualStudio/feedback/details/498844/wpf-designer-throws-invalidcastexception

Scratch that idea. I have tried merging the dictionaries at Application, Window and TwitterClientTemplate levels but have had no luck.

Below are the xaml files.

DialogTemplates.xaml

TwitterClientDataTemplate.xaml

Help me stackoverflow, you're my only hope!

EDIT: Did some further work on this issue. If both templates are in the same file dynamicresource and staticresource extensions both resolve the resource without issue. If they are in separate files, the resource will not resolve regardless of how I merge the dictionaries; each extension returns null.

Obviously the solution is to throw both resources into the same dictionary but as far as I'm concerned this is a hack and is not intended behaviour of the logical resource lookup system. I am not a happy bunny right now. This seems pretty undocumented...

0 投票
4 回答
13266 浏览

wpf - 将 IValueConverter 与 DynamicResource 一起使用?

DynamicResource使用扩展时有没有办法定义转换器?有点像

不幸的是,这给了我以下例外:

不能在“Binding”类型的“Source”属性上设置“DynamicResourceExtension”。只能在 DependencyObject 的 DependencyProperty 上设置“DynamicResourceExtension”。

0 投票
2 回答
723 浏览

wpf - WPF 样式不影响某些属性

作为我的资源部分的一部分,我有一个Style指定的 for :ParagraphFlowDocumentReader

我有一个 .xaml 文件,其中包含 myFlowDocument并且它有一些Paragraph定义如下:

我遇到的问题是Foreground不适用于文本(它显示为黑色而不是 LightSteelBlue),并且在修改属性FontSize时不会改变。MyFontSize

我检查了后面代码中的属性值并设置了它,但它不会导致 UI 发生变化。

FlowDocument如果它FlowDocumentReader在运行时加载到,这似乎只是一个问题。如果 XAML 显式放置在FlowDocumentReader.xaml 文件中,则Foreground它是正确的颜色,并且FontSize基于属性设置的更改。

想法?


解决了:

正如我在下面的回答中所写,通过将Style块移动到其本身的资源部分FlowDocument可以解决问题。