开发者

Setting local int variable's value to Int.MaxValue in xaml

I was wondering how I can set a local int variable's value to Int.MaxValue in xaml (which is in the ResourceDictionary in my case).

something like: (but something that works :) )

xmlns:s="clr-namespace:System;assembly=mscorlib

<s:Int32 x:Key"HelloWorld">{x:Static s:Int.MaxValue}</s:Int32>

E开发者_JS百科DIT:

@Ian:

Thank you :) But how do I use the static resource as an int? let's say if I have in my ResourceDictionary

<ResourceDictionary>
    <x:Static
        x:Key="HelloWorld"
        Member="s:Int32.MaxValue"
        />
...
    <blablalba TooltipService.ShowDuration="{StaticResource HelloWorld}"/>` <-- this does not work by the way
</ResourceDictionary>


This should do it:

<x:Static
    x:Key="HelloWorld"
    Member="s:Int32.MaxValue"
    />

Just in case it's not clear what's actually happening here, it's using a handy trick that a lot of people don't know about: although markup extensions like x:Static are normally used in attributes inside braces ({}) you can also use the element syntax for them. This is useful in situations like these where you want to use a markup extension, but the Xaml syntax expects an element. (It's most often necessary in scenarios involving collections, like this one.)

The one slight gotcha with using element syntax for markup extensions is that you no longer get to pass in constructor arguments. (Technically, constructor argument support was introduced in XAML 2009, which has been supported since .NET 4. However, it's only supported if you load Xaml via the new Xaml APIs. The MSDN documentation says that the new XAML 2009 features are not supported for compiled Xaml in WPF.) So you can only use this trick with markup extensions that offer default constructors, and which provide properties that can be used instead of constructor arguments. The StaticExtension provides a Member property that does the trick, so we're OK here, but be aware that sometimes, you'll be stuck due to the inability to call the appropriate constructor.

Note that as you type this in, the Xaml IntelliSense in Visual Studio will probably suggest StaticExtension, which is the real name of this markup extension. When you use the attribute syntax, it leaves off the Extension part, but with elements, it seems not to, for some reason. Either form is correct though - I'd go with what I've written here and not what IntelliSense suggests, because we're more accustomed to seeing "{x:Static ...}" in attributes than "{x:StaticExtension ...}" - again, both are legal, but idiomatically, we tend to leave off the Extension.


You were so close first time, just turns out you missed the 32 from Int32

<blablalba TooltipService.ShowDuration="{x:Static s:Int32.MaxValue}"/>

The other solution of adding a static resource seems a bit ridiculous - you're essentially creating a static resource as a wrapper around an existing static property...


The problem is that there is no class Int If you try with Int32 it works


As such.

<Button>
   <ToolTipService.ShowDuration>
      <x:Static Member="sys:Int32.MaxValue" />
   </ToolTipService.ShowDuration>
</Button>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜