My.Resources in WPF XAML?

Is there any way to access My.Resources through 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: the member "my: Resources" is invalid because it does not have a qualification type name.

The above does not work of course.

NOTE. The converter is for illustration purposes only.




Update:

I was thinking of one idea that might be a good approach, if it works, I created a wrapper class for 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 this:

 <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 the value "MyResources" (enter "Object") from "(enter" Resources "). BindingExpression: Path = MyResources [Ok]; DataItem = 'Resources' (HashCode = 59109011) ; the target element is "Image" (Name = 'btnOk'); target is' Source '(type' ImageSource ') TargetParameterCountException:' System.Reflection.TargetParameterCountException: mismatch of the parameter counter.

By the way, I put a test MessageBox in MyResources Getter, and it seems that the properties are not available at all.

+7
wpf xaml
Sep 14 '09 at 6:52
source share
2 answers

The problem is that, by default, the tool that generates the code for the Resource.resx file is VbMyResourcesResXFileCodeGenerator (the "Custom Tool" property of the project element). This tool generates a module in which the properties of the resource are internal (Friend), so StaticExtension cannot access it. To solve this problem, you should change the custom tool for Resource.resx to PublicVbMyResourcesResXFileCodeGenerator , which will generate open items.

In addition, the VB module is roughly equivalent to a static (Shared) class, so there is no instance of resources that can be used as a binding source, so you cannot specify Path to bind to. You must set the binding source directly to the desired property:

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

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

EDIT: The namespace mapping used is as follows:

 xmlns:myRes="clr-namespace:YourApplicationName.My.Resources" 
+9
Sep 14 '09 at 13:48
source share
— -

Change the access modifier my.resources from friend to public on the resource tab my.project.

+3
Sep 16 '11 at 11:21
source share



All Articles