使用资源定义厚度 [英] Define a Thickness using resources

查看:22
本文介绍了使用资源定义厚度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 windows UWP 应用项目中,我试图通过分配给它的 LeftTopRight 来定义厚度>底部属性:

In a windows UWP app project, I am trying to define a Thickness by assigning to its Left, Top, Right and Bottom properties:

<Setter Property="Margin">
    <Setter.Value>
        <Thickness Left="{StaticResource SomeDouble}"
                   Top="0"
                   Right="0"
                   Bottom="0" />
    </Setter.Value>
</Setter>

这个答案 似乎表明这在 WPF 中是可能的,但是,在我的 UWP 项目中(以及在WinRT 应用程序),我收到以下错误:

This answer seems to suggest that this is possible in WPF, however, in my UWP project (as well as in a WinRT app), I get the following error:

XAML Thickness type cannot be constructed. 
In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic 
or a struct, and must have a public default constructor.

有没有办法使用资源来定义厚度?

Is there a way to define the Thickness using resources?

推荐答案

你仍然可以.仅将 System:Double 更改为 x:Double.例如

You still can. Only to change System:Double to x:Double. e.g.

12

有趣的是,使用上面的 xaml,设计器显示正常,但无法编译...是的,它给出了与您显示的完全相同的错误.

Interesting, with the above xaml the designer shows up fine but it doesn't compile... Yes it gives the exact same error like you showed.

所以我猜你必须在 xaml 中定义整个 Thickness.

So I guess you have to define the whole Thickness in xaml.

<Thickness x:Key="Thickness1">12,0,0,0</Thickness>

<Setter Property="Margin" Value="{StaticResource Thickness1}" />

这次我运行了这个项目,是的,它奏效了.:)

I ran the project this time and yeah it worked. :)

这篇关于使用资源定义厚度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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