'InitializeComponent' is not declared - Expression Blend Error

4199
9
08-25-2010 03:41 PM
MickClifford
New Contributor
Hi,

I'm new to Silverlight, and am trying to make use of the code available on the ArcGIS Resource Center.  I am using Microsoft Expression Blend 4 to create a simple map using the ESRI online services.

I have created a new Silverlight Application and Website project in Expression Blend, and then added the code from the Resource Center (http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Navigation) into the XAML and XAML.VB pages.  I have used the project name for the Namespace, rather than 'ArcGISSilverlightSDK', which is in the online code.

I can view the XAML in the Expression Blend Design view, but when I build the project I get the following error:

Description:  'InitializeComponent' is not declared.  It may be inaccessible due to its protection level.
File:  MainPage.xaml.vb
Project:  SilverlightApplication_Web

The XAML code is:
<UserControl x:Class="SilverlightApplication_Web.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
    <Grid x:Name="LayoutRoot" Background="White">

<esri:Map x:Name="MyMap" Extent="-130,10,-70,60">
                    <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
                    Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
             </esri:Map>
 
  <esri:Navigation Margin="5" HorizontalAlignment="Left" VerticalAlignment="Bottom"
                         Map="{Binding ElementName=MyMap}"  >   
     </esri:Navigation>
     </Grid>
</UserControl>


The XAML.VB code is:
Imports Microsoft.VisualBasic
Imports System.Windows.Controls

Namespace SilverlightApplication_Web
Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

End Class
End Namespace

Any help would be great!!  Thanks in advance.

Mick
0 Kudos
9 Replies
JenniferNery
Esri Regular Contributor
This error usually occurs when the namespace for Xaml and Code-behind do not match. Have you tried building this in VS2010?  Do you get the same compile error?  Can you check that all project references are there (there should be warning icon next to them if they are not)? Can you also check project properties for namespace?
0 Kudos
MickClifford
New Contributor
Hi Jennifer

Thanks for your reply.

I downloaded an evaluation copy of VS2010, attempted to build my project and got the same error message:  'InitializeComponent' is not declared.  It may be inaccessible due to its protection level.  However, further informaiton was:

Generate method stub for 'InitializeComponent' in 'SilverlightApplication_Web.SilverlightApplication_Web.MainPage'

All references seemed to be fine - without warning icons.

In the .xaml.vb page, the error offered a 'fix error' option.  When selected the code below was added:


        Private Sub InitializeComponent()
            Throw New NotImplementedException
        End Sub

When I rebuild the project, it worked!!

Thank you very much for your advise.

Mick
0 Kudos
JenniferNery
Esri Regular Contributor
I think adding that code will just hide the reason for the compile error.

Below is actually most helpful error message.
Generate method stub for 'InitializeComponent' in 'SilverlightApplication_Web.SilverlightApplication _Web.MainPage'


As you can see "SilverlightApplication_Web" was written twice. I suspect that your XAML only has one but in your code-behind, you may have defined this twice.
0 Kudos
MickClifford
New Contributor
Hi ,

The fix in VS2010 did just suppress the error.  The double naming of "SilverlightApplication_Web" (SilverlightApplication_Web.SilverlightApplication_Web.MainPage) still has me confused.

I think it is because of the file structure rather than it being defined twice.  The solution file sits on my C drive with the file structure:  C:\...\SilverlightApplication_Website\

Also in this folder is another folder called SilverlightApplication_Web (which contains my App.xaml and MainPage.xaml, SilverlightApplication_Web.vbproj files), and one called SilverlightApplication_WebSite (which contains the Default.html and Web.Config files).

I assume that I have set things up incorrectly, and this may be causing my problems.

I've also noticed that if I remove 'Namespace SilverlightApplication_Web' from my .XAML.VB file, the error goes away in VS2010.  When I run the project, I get a new error:  A security exception occured while trying to connect to the REST endpoint.  I have tried putting the ClientAccessPolicy.xml in each of the three folders, and in fact all of them too, and also in the WWWROOT directory.  It doesn't seem to make a difference.

Could someone please advise what the root directory? 

Thanks again,

Mick
0 Kudos
JenniferNery
Esri Regular Contributor
Did you get to resolve this? If not, I suggest starting a new Silverlight application using VisualStudio (instead of Expression Blend) and then updating the XAML and code-behind with the sample.  However, when updating the default XAML and code-behind, do not overwrite these parts, keep the default class and namespace definition that was created by VisualStudio:

XAML:
<UserControl x:Class="ArcGISSilverlightSDK.ToolBarWidget"

Code-behind:
Namespace ArcGISSilverlightSDK
  Partial Public Class ToolBarWidget
    Inherits UserControl
0 Kudos
ElizabethMiller
New Contributor III
Did anyone find a way to resolve this problem? I am also trying to set up a basic map using the sample code and am getting many errors of this nature (i.e." 'InitializeComponent' is not declared," "'MyMap' is not declared," etc.) When I mouse over the errors in Visual Web Developer 2010Express, I get the error correction options that list the name of my project twice, for example:

"Generate method stub for 'InitializeComponent' in 'SilverlightApplication1.SilverlightApplication1.MainPage'"

How can I fix this?

Thanks for your help!
0 Kudos
DominiqueBroux
Esri Frequent Contributor
In VB (unlike in C#), the rootnamespace and the namespace embedding the classes are cumulative.

So if the rootnamespace of your project is 'SilverlightApplication1' (right click on the project --> properties --> look at Rootnamespace) and your classes are surrounded with a 'SilverlightApplication1' namespace, the full path of your class becomes 'SilverlightApplication1.SilverlightApplication1.MyClass.
So in XAML you have to see :
<UserControl x:Class="SilverlightApplication1.SilverlightApplication1.MainPage" ........


My suggestion would be to remove the Namespace from your class and so to keep
<UserControl x:Class="SilverlightApplication1.MainPage" ........
0 Kudos
ElizabethMiller
New Contributor III
Dominique,

If I understand you correctly, you are saying to remove the Namespace code from my xaml.vb file?

I have done this: I have removed

Namespace SilverlightApplication1
...
End Namespace

From my xaml.vb file.

This has fixed the problem! That's great for now -- but will it create problems for me in the future? Why did ESRI put the Namespace in the xaml.vb code sample if it doesn't work? I am probably still missing something.

Thanks again.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
This has fixed the problem! That's great for now -- but will it create problems for me in the future?

No problem, the important point is that the class name declared in the xaml file be in sync with the full classname (rootnamespace + namespace surrounding the class + class name) of the VB project.


Why did ESRI put the Namespace in the xaml.vb code sample if it doesn't work?

It works if you have no rootnamespace for your project.
I guess this has been done to keep VB code close to C# code (and for a C# project there is no rootnamespace).
0 Kudos