Skip to main content

Microsoft Silverlight

Answered Question Silverlight 2 Beta Bug?RSS Feed

(0)

arshadm
arshadm

Member

Member

20 points

7 Posts

Silverlight 2 Beta Bug?

Hi,

 I am trying to write a library of usercontrols that the rest of my applications can use but have come across a problem. Let's say I create a user control called MyControl and then I create a subclass of that (a standard class) then we I try and create an instance of the new class I get the following error

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll

Additional information: AG_E_PARSER_BAD_TYPE [Line: 4 Position: 29]

From reading on the internet I gather this is because the x:Class in the XAML does not match the type to Application.LoadComponent in InitializeComponent. In my case the argument to Application.LoadComponent is the subclass, but I don't see why that should cause a problem per se.

Any ideas why this behaviour is implement in Silverlight.

Regards.

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight 2 Beta Bug?

Someone posted about that in this forum earlier. I tried two different approach in User control inheritance.

1)

  • BaseControl = It is inherited from UserControl. It's just a standard class that doesn't have XAML.
  • InheritedControl = It is inherited from Base Control. It has XAML. 

http://michaelsync.net/2008/03/24/silverlight-2-beta1-user-control-inheritance 

2)

  • BaseControl = It is inherited from User Control. It's just a standard class that doesn't have XAML.
  • MyControl = It is inherited from BaseControl. It is also just a standard class that doesnt have XAML.

Both ways are working fine. Are you using other different way? Can you show us your code?

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


arshadm
arshadm

Member

Member

20 points

7 Posts

Re: Silverlight 2 Beta Bug?

Hi,

I am trying to use your first solution but I am still not getting it to work. Here are the files (main snippet, just missing using clauses).

SimplyActivity.xaml:

<UserControl x:Class="SmartkidsLibrary.Controls.SimpleActivity"

xmlns="http://schemas.microsoft.com/client/2007"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="550" Height="400">

<Canvas x:Name="LayoutRoot" Background="White">

<TextBlock x:Name="ScoreText" Text="Score"></TextBlock>

<TextBlock x:Name="QuestionText" Text="Question" Canvas.Top="100" Canvas.Left="20" VerticalAlignment="Bottom" Width="250" Height="250"></TextBlock>

<TextBlock x:Name="FeedbackText" Canvas.Top="270" Canvas.Left="20" Text="Feedback"></TextBlock>

<Canvas x:Name="Diagram"></Canvas>

<TextBox x:Name="AnswerInput" Canvas.Top="230" Canvas.Left="70" Width="400"></TextBox>

</Canvas>

</UserControl>

SimpleActivity.xaml.cs:

namespace SmartkidsLibrary.Controls {

/// <summary>

/// This class defines the ui/event handling for simple activities

/// </summary>

public partial class SimpleActivity : UserControl {

/// <summary>

/// The name of the activity

/// </summary>

public String name { get; set; }

/// <summary>

/// The activity parameters

/// </summary>

public String parameters { get; set; }

/// <summary>

/// Create and initialise a new instance

/// </summary>

public SimpleActivity() {

InitializeComponent();

}

}

}

Sublcass SilverlightControl1.xaml:

<SimpleActivity x:Class="SmartkidsLibrary.Controls.SilverlightControl1"

xmlns="http://schemas.microsoft.com/client/2007"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="400" Height="300">

</SimpleActivity>

SilverlightControl1.xaml:

namespace SmartkidsLibrary.Controls {

public partial class SilverlightControl1 : SimpleActivity {

public SilverlightControl1() {

InitializeComponent();

}

}

}

 Here is code where I create an instance of the subclass and then add it as a child to a canvas in my application:

UserControl control = new SilverlightControl1();

LayoutRoot.Children.Add(control);

As mentioned this gives an exception as follows:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll

Additional information: System.Windows.Markup.XamlParseException: AG_E_PARSER_BAD_TYPE [Line: 4 Position: 29]

The only thing I didn't do which was mentioned in your blog is add anything to the assemblies, but I thought that was if I wanted add the name to a namespace so that I could use it as child.

Regards.

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight 2 Beta Bug?

 Hi,

I have tested your code. It's working fine. (I think there are some typo errors in yourposts)

You can download the code from this link. http://michaelsync.net/demo/SilverlightApplication10.zip


(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


arshadm
arshadm

Member

Member

20 points

7 Posts

Re: Silverlight 2 Beta Bug?

Hmm, your code works if the subclass is treated as a control via the assembly.

But, if I add the following line to Page.xaml.cs then it throws the error I mention, and I am not sure why anymore because it works when it's used via the Xaml but not in code.

public Page() {

InitializeComponent();

UserControl control = new SilverlightControl1();

LayoutRoot.Children.Add(control);

Canvas.SetTop(50);

Canvas.SetLeft(50);

 

I changed the Page.xaml to remove the mention of the control to :-

<Grid x:Name="LayoutRoot" Background="White">

 

</Grid>

This should be doing in C# code what the xaml is doing, but it throws the exception I mentioned above.

Regards.

arshadm
arshadm

Member

Member

20 points

7 Posts

Re: Silverlight 2 Beta Bug?

Sorry, syntax error in code

Canvas.SetTop(control, 50);

Canvas.SetLeft(control, 50);

But still gives same exception.

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight 2 Beta Bug?

You are right. Adding the control from code is giving you this problem... Is it possible for you to add the control in XAML? (Like I did in my sample)

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


arshadm
arshadm

Member

Member

20 points

7 Posts

Re: Silverlight 2 Beta Bug?

Actually, it doesn't work from the XAML either because afterwards I noticed that Page.xaml was uing ctl:SimplyActivity rather than the subclass ctl:SilverlightControl1, when I changed that it gave the same error.

I think I can understand why this might not be allowed in the current circumstances because it doesn't really make sense to have two separate XAML in the class hierarchy (which one is the real user interface because there can only be one root control in each user control).

My original requirement though was much simpler and shouldn't cause a problem as far as I can see, I wanted a base class with XAML. This base class may be abstract, a derived class would then provide the actual implementation for the event handlers and other logic. I think this would be a very useful feature, but obviously since I am a Silverlight newbie it might open up a can of worms which I don't understand.

Regards.

Necroman
Necroman

Member

Member

20 points

7 Posts

Re: Silverlight 2 Beta Bug?

I use different way, when inheriting objects:

3)

  • BaseControl = It is inherited from User Control. It has XAML.
  • MyControl = It is inherited from BaseControl. It is a class that doesnt have XAML. Additional controls and methods are added in C# code in constructor.

In classic WPF it works without a problem, but in Silverlight 2 I still get the error, when I try to instantiate MyControl.

MyControl control = new MyControl();

LayoutRoot.Children.Add(control);

It crashes with AG_E_PARSER_BAD_TYPE when calling InitializeComponents() in BaseControl.
Just try it and you'll surely get the same error message.
 

Btw. In your example, http://michaelsync.net/demo/SilverlightApplication10.zip you are adding the Base class <ctl:SimplyActivity /> into Page, try adding the inherited class <ctl:SilverlightControl1 /> and you get the exact same error.

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight 2 Beta Bug?

 Hello,

The problem is that we can't have XAML in Base Control. Is it possible for you not to use XAML in BaseControl?  

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Re: Silverlight 2 Beta Bug?

Hello, this is a known issue. It's true that if the base UserControl is declared in XAML, the sub UserControl can only use code. But due to some internal parser issues, currently even if the sub UserControl only uses code, you'll get errors. Our developers are tracking this issue. But sorry I can't give you more details at this time...

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Re: Silverlight 2 Beta Bug?

Yi-Lun Luo - MSFT:
Hello, this is a known issue. It's true that if the base UserControl is declared in XAML, the sub UserControl can only use code. But due to some internal parser issues, currently even if the sub UserControl only uses code, you'll get errors. Our developers are tracking this issue. But sorry I can't give you more details at this time...
 

Thanks for your reply. Yi-Lun. Yes 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


makkros
makkros

Member

Member

14 points

7 Posts

Re: Re: Silverlight 2 Beta Bug?

Hi

what about the solution of this problem? Does this problem is solved in the future release? Actually I can't run my silverlight application and I need to kwow it in order to plan the update that application.

Thanks in advance 

 

makkros
makkros

Member

Member

14 points

7 Posts

Re: Re: Silverlight 2 Beta Bug?

Hi all,

does anybody has found a workaround to this problem?

Yi-Lun Luo - MSFT:

It's true that if the base UserControl is declared in XAML, the sub UserControl can only use code. But due to some internal parser issues, currently even if the sub UserControl only uses code, you'll get errors.

 

 I have urgently to solve it, otherwise I have to change all my application logic.

thanks

peterdungan
peterdungan

Member

Member

223 points

150 Posts

Re: Re: Re: Silverlight 2 Beta Bug?

I'm getting this error after trying to update a beta 2 project to rtw.

It worked fine in beta 2.

Should it also wrk ok in rtw, or does it indicate the update didn't work properly?

makkros
makkros

Member

Member

14 points

7 Posts

Re: Re: Re: Silverlight 2 Beta Bug?

Strange, because with the final release it worked for me .

I have the base UserControl declared in XAML and the sub UserControl in the code only.

peterdungan
peterdungan

Member

Member

223 points

150 Posts

Re: Re: Re: Re: Silverlight 2 Beta Bug?

Sorry I found that the error I was getting was because of some differences in xaml syntax when I upgraded - it wasn't due to this bug

Bijoy Thangaraj
Bijoy Th...

Member

Member

29 points

29 Posts

Re: Re: Re: Re: Re: Silverlight 2 Beta Bug?

What were those differences?

Thanks and Regards,
Bijoy Thangaraj
JSplash | Google Gadgets

Play hundreds of free games at JoyRack Games

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities