在 XAML 根元素中使用 StaticResource [英] Use StaticResource in XAML root element

查看:24
本文介绍了在 XAML 根元素中使用 StaticResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 xaml 文档的根元素中使用 StaticResource.但 MSDN 说:

I want to use StaticResource in the root element of a xaml document. But MSDN says:

来自内部的静态资源引用资源字典必须引用一个已经定义的资源在资源之前的词法上参考.前向引用不能由静态资源解析参考.

Static resource references from within a resource dictionary must reference a resource that has already been defined lexically before the resource reference. Forward references cannot be resolved by a static resource reference.

还有这个:

查找过程然后遍历向上的逻辑树,到父级元素及其资源字典.这一直持续到根元素已到达.

The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.

我应该在应用程序中定义我的资源还是从代码创建它?

Should I define my resource in the application or create it from code?

推荐答案

您实际上可以将任何属性设置为元素和属性,包括像 Window.Left 这样简单的属性.

You can actually set any property as an element as well as an attribute, including ones as simple as Window.Left.

这意味着您可以在声明资源后设置 Left 的值.

This means you can set the value of Left after you declare your resources.

<Window.Resources>
    <app:LeftConverter
        x:Key="LeftConverter" />
</Window.Resources>

<Window.Left>
    <Binding
        Path="UnconvertedLeft"
        Converter="{StaticResource LeftConverter}" />
</Window.Left>

这篇关于在 XAML 根元素中使用 StaticResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆