Define a Thickness using resources

╄→尐↘猪︶ㄣ 提交于 2020-01-14 14:16:28

问题


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>

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?


回答1:


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

<x:Double x:Key="SomeDouble">12</x:Double>

Update

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.

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



来源:https://stackoverflow.com/questions/35149204/define-a-thickness-using-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!