Adding resources into Silverlight application
I am trying to add resource dictionary into my silverlight-4 aplication (suggested in "Applying a View to a ViewModel" chapter of the http://msdn.microsoft.com/en-us/magazine/dd419663.aspx article).
The 1st problem: I don't see any resource in my MainPage. Am I correctly understand that I need to add resource dictionary manually into Silverlight app?
The 2nd: When I did that, in the Dictionary1.xaml file
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib">
<DataTemplate DataType="{x:Type vm:MyViewModel}">
<vw:MyView />
</DataTemplate>
</ResourceDictionary>
I am getting an error: Can't resolve a symbol 'DataType'...
Is 开发者_JS百科there any idea hot to do that?
ad 1: MainPage has a ResourceDictionary. You add elements to it in xaml like this:
<MainPage>
<MainPage.ResourceDictionary>
<DataTemplate>
<vw:MyView />
</DataTemplate>
</MainPage.ResourceDictionary>
...
You can add a ResourceDictionary to the MainPage.ResourceDictionary by using the Source and MergedDictionaries properties of ResourceDictionary:
<MainPage>
<MainPage.ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</MainPage.ResourceDictionary>
...
ad 2: DataTemplate does not have a Property DataType in the Silverlight framework. :-(
You also need to add an x:Key into the DataTemplate if it's going to be in the ResourceDictionary.
精彩评论