Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would adding an x:Name attribute to a user control cause a compilation error?

I really need some more ideas as to what's causing this, currently it's driving me up the wall.

I have a Xaml user control which contains another user control like this :


<UserControl x:Class="MyModule.View.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:igDock="http://infragistics.com/DockManager"
    xmlns:views="clr-namespace:MyModule.View"
    >
  <StackPanel x:Name="panel">
    <views:MyHeaderView/>
    <igDock:XamDockManager x:Name="dockingManager"/>
  </StackPanel>
</UserControl>

This is within a Prism module (hence the MyModule name), but otherwise I don't think this is anything particularly unusual.

Now, as soon as I add a name to the header view the code fails to compile, e.g.

<views:MyHeaderView x:Name="header"/>

The error I get back is simply :

"The type name 'View' does not exist in the type MyModule.MyModule'"

I've tried moving the contained user control into another namespace, creating a blank user control and trying with that (same result) and removing the XamDockManager, but nothing seems to make any difference.

Can anyone shed any light on this?

like image 833
adhocgeek Avatar asked Jul 28 '10 10:07

adhocgeek


Video Answer


3 Answers

I think I found the answer.

If I rename the module class to MyModuleThingy, then it compiles. It seems the problem arises because the module class has the same name as the namespace which contains it (MyModule.MyModule).

If anyone can let me know why that might be a problem, I'd be very interested to know. Particularly because I've inherited a lot of code where that seems to be the common pattern within this particular implementation of Prism.

like image 200
adhocgeek Avatar answered Oct 12 '22 20:10

adhocgeek


The issue is you have a class and namespace with the same name. If you have something like:

namespace MyModule {
    public class MyModule {
    }
    public class MyView {
    }
    public class MyControl {
        public void Foo() {
            MyModule.MyView v = ..;
        }
    }
}

The type before v will think MyView is in the MyModule type as a nested type. It does not resolve MyModule as a namespace, since the type wins based on the type resolution rules of C#.

The generated code behind is simply taking the namespaces you define in the xmlns and prepending it when creating the backing field (due to the x:Name attribute). So you end up with MyModule.View.MyHeaderView, where MyModle is interrupted as the type, not the namespace.

like image 36
CodeNaked Avatar answered Oct 12 '22 21:10

CodeNaked


I was unlucky enogh to call my project TextEditor, and apparently the name conflicted with an exiting MS class. My attempts to add a x.Name or Name attribute to a reference to my user control gave me compilation error complaining that compiler can not find my user control. Apparently it was looking into MS class, and not into the local project. I renamed the project into TextEditorProject, had to use Grep to find all the occurances, but eventually, I got it working.

like image 23
user684420 Avatar answered Oct 12 '22 22:10

user684420