Converting WPF application project into UserControl Library causes InitializeComponent() can not be find error
I was working on the project where I was creating a bunch of 开发者_JAVA百科User Controls. At the later time I have decided to move those controls into a UserControl library. So that's what I did:
- Created a new VS2010 project (User Control library).
- Set the .NET version to 3.5
- Created a bunch of folders
- Added files from the previous project to my library
However when I try to build my library, I get the following error:
Error 14 'EChart.Controls.EPreviewGraphSelector' does not contain a
definition for 'InitializeComponent' and no extension method 'InitializeComponent'
accepting a first argument of type 'EChart.Controls.EPreviewGraphSelector'
could be found (are you missing a using directive or an assembly reference?)
C:\Projects\eControls\EChart\EChart\Controls\EPreviewGraphSelector.xaml.cs 73 18 EChart
Namespace of my XAML file and code behind do match. The whole project when it was an application rather than a library was building fine.
Here is the simplified code for my UserControl:
<UserControl x:Class="EChart.Controls.EPreviewGraphSelector">
Code behind:
namespace EChart.Controls
{
public partial class EPreviewGraphSelector : UserControl
{
public EPreviewGraphSelector()
{
InitializeComponent();
}
}
}
- g.cs file doesn't get generated for this user control !
- Build Action set to page.
- I have all necessary references added
- User control Build tool is set to MSBuild:Compile
EDIT:
I have found the possible place where it fails to compile:
If I comment out all code apart from constructors, g.cs file for my usercontrol gets generated, however it comes with an error:
Error 2 The type or namespace name 'Windows' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) C:\Projects\eControls\EChart\EChart\obj\Debug\Controls\EPreviewGraphSelector.g.cs 15 17 EChart
this comes from this part of g.cs
using Microsoft.Windows.Themes;
Any ideas why is that happening?
Actually I have sold the issue, I am not sure why it occurs, but I know hot to fix it.
In my UserControl I had a following reference:
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"
This was probably added by Blend, when I was styling some standard controls. When I am in the design mode I don't get any error messages. So when I compile my userControl, I get g.cs files where my InitializeComponent method is generated. Then there I get the reference to Microsoft.Windows.Themes;
, because of that reference I had in my xaml (see above). And when it compiles the g.cs file, then it fails, so the g.cs file is not generated and no InitializeComponent methods exists, which result in that error.
I guess you have not set up the project correctly. I suggest to look at the setup of your WPF application, such as references, and copy them to your library. Is the build tool of the user control set to MSBuild:Compile?
Have you checked the namespaces of the existing files you have added to the new Project? I could imagine that there are inconsistencies or different namespaces that don't fit, or namespaces in xaml don't match the namespaces in the class definitions.
精彩评论