views:

906

answers:

1

Hello!

Is there a way to access My.Resources thru Xaml?

Like this

<Image Source="{Binding MyImage,
    Source={x:Static my:Resources},
    Converter={StaticResource MyBitmapToImageSourceConverter}}" />

This is the error I get: \Application.xaml(122,36): error MC3029: 'my:Resources' member is not valid because it does not have a qualifying type name.

The above doesn't work of course.

NOTE: The converter is for illustration only.


Update:

I thought about 1 idea, which might be a good approach if it will work, I created a wrapper class for the resources:

Public Class Resources
    Public Shared ReadOnly m_Resources As New Resources
    Public Shared ReadOnly Property Resources() As Resources
        Get
            Return m_Resources
        End Get
    End Property

    Public ReadOnly Property MyResources(ByVal name As String) As Object
        Get
            Return My.Resources.ResourceManager.GetObject(name)
        End Get
    End Property
End Class

And then in my binding I try to access it like so:

<Setter Property="ImageSource"
Value="{Binding MyResources[Ok], Source={x:Static src:Resources.Resources}}"/>

But I still get the following message:

System.Windows.Data Error: 16 : Cannot get 'MyResources' value (type 'Object') from '' (type 'Resources'). BindingExpression:Path=MyResources[Ok]; DataItem='Resources' (HashCode=59109011); target element is 'Image' (Name='btnOk'); target property is 'Source' (type 'ImageSource') TargetParameterCountException:'System.Reflection.TargetParameterCountException: Parameter count mismatch.

BTW, I placed a test MessageBox in the MyResources Getter, and it seems the property is not accessed at all.

+1  A: 

The problem is that by default, the tool that generates code for the Resources.resx file is VbMyResourcesResXFileCodeGenerator ("Custom tool" property of the project item). This tool generates a Module in which the resource properties are internal (Friend), so the StaticExtension can't access it. To solve that problem, you should change the custom tool for Resources.resx to PublicVbMyResourcesResXFileCodeGenerator, which will generate public members.

Also, a VB module is roughly equivalent to a static (Shared) class, so there is no instance of Resources that could be used as the source of the binding, so you can't specify a Path for the binding. You should set the binding source directly to the property you want :

<Image Source="{Binding Source={x:Static my:Resources.MyImage},
    Converter={StaticResource MyBitmapToImageSourceConverter}}" />

Note: there is another pair of tools available to generate the code for a resource file : ResXFileCodeGenerator and PublicResXFileCodeGenerator. These tools generate a class instead of a module.

EDIT: The namespace mapping to use is the following :

xmlns:myRes="clr-namespace:YourApplicationName.My.Resources"
Thomas Levesque
I checked and the generator was PublicVbMyResourcesResXFileCodeGenerator.Anyhow, I don't think the problem from security reasons, Friend is accessible from within the assembly. I think the problem is My.Resources is a namespaces while My.Resource.TheResource is a property, there ain't no class here, take a look at he error I quote above and you will see that this is the reason.Do you have any further ideas?
Shimmy
There's *always* a class in .NET... well, in that case that's a module, but it's almost the same as a static class. I think your namespace mapping is incorrect, see my updated answer
Thomas Levesque
You're the Man.I thought since the Module is hidden (HideModuleNameAttribute), it won't work.What I've learn from here is always give it a shot, even you won't kill anything...
Shimmy
The thing that worked is the "EDIT: The namespace mapping to use is the following :"
Shimmy