Define a Thickness using resources

2019-07-11 18:16发布

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条回答
我只想做你的唯一
2楼-- · 2019-07-11 18:57

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

查看更多
登录 后发表回答