0

I have something like this converter with the difference that class is internal and for security the converter belongs to another assembly (MyAssembly.dll):

namespace MyAssembly.InAnotherNameSpace
{
    internal class ValueConvertor : IValueConverter
        {        
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                ...
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                ....
            }
        }
}

And I use InternalsVisibleToAttribute to access the converter from a WPF project:

....
xmlns:c="clr-namespace:MyAssembly.InAnotherNameSpace;assembly=MyAssembly"
....
 <UserControl.Resources>
        <ResourceDictionary>
            <c:ValueConvertor x:Key="valueConvertor"/>
        </ResourceDictionary>
    </UserControl.Resources>

I have a problem in XAML, not recognize the class as internal and Visual Studio show this error:

Only public or internal classes can be used within markup. 'ValueConvertor' type is not public or internal. 

Follow this answer thread, InternalsVisibleTo not working in XAML, that is true?

Ejrr1085
  • 975
  • 2
  • 16
  • 29
  • Why should it not be true, taking that fact into account that you experience the same problem? – Clemens Mar 02 '20 at 21:36
  • @Clemens Because in that answer thread Ângelo Moreira asking: "Are you saying, that InternalsVisibleTo works only for C# but not for XAML?" and nobody affirms if it is true or false, so I suspect that nobody knows the answer, or is a WPF bug. – Ejrr1085 Mar 03 '20 at 00:48

2 Answers2

1

you can apply a workaround - create converter in control code-behind and use it from XAML markup:

public partial class View : UserControl
{
    public static readonly IValueConverter valueConvertor = new MyAssembly.InAnotherNameSpace.ValueConvertor();

    public View()
    {
        InitializeComponent();
    }
}

<TextBlock Text="{Binding Something, Converter={x:Static local:View.valueConvertor}}"/>
ASh
  • 34,632
  • 9
  • 60
  • 82
  • Ok, so I must create public copy properties, because XAML only working with public properties. Nice solution, thank you!. – Ejrr1085 Mar 03 '20 at 16:25
  • @Ejrr1085, note that I used *static* field, which can be accessed via `{x:Static}` extension. Instance properties will require binding, which is not supported inside `{Binding}` expression. And yes, they have to be public. – ASh Mar 03 '20 at 16:42
  • Or you can use `Converter={DynamicResource valueConvertor}` and add converter to control Resources in code-behind, or anywhere really – ASh Mar 03 '20 at 16:44
  • Thank you very match. – Ejrr1085 Mar 03 '20 at 21:20
0

You can use an internal class in another assembly with InternalsVisibleTo(). Confirmed it with VS2019 and .net 4.7.2.

The VS2019 XAML editor complains throws a few errors but runs just fine.

  • XDG0009 - The type "UserControl1" is not accessible.)
  • XLS0419 - Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'WpfControlLibrary2' that could not be found

This seems just a limitation of VS XAML Editor.

user823959
  • 782
  • 2
  • 9
  • 30