问题标签 [staticresource]

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 投票
1 回答
2341 浏览

wpf - 属性为 StaticResource 的自定义按钮

我正在尝试实现以下目标:在自定义按钮中使用 svg 图像。

为此,我创建了一个自定义按钮:

我拿了一个 svg 文件,在 inkscape 中打开它并将其保存为 xaml 文件。

我打开 Themes.xaml 并将创建的 xaml 图像添加为 ControlTemplate

按钮样式为:

如您所见,我已经硬编码了 StaticResource 名称<ContentControl x:Name="Img" Template="{StaticResource Home}" />

我希望能够在此模板上与属性 Image 进行绑定,例如

这样我就可以使用我想要的 StaticResource 的名称设置按钮的 Image 属性。

例如,在“主页”图像旁边,另一个“返回”我将在 MainWindow 中有两个按钮,声明如下:

任何建议都会被采纳。感谢您的时间。

0 投票
1 回答
1069 浏览

wpf - WPF 压缩此 XAML,它将静态字符串分配给绑定

我想做的事情可能没有任何实际用途,但为了满足我的好奇心,我们可以将以下 XAML 中的第二个绑定压缩为 1 行

StaticResource One 给我“1”,而 MyConverter 只是连接它获得的所有值。我正在寻找的是某种以紧凑格式表达第二个绑定的方法,例如第一个绑定。

0 投票
1 回答
1030 浏览

silverlight - 在 VisualState 中修改 Silverlight 资源

我想知道是否有一种简单的方法可以修改不同 VisualStates 之间控件的某种共享资源(即画笔)。例如,我想定义一个画笔作为边框的背景和不同矩形的填充。在不同的 VisualState 中,我想在一个地方(资源)更改此背景画笔,并将其反映在使用该资源的所有元素中。

我不确定是否真的可以通过名称(而不是键)为 VisualState 中情节提要的 TargetName 引用资源。

这是我在 XAML 中尝试做的一个简化示例:

我有一种感觉,因为这些是 Silverlight 中的静态资源,它们只加载一次并且无法更改。我知道 WPF 有一些动态资源的概念。有什么方法可以在 Silverlight 中实现这种类型的行为,而无需在所有元素中重新定义我的画笔?

0 投票
1 回答
3468 浏览

wpf - 如何从当前组件资源中引用静态资源?

这是问题的演示:

从一个简单的窗口:

我正在尝试访问 ResourceDictionary 中的资源:

Visual Studio 2010 显示带有预期结果的窗口,但在运行应用程序时出现此异常:

注意 1:当使用 DynamicResource 时,结果在 VS 2010 和运行时都很好。

注意 2:将资源字典的引用放在 TextBlock 之外,例如在 Windows 资源中,给出了预期的结果,但我的实际情况不允许这样做。

那么,我做错了什么以及如何获得预期的结果?

回答: 经过更多测试后,XAML 解析器似乎非常小,它会按顺序解析内容,而不会尝试解释任何内容。所以声明顺序很重要:你必须把对“文本”属性的引用放在对字典的引用之后。这样做的唯一方法似乎是将对 StaticResource 的引用包装在“Binding”元素中。

提前致谢。

0 投票
2 回答
7428 浏览

c# - 在 WPF 中,如何引用在不同 XAML 文件中定义的静态资源?

在 WPF 中,如何引用在不同 XAML 文件中定义的静态资源?它在同一个项目中。

0 投票
2 回答
19996 浏览

wpf - 使用 XAML 中的系统类型作为资源

我遇到了一种情况,直接在 XAML 中指定浮点值并将其用作我的几个 UI 片段的资源非常有用。在四处搜索后,我发现了大量有关如何在 XAML 中包含正确程序集 (mscorlib) 的信息,以便您可以做到这一点。

不幸的是,在我尝试这样做的一个实例中,我遇到了一个例外。下面是重现这种情况的以下 XAML:

当我尝试编译并运行它时,我收到一个 XamlParseException 向我抛出,它说“'120' 不是属性'Width' 的有效值”。

但是“Width”属性双精度的,那么为什么我不能使用定义的 StaticResource 来设置它呢?有谁知道如何做到这一点?

0 投票
1 回答
1594 浏览

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

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

0 投票
1 回答
1967 浏览

c# - 将用户控件与来自外部程序集的静态资源一起使用

这听起来很简单,它杀死了我!

我正在尝试将一个用户控件与来自外部程序集的 ResourceDictionary 的样式一起使用,但在运行时出现异常。

以下是如何重现:

  1. 创建一个名为 MyControls.dll 的 silverlight 类库
  2. 创建一个名为 SuperControl 的用户控件:

    /li>
  3. 创建 Styles.xaml ResourceDictionary 并添加:

  4. 创建一个名为 SL 的 Silverlight 应用程序并添加 Mycontrols 作为参考

  5. 在 MainPage.xaml 网格中,添加:

    /li>
  6. 它会编译,但运行应用程序时会得到“无法分配给属性'System.Windows.ResourceDictionary.Source'。[行:10 位置:36]”

  7. 我将此添加到应用程序的 App.xaml

    /li>
  8. 同样的错误... :(

有什么想法吗?

0 投票
2 回答
4628 浏览

wpf - 合并字典中共享的静态资源

我目前正在研究可以动态应用于我的应用程序的样式和模板字典。在这种“新的”动态行为之前,我有几个资源字典,每个样式控件一个,我合并到 App.xaml 中:

现在,我希望我的应用程序具有样式,因此我决定将我以前的所有资源合并到一个名为“MyFirstTemplates”的新资源中,并仅将这个字典添加到 App.xaml。

新字典“MyFirstTemplates.xaml”:

新的 App.xaml:

注意:Window的默认样式是为了更正 WPF 4 的一个错误,请参阅将合并字典添加到合并字典

现在我已经进行了这个更改,我不能再将“ColorsDictionary.xaml”中的颜色资源用作 ControlsTemplateDictionary.xaml”中的静态资源。如果我改回在 app.xaml 中合并这些文件,一切正常。为了使其工作,我必须将这些StaticResource更改为DynamicResource。你知道为什么这不再起作用了吗?

谢谢 :-)

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...