为什么需要静态资源?
首先,我进行了长时间的努力,试图找到这个问题的答案。我在这里寻求专家帮助来解决这个问题。
我目前正在阅读一本有关 Windows Phone 7 编程的书。我目前正在学习数据绑定(而且做得也不错)。我遇到了一个关于WPF中DataBinding格式的问题,主要是关于StaticResource的功能。
在您将要看到的以下代码中,有一个滑块和一个文本块。文本块绑定到滑块,以便当滑块移动时,文本块的值会发生变化。已创建一个类 TruncationConverter,并且可以在 XAML 中使用关键字“truncate”调用该类。它在phone:ApplicationPage.Resources中声明。
所以,这是对的
<TextBlock Name="txtblk"
Text="{Binding ElementName=slider,
Path=Value,
Converter={StaticResource truncate}}"
这是错的
<TextBlock Name="txtblk"
Text="{Binding ElementName=slider,
Path=Value,
Converter=truncate}"
这本书从未真正解释为什么必须将 StaticResource 放在函数之前。
那么,问题来了,为什么需要在调用之前放置StaticResource呢?它有什么作用,它的作用是什么?为什么在 truncate 之前不放置 StaticResource 会出现错误。
提前致谢!
First, I searched long and hard to try to find the answer to this. I resorted to here for expert help with this problem.
I am currently reading a book about programming for the Windows Phone 7. I am currently learning about Data Binding (and doing pretty good too). I have come across a question about the formatting of DataBinding in WPF, mostly about the function of StaticResource.
In the following code you are about to see, there is a slider and a text block. The text block is binded to the slider so that when the slider is moved, the text block's value changes. A class has been created, TruncationConverter, and has can be called in XAML with the keyword "truncate". It is declared in phone:ApplicationPage.Resources.
So, this is right
<TextBlock Name="txtblk"
Text="{Binding ElementName=slider,
Path=Value,
Converter={StaticResource truncate}}"
And this is wrong
<TextBlock Name="txtblk"
Text="{Binding ElementName=slider,
Path=Value,
Converter=truncate}"
The book never really went in to explaining why one must put StaticResource before the function.
So, the question is, why do you need to put StaticResource before the call? What does it do, what is its function? Why is there an error when you don't put StaticResource before truncate.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Converter 类的构造函数使用标记扩展才能工作。标记扩展要求预先在对象图中定义该对象,这是在为转换器类分配键时完成的。当 Xaml 解析器看到 StaticResource(或 DynamicResource)时,它开始在对象图中向上查找,直到找到该值。在运行时,将创建该类的实例并用于进行转换。创建转换器的实例后,WPF 将在应用程序的生命周期内使用它,因此是“静态”。
“StaticResource”可能看起来无关紧要或多余,因为转换器不能是 DynamicResource,但这就是 Xaml 的语法规则。
The constructor for the Converter class uses a markup extension in order to work. The markup extension requires that the object be previously defined in the object graph, and this is was done when you assigned your converter class a key. When the Xaml parser sees StaticResource (or DynamicResource) it starts looking upward in the object graph until the value is found. At runtime, an instance of the class is created and used to do your conversions. Once an instance of your converter has been created, WPF uses it for the life time of your application, hence 'Static'.
The 'StaticResource' may seem extraneous or redundant because a converter cannot be a DynamicResource, but such are the syntax rules of Xaml.
基本上放置 StaticResource 是告诉它在 ResourceDictionary 中查找可能具有“截断”功能的外部属性
所以就像另一个例子,如果我去创建另一个控件或转换器,甚至是画笔或我希望的其他实例为了在应用程序的其他元素中可用,它被创建为仅在一个位置(资源字典)可编辑但可供所有人使用的可用资源,例如; StaticResource
就像放置滑块和 Textblock 时一样,它默认为 CoreStyles 资源字典中找到的每个元素调用一个样式。例如,如果我想更改他们的行为或外观,我可以复制资源,根据需要进行编辑,重命名,然后通过以下方式调用它:
Basically placing StaticResource is telling it to find the external property likely in a ResourceDictionary which holds the function of for example "truncate"
So like another example would be if I go and say create another control or converter or even a brush or other instance I wish to be made available throughout other elements of an application, it's created as an available resource that is only editable in one spot (a resource dictionary) but usable by all, eg; a StaticResource
Like when you placed your slider and your Textblock, it by default is calling a style for each found in your CoreStyles resource dictionary. If I wanted to change what they did or how they look for example I could copy the resource, edit it as necessary, rename it, and say call it by