Adding x:Name to a user control gives error in generated code of page its used in
I am basically using a user control for the first time, so hopefully it's just a dumb mistake.
I have a simple user control
<UserControl x:Class="TestProject.WebApp.myUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid>
<sdk:AutoCompleteBox Name="myACB" ItemsSource="{Binding Data}" FilterMode="StartsWith" MinimumPrefixLength="2" >
</sdk:AutoCompleteBox>
</Grid>
</UserControl>
I am using the control in another page (in a stack panel):
<navigation:Page x:Class="TestProject.WebApp.myPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:TestProject.WebApp"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">
<Grid>
<StackPanel Orientation="Horizontal">
<local:myUserCont开发者_如何学Crol x:Name="myUC" />
</StackPanel>
</Grid>
</navigation:Page>
The control is in the same namespace as page. Removing the x:Name allows the project to compile and the control functionally works. When x:Name is specified I get an error in the generated code of the page:
Type 'TestProject.WebApp.myUserControl' is not defined.
It happens on these lines of code:
internal TestProject.WebApp.myUserControl myUC;
this.myUC == (TestProject.WebApp.myUserControl)this.FindName("myUC")
I tried with two different controls. Same thing. The generated code has "using TestProject.WebApp;" so I don't know why there is an error only when I have a named instance of the control.
I also cannot seem to use just the Name property. I even tried overloading the NameProperty in the control.
I was able to find a post about this here:
.g.vb file claims that Type X is not defined, even though it is!
Apparently, there is an issue when a project has a service reference (in my case I am using RIA services) and the user control resides in an assembly with the same root namespace as the one you are trying to use it in.
So for me it was,
TestProject.WebApp
Then I tried creating a new project for just the control
TestProject.Controls
Same problem.
Then, when I renamed the project and changed the namespace to
CustomControls
it worked.
Hopefully this saves someone else from some frustration and I really hope this problem can be fixed by MS soon.
What you are showing here should work. What is the default namespace in your project property's? Also did you rename your user control after you created it. Both of those issues could be causing you problems.
精彩评论