views:

63

answers:

2

I am trying to create a reusable System:Char value in my xaml resources.

I have:

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

Then:

<s:Char x:Key="MaskPromptChar">#</s:Char>

I get an exception:

The type 'Char' was not found. [Line: 8 Position: 44]

But... I also have:

<s:Double x:Key="DefaultControlWidth">200</s:Double>

And...

<s:String x:Key="ApplicationTitle">My Title</s:String>

Both String and Double work fine.

Ideas??

A: 

My guess is that Char is a structure, rather than an object, and what's you're really looking at is boxing of a 16-bit integer value representing Unicode. I tend to look at XAML as a specialization serialization of objects, and if Char is actually a struct, this model may break down.

You might want to consider biting the bullet and using a String of length one instead.

UPDATE: I agree with gmcalab's solid line of reasoning, and based on that conducted an experiment using a namespace to mscorlib and the corresponding character resource. It worked without a hitch. (Are we now looking at a namespace or library collision problem?)

Walt Stoneburner
But Double is a struct too, so if that works I'd expect Char to be okay...
Jon Skeet
I tried to use a `string` with Length of one but when I go to use the Key it won't convert the type `string` to type `char`. i.e. `PromptChar='{StaticResource MyStringLengthOfOne}'` gives me an exception `Object of type 'System.String' cannot be converted to type 'System.Char'.`
gmcalab
I was just suggesting using the My Title example and keeping it a String throughout the whole app. Perhaps not ideal for your masking case, though.
Walt Stoneburner
@Walt, yes I know that's what you were suggesting, but the control property expects a `char`, therefore using a `string` with a length of 1 will not work, hence the exception in my previous comment.
gmcalab
@Walt, in response to your UPDATE... what worked? You don't provide any code to show us.
gmcalab
Your example of `<sys:Char x:Key='myChar'>#</sys:Char>` worked just fine in my XMAL file. I just did what you did.
Walt Stoneburner
What is your namespace declaration for `sys`
gmcalab
Oh my. Two Walts on one thread. I think you are talking with Walt Stoneburner, but here's my namespace declaration. xmlns:sys='clr-namespace:System;assembly=mscorlib'
Walt Ritscher
`xmlns:sys="clr-namespace:System;assembly=mscorlib"`
Walt Stoneburner
A: 

This code works for me in both Silverlight and WPF.

<UserControl.Resources>
<sys:Double x:Key='myDouble'>4</sys:Double>
<sys:Char  x:Key='myChar'>#</sys:Char>
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot">
  <PasswordBox Password='aaa'
               PasswordChar='$' />
  <PasswordBox Password='aaa'
               PasswordChar='{StaticResource myChar}' />
</StackPanel>

What property are you trying to assign the char to?

Walt Ritscher
I am assigning it to a 3rd party control's property. Currently, right now I am not assigning it to anything, just defining it in xaml. At runtime it complains with `The type 'Char' was not found. [Line: 8 Position: 44]`
gmcalab
Did you name any of your types in your project named Char? That could cause a name conflict.
Walt Ritscher
No I didn't name any type Char anywhere.
gmcalab
At this point I'm not sure what to say. The XAML works on my computer, but is failing on yours.
Walt Ritscher