开发者

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?


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.


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.


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.


Could it be that you have class that is named "View", same as namespace?


WPF doesn't like you using x:Name for a control defined in your assembly, assuming xmlns:views="clr-namespace:MyModule.View" is the same assembly as your

Use name instead, and you will be fine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜