4

Closed

Resource Selector sometimes does not select the correct resource, and displays the Text instead

description

One of the issues that I have seen across multiple machines is driving me bananas. We have tested this on about 5 machines now, using IE and FireFox.
 
We are using MVVM, and using the ResourceSelector to push out the correct view to our page. Sometimes, but not always, the resource selector will not put up the correct view, instead it will pump out the full namespace/class into the area where the View should have been. A quick refresh of the browser and that fixes the issue most of the time. It’s hard for me to reproduce, but in our application, we have a full MVVM model, using the Resource selector for our Shell, and sub pages use their own resource selector.
 
Don’t know if anyone else has seen this, but this has happened every day since I started using the Resource Selector. We have had better luck when we close all the browser windows and try again, but that might just be a coincidence.
 
Example:
 
<UserControl.Resources>
<Data:ResourceSelector x:Key="resourceSelector">
<Data:ResourceSelector.Resources>
<ResourceDictionary>
<DataTemplate x:Key="UserLoginViewModel">
<Views:UserLoginView/>
</DataTemplate>
<DataTemplate x:Key="MainMenuViewModel">
<Views:MainMenuView/>
</DataTemplate>
<DataTemplate x:Key="MainDataDisplayViewModel">
<Views:MainDataDisplayView/>
</DataTemplate>
<DataTemplate x:Key="SetupDisplayViewModel">
<Views:SetupDisplayView/>
</DataTemplate>
<DataTemplate x:Key="WelcomeViewModel">
<Views:WelcomeView/>
</DataTemplate>
</ResourceDictionary>
</Data:ResourceSelector.Resources>
</Data:ResourceSelector>
 
Sometimes, we will Request, for example: WelcomeViewModel, or UserLoginViewModel and instead of getting the correct view, it will print out the full NameSpace in a text box.
Closed Feb 20, 2009 at 5:24 PM by simonferquel

comments

jmorrill wrote Dec 30, 2008 at 6:48 AM

I have the exact same problem (and a few others that are using the ResourceSelector). Here is the text from my output window when it happens.

System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value 'MyViewModel' (type 'MyViewModel'); BindingExpression: Path='' DataItem='MyViewModel' (HashCode=58752636); target element is 'System.Windows.Controls.ContentControl' (Name=''); target property is 'ContentTemplate' (type 'System.Windows.DataTemplate').. System.InvalidOperationException: Can't convert type MyViewModel to type System.Windows.DataTemplate.
at MS.Internal.Data.DefaultValueConverter.Create(Type sourceType, Type targetType, Boolean targetToSource)
at MS.Internal.Data.DynamicValueConverter.EnsureConverter(Type sourceType, Type targetType)
at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertToTarget(Object value).

wrote Feb 4, 2009 at 7:03 PM

wrote Feb 19, 2009 at 4:07 PM

theotherjay wrote Feb 19, 2009 at 4:15 PM

I am receiving the same error. I have a similar MVVM design scenario which utilizes the resource selector to bind views to my view models. The error occurs intermittently and is fixed temporarily by switching between views (through application commands while running). The following is my resource selector markup inside my Usercontrol.Resources element:


<data:ResourceSelector x:Key="resourceSelector">
        <data:ResourceSelector.Resources>
            <ResourceDictionary>
                <DataTemplate x:Key="PersonViewModel">
                    <vw:PersonViewC></vw:PersonViewC>
                </DataTemplate>
            </ResourceDictionary>
        </data:ResourceSelector.Resources>
    </data:ResourceSelector>


I have attached text from my output window:

System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value 'SimpleMVVMApp.ViewModels.PersonViewModel' (type 'SimpleMVVMApp.ViewModels.PersonViewModel'); BindingExpression: Path='' DataItem='SimpleMVVMApp.ViewModels.PersonViewModel' (HashCode=15748167); target element is 'System.Windows.Controls.ContentControl' (Name=''); target property is 'ContentTemplate' (type 'System.Windows.DataTemplate').. System.InvalidOperationException: Can't convert type SimpleMVVMApp.ViewModels.PersonViewModel to type System.Windows.DataTemplate.
at MS.Internal.Data.DefaultValueConverter.Create(Type sourceType, Type targetType, Boolean targetToSource)
at MS.Internal.Data.DynamicValueConverter.EnsureConverter(Type sourceType, Type targetType)
at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertToTarget(Object value).

simonferquel wrote Feb 20, 2009 at 4:57 PM

From what I can see, the DataBinding system does not call the converter at all (it is using the DynamicValueConverter instead of the ResourceSelector). Could you please try 2 things :
  • Replace the ResourceSelector by a custom Converter returning a basic DataTemplate and see if it happens as well
  • Make sure that there is no problem during Resource resolution (is the ResourceSelector really accessible from where it is invoked ? are there Resource name collisions ? Is the control using the ressource selector created and firstly loaded in the same scope where it makes use of the resource ?)
If you could send me a repro, it would be very nice of you

simonferquel wrote Feb 20, 2009 at 5:22 PM

Ok, After a couple of tries, I managed to reproduce the bug.
It seems to be related to the order of the instanciation of the resources in a ResourceDictionnary with Silverlight.
In fact, when you refer to a Resource in Silverlight, you have no choice but using a StaticResource markup extension. So the resource is resolved only once. So let's imagine a xaml file like this :
<UserControl>
<UserControl.Resources>
<MyCustomResource x:Key="myRes" />
<DataTemplate x:Key="aTemplate">
   <ContentControl ContentTemplate="{Binding Converter={StaticResource myRes}}" />
</DataTemplate>
</UserControl.Resources>
</UserControl>

What if the DataTemplate is inserted in the Dictionnary before the custom resource ? then the Binding Converter property is set to null. That is what seems to happen in your case (and that also happen in the ShowCase demo of the ResourceSelector). So, how to workaround this ? The better solution is to use the resource selector only in a nested scope :
<UserControl>
<UserControl.Resources>
<MyCustomResource x:Key="myRes" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
  <Grid.Resources>
     <DataTemplate x:Key="aTemplate">
        <ContentControl ContentTemplate="{Binding Converter={StaticResource myRes}}" />
     </DataTemplate>
  </Grid.Resources>
</Grid>
</UserControl>

Thanks, Simon

wrote Feb 20, 2009 at 5:24 PM

theotherjay wrote Feb 20, 2009 at 8:32 PM

I have attached info and a file in the discussion related to this usse.

MihaMarkic wrote Feb 24, 2009 at 8:14 PM

+1

wrote Feb 13, 2013 at 9:39 PM

wrote May 16, 2013 at 2:19 AM