开发者

Silverlight 4: "Invalid XAML" using Converter in DataTemplate

maybe you could help me understand why I get an unhandled exception "Invalid XAML" in Visual Studio 2010 designer when trying to do the following on a Page.

I have a Converter named DateTimeConverter that converts a date into a German date string. The converter works fine. I've included the namespace and added the following to the page's resources:

<navigation:Page.Resources>
    <myClasses:DateTimeConverter x:Key="dateTime" />
</navigation:Page.Resources>

Now I have a list box that I want to bind to a list of objects. I do the binding in code, but I would like to define the data template. Thus I've added the following to my layout:

<ListBox x:Name="lbConversation" BorderBrush="#00000000">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0" Padding="4">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Message, Mode=OneWay}" />
                    <TextBlock Text="{Binding TimeStamp, Mode=OneWay, Converter={StaticResource dateTime}}" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And this works fine when I run. However, in the code section, the code for the data template is undercurled and the designer says "Invalid XAML". When I remove the Converter=.开发者_StackOverflow中文版.. part, this error is gone.

Is this a designer bug? What can I do?

EDIT

By the way: The exact same code does not throw the error within a WPF project!


Just adding to this question as I found a solution.

The solution to my case was here: http://forums.silverlight.net/post/618518.aspx

Apparently you must not have a space character in your project name or assembly name. D'oh!

Hope it helps.


Sorry, can't replicate this at all, do you have some design-time data that may be the cause of the weird error?

Also, since you said that you're using the converter to output german dates... wouldn't it be easier to let the framework do this kind of things, as it probably does them a lot better? Set the entire application thread's CultureInfo to german and all formatting will be done with that culture's settings; of course it's still possible you only want some controls internationalized...


I can't see anything wrong with your Xaml however I wonder if this is a result of the language setting used when the Xaml is parsed. By default Xaml is parsed using the InvariantCulture however it would appear that the designer in visual studio parses the Xaml using the current culture. Hence at times you can get unexpected differences in behaviour at design time than you do at runtime.

In fact if you do this in the constructor of your UserControl before calling InitializeComponent:-

 this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

You might not need your converter at all.


I came across "Invalid XAML" error when I had my converter marked internal. Change the modifier to public and everything is as expected.

Hope this help.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜