0

Somehow I came across an issue I cannot figure out.

Assuming we have a View:

public partial class TestView : UserControl
    {
        public TestView(TestViewModel vm)
        {
            InitializeComponent();

            DataContext = vm;
        }
    }

And the injected ViewModel:

 class TestViewModel
{
    public TestViewModel()
    {

    }
}

This logically results in an error:

Inconsistent accessibility: parameter type 'TestViewModel' is less accessible than method 'TestView.TestView'

So when I want to declare my View and ViewModel as internal:

internal class TestViewModel
    {
        internal TestViewModel()
        {

        }
    }

This works:

partial class TestView : UserControl
    {
        TestView(TestViewModel vm)
        {
            InitializeComponent();

            DataContext = vm;
        }
    }

But this doesn't:

internal partial class TestView : UserControl
    {
        internal TestView(TestViewModel vm)
        {
            InitializeComponent();

            DataContext = vm;
        }
    }

Why?

steveax
  • 17,527
  • 6
  • 44
  • 59
Johannes Wanzek
  • 2,825
  • 2
  • 29
  • 47
  • what error do you get? – NeddySpaghetti Apr 25 '14 at 08:02
  • Partial declarations of 'TestView' have conflicting accessibility modifiers. But where and why? I thought a class is automatically internal when there is no modifier. – Johannes Wanzek Apr 25 '14 at 08:03
  • There are plenty of similar questions if you want the reason. [Example one](http://stackoverflow.com/questions/9726974/parameter-is-less-accessible-than-method), [example two](http://stackoverflow.com/questions/6229504/inconsistent-accessibility-parameter-type-is-less-accessible-than-method). – icebat Apr 25 '14 at 08:21
  • This is not the answer to my Question. The `internal` modifier should be optional, but if i change it it causes an error. – Johannes Wanzek Apr 25 '14 at 08:25

1 Answers1

1

You get an error because xaml consider your class as public where as your code behind says it is internal. Add this to your xaml x:ClassModifier="internal"

<UserControl x:Class="YourNamespace.TestView " x:ClassModifier="internal"
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Thank you for your answer! But Why does it work without any modifier? Shouldn't this be the same like `internal`? – Johannes Wanzek Apr 25 '14 at 08:15
  • @JohannesWanzek By default [x:ClassModifier is public](http://msdn.microsoft.com/en-us/library/ms754029%28v=vs.110%29.aspx) – Sriram Sakthivel Apr 25 '14 at 08:18
  • Yeah `x:ClassModifier` is public in XAML, But in My code I don't declare any modifiere, which means it should be internal by default: http://stackoverflow.com/questions/2521459/what-are-the-default-access-modifiers-in-c – Johannes Wanzek Apr 25 '14 at 08:22
  • Yes, you're right. If you don't declare in xaml and code behind it will be internal – Sriram Sakthivel Apr 25 '14 at 08:30