Posted By: stevo_ | Nov 19th, 2008 @ 10:13 AM
page 1 of 1
Comments: 18 | Views: 1619
stevo_
stevo_
Human after all

Given the poco class:

public class TestClass
{
  public int SomeInt { get; set; }
}

and the xaml:

<TestClass xmlns="ref to clr namespace where TestClass is">
  <TestClass.SomeInt>20<TestClass.SomeInt>
</TestClass>

I get an exception:

Unknown build error, 'Object reference not set to an instance of an object.'

If I use the xaml:

<TestClass xmlns="ref to clr namespace where TestClass is" SomeInt="20" />

All is good..

I wondered at first if this was some sort of conversion issue, but as far as I can tell - xaml should be doing primative type conversions itself.. whats even weirder is - this is currently compiled xaml to baml.. so I actually have the x xaml namespace, a class attribute and code behind.. but if I change it to loose xaml, I don't see any errors..

Anybody done anything with xaml outside of wf wpf?

It isn't a biggy, but I really aren't sure where to look given the error is basically "i'unno, something bad i guess?"

I think I remember reading that for simple types you can't do them like your first example.  That is reserved for collections, like Grid.RowDefinitions.

I could also be remembering some crazy XAML dream, so take this with a grain of salt.
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
Could you post a compilable sample project that shows what you're doing exactly?
This sounds like PropertyElements inside the XAML MarkupCompiler may have a problem with types from mscorlib which have type converters?  Our markupcompiler and the normal runtime parser sometime have bugs that cause them to not work consistently.  We now have a bug filed on this issue and will address this issue in the future.  For now, you can likely either:
1) use an attribute to represent this value
2) you may be able to add a property level type converter to point to BoolConverter, etc......However, this doesn't scale very well.  And you can only do it on properties that you define.

The reason for this bug is probably the fact that types in MsCorLib can't be marked with TypeConverterAttributes since that is a System.dll thing.  We have a hard coded list in the XAML stack of the list of types from mscorlib that have type converters.

In .NET 4, the WPF parser is being rearchitected to run on top of System.Xaml.dll that will make it so that if an attribute form works, so will the property element form.  The XamlXmlReader component will represent both of those situations identically...so we should have better behavior.

The WPF forum and relevelant posts on blog are great places to ask XAML questions.

Thanks, Rob Relyea
Architect, XAML/WPF Teams
http://robrelyea.com/blog
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...

I dug into this a little by attaching a second instance of VS to the first VS instance and running the build. Thanks to the .Net reference source I could get some idea of what was going on. It was encountering an error loading the assembly (obviously, since we're compiling that assembly it doesn't exist yet) and stores a null value, which the XamlParser tries to use as context somewhere and throws a NullReferenceException. It jumps around a lot because of the optimizations but I think that's what happens.

In any case, after moving the type TestClass to another assembly, Stevo's example works fine. It appears to be a bug in the parser when using types from the assembly you're building as the root element of a XAML file.

Harlequin
Harlequin
http://twitter.c​om/TrueHarlequin
If not, you'll need to use DependencyProperty I think....
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
I tried that too, made no difference.
Harlequin
Harlequin
http://twitter.c​om/TrueHarlequin
No, it's silverlight as well. Did you try it as an attribute, with SomeInt="20"?
Harlequin
Harlequin
http://twitter.c​om/TrueHarlequin

public static readonly DependencyProperty SomeIntProperty = DependencyProperty.Register("SomeInt", typeof(int), typeof(YourControl), new PropertyMetadata(new PropertyChangedCallback(onIntChanged)));

So you can't use anything like this? Or do you still get errors...

Harlequin
Harlequin
http://twitter.c​om/TrueHarlequin
Nice conversation.

An adult would have said something like this:
"I see where you're coming from, but here's the problem..."

Instead of going off on a rant...
page 1 of 1
Comments: 18 | Views: 1619
Microsoft Communities