Skip to main content

Answered Question Problem with custom controlsRSS Feed

(0)

Snowbunny
Snowbunny

Member

Member

10 points

9 Posts

Problem with custom controls

Hi there.

I have a Silverlight solution containing two projects. One, TestProject is the project containing my main root and the other, SilverlightUIControls is a copy of the same Silverlight controls provided by MS. When the controls project is built, the DLL is copied to the ClientBin directory in TestProject.

In TestProject Page.xaml, I have this line in my <Canvas> declaration:

xmlns:uicontrol = "clr-namespace:Silverlight.Samples.Controls;assembly=ClientBin/Silverlight.Samples.Controls.dll"

and below this, I have:

<uicontrol:ListBox x:Name = "listBox" Canvas.Top="10" Canvas.Left="10"></uicontrol:ListBox >

When I build and test, this works fine, and any changes I make the the ListBox control appear as you'd expect.

Now, I woud like to add a new control, SelectBox. I tried and failed for a while - it always built correctly, but I'd get an error when I tried to view the html (which I'll get to in a minute). So, I thought that I'd go back to basics. I copied the ListBox.cs and ListBox.xaml files as SelectBox.cs and SelectBox.xaml - updated the class declarations and the link to the xaml so it all should work. I included this in my SilverlightUIControls project and built it. No errors. Woohoo.

Going back to my Page.xaml, I looked at the codebehind, and the Intellisense was working fine for the new SelectBox control, so all I did to that <uicontrol:ListBox /> is change it to <uicontrol:SelectBox />. There were no build errors, so I F5ed and, lo and behold, I get the following error:

Silverlight error message
ErrorCode: 2210
ErrorType: ParserError
Message: AG_E_INVALID_ARGUMENT
XamlFile: Page.xaml
Line: 19
Position: 70

The position of the error is the end of that uicontrol. This has been driving me mad. I've checked the DLLs and they're updated with the correct date/time, so I can't for the life of me work out why simply copying the class under a different name would break it!

Any help would save my mind and my hair...and probably my laptop, too, which at the moment is under threat of disappearing off the balcony...

Thanks!

Vivek Dalvi
Vivek Dalvi

Member

Member

458 points

123 Posts

Microsoft

Re: Problem with custom controls

Dont know about laptop but hair...dont make them disappear over and off the balcony, especially if attached to your head  :) Lets see if we can resolve the issue before it gets there.

When you add a new control, SelectBox, does it's constructor  succeeds? Does it even gets called? if you can put a break point and see if that works then it would be helpful to figure out whether the problem is in SelectBox control or something the way ListBox is working or something else.

You dont get errors while compilation because when you put those custom tags in XAML they are not really instantiated at compile time. So that is why you get runtime error.

 

Vivek Dalvi
Program Manager
Microsoft
This post is provided "as-is"

tmasopust
tmasopust

Member

Member

4 points

2 Posts

Re: Problem with custom controls

I'm having the same error and issue. The break point in the constructor never gets hit. The problem appears to be somewhere in the XAML parsing side. 

sony05
sony05

Member

Member

8 points

5 Posts

Re: Re: Problem with custom controls

 im gettin tis error whn im running the code

Silverlight error message
ErrorCode: 2254
ErrorType: Parser Error
Message: AG_E_RUNTIME_MANAGER_ASSEMBLY_DOWNLOAD
XamlFile:xaml/Page.xaml
Line: 10
Position:3

x:Class="SilverlightStarterKit.Page;assembly=ClientBin/SilverlightStarterKit.dll"

xmlns:uicontrol="clr-namespace:Silverlight.Samples.Controls;assembly=ClientBin/Silverlight.Samples.Controls.dll"

tis is my xaml code

and wid the same assembly used in another project its running perfectly well

i hav downloaded the SDk 1.1 alpha also, but nothin seems to working and even in tis project it is not even going to the breakpoint

nirajswami
nirajswami

Member

Member

34 points

16 Posts

Re: Re: Problem with custom controls

Hi Sony5,

I have tried same thing as above. But not get solution of my error.

Any help?

Thanks in Advance.

 

Thanks and Regards,
Nirajswami

Alex Ivanov
Alex Ivanov

Member

Member

18 points

30 Posts

Re: Problem with custom controls

Hi,

 I experienced the same problem (AG_E_INVALID_ARGUMENT error). I was implementing the Control UI from the Silverlight 1.1 SDK. I added the library to a project and the error was with Line and Position on that line:

<my:MyLabel x:Name="label" Height="30" Width="200" Text="Hello" LabelColor="Blue"/>

When I traced the program, I discovered that the fault was one minor omission on my part. I named my library project "MyFirstLibrary" and the default name for the project was "SilverlightSampleControl". I forgot to change the following line in the contructor that created the object:

public MyLabel()
        {
            Stream s = this.GetType().Assembly.GetManifestResourceStream("SilverlightSampleControl.MyLabel.xaml");
            ....

            ....

        }

 Changing the above line to "...MyFirstLibrary.MyLabel.xaml" solved my problem. Now see whether you have not made the same omission in your ListBox constructor.

                                                                                              ~ Alex Ivanov


nirajswami
nirajswami

Member

Member

34 points

16 Posts

Re: Problem with custom controls

Thanks Alex,

My problem has solved.

Thanks and Regards,
Nirajswami

lefnire
lefnire

Member

Member

20 points

13 Posts

Re: Re: Problem with custom controls

I, too, am getting the ag_e_invalid_argument.  i'm just gonna dump all my code here :)  I tried alex's solution, but my namespace was already in the constructor :(

 

Page.xaml

<Canvas xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="client.Page;assembly=ClientBin/client.dll"
        xmlns:my="clr-namespace:client;assembly=ClientBin/client.dll"
        x:Name="parentCanvas"
        Loaded="Page_Loaded"
        Width="640"
        Height="480"
        >
  <my:MyControl x:Name="mycontrol1" Canvas.Left="100" Canvas.Top="100"/>
</Canvas>

 MyControl.xaml

<Canvas
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="MyControl"
    Width="640" Height="480">
</Canvas>

 

MyControl.xaml.cs

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace client
{
    public class MyControl: Control
    {
        public MyControl()
        {
            Stream s = this.GetType().Assembly.GetManifestResourceStream("client.MyControl.xaml");
            this.InitializeFromXaml(new StreamReader(s).ReadToEnd());
        }
    }
}

 

tmasopust
tmasopust

Member

Member

4 points

2 Posts

Re: Re: Problem with custom controls

My issue is resolved. It also traced back to the constructor of the custom control. Inside the custom control's constructor there is a line:

System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("[YourFileName].xaml"); 

When I set a breakpoint in this constructor, "GetManifestResourceStream" was returning null. The subsequent call to “InitializeFromXaml” caused the 2210 ParserError.

After using the ILDASM to view the manifest of the control’s DLL, I noticed that the embedded resource for the xaml file was prefixed with a namespace that I had not placed in the GetManifestResourceStream parameter.

Snowbunny
Snowbunny

Member

Member

10 points

9 Posts

Re: Re: Problem with custom controls

Ah, mine was a different issue - eventually I found out that the Build Action for the XAML wasn't set to 'Embedded Resource' as it should have been.... Fixing this solved my issue.

 

Hope this helps someone.

 

 

nirajswami
nirajswami

Member

Member

34 points

16 Posts

Answered Question

Re: Problem with custom controls

Better to try code given in Silverlight Alpha 1.1 SDK/Control Library Starter kit.

string dotResource = '.' + ResourceName;

Assembly assembly = typeof(ControlBase).Assembly;

string[] names = assembly.GetManifestResourceNames();foreach (string name in names)

{

if (name.Equals(ResourceName) || name.EndsWith(dotResource))

{

resourceStream = assembly.GetManifestResourceStream(name);

break;

}

}

StreamReader sr = new StreamReader(resourceStream);

 

It dynamically resolves issue related to assembly stream reader.

Thanks & regards,

Nirajswami

Thanks and Regards,
Nirajswami

varza
varza

Member

Member

8 points

4 Posts

Re: Re: Problem with custom controls

I'm getting the same error as sony05.

AG_E_RUNTIME_MANAGER_ASSEMBLY_DOWNLOAD

I built a custom control in a Silverlight class library and referenced it from a Silverlight project.

I used Expression Blend to open the xaml file where I used my custom control and I got the error:

The name 'SlidingText' does not exist in the namespace 'clr-namespace:SilverControls;assembly=ClientBin/SilverControls.dll'.

My xaml file from the Sillverligh project references the assembly like this:

xmlns:my="clr-namespace:SilverControls;assembly=ClientBin/SilverControls.dll"

and uses the control like this:

<my:SlidingText Text="This is my text" ForeColor="Red"></my:SlidingText>

Any idea what the problem could be?

 

mario_mh
mario_mh

Member

Member

498 points

117 Posts

Re: Re: Problem with custom controls

the problem with the embedded resource comes when you copy the [FileName].xaml and .cs into the project. so whenever you add an exiting control, "BuildAction" is Silverlightpage, but for a control it should be embedded resource as Snowbunny already mentioned

 furthermore, you need to change the filename in

System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("[ProjectName].[File].xaml");

Mario Meir-Huber
Microsoft Senior Student Partner
Silverlight: www.silverlight-magazin.de

mchlsync
mchlsync

Star

Star

14606 points

2,732 Posts

Silverlight MVP

Re: Problem with custom controls

You can take a look this sample. You can download the sample (zip format)  that is working with Silverlight 1.1 beta.

(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


  • Unanswered Question
  • Answered Question
  • Announcement