Silverlight trap: Pay attention to Assembly reference issues

Source: Internet
Author: User

Assume that I want to use the Silverlight class library to implement some general controls and thenProgramReference this control library. Of course, controls usually need to access other third-party or open-source development packages, such as Silverlight toolkit.

The dependency of this project is as follows: Silverlight application => Silverlight control => Silverlight toolkit. Create reference relationships between projects in Visual Studio:

 

 

Create a simple control in the class library project, for example:

 

< Usercontrol X: Class = "Sllib. testcontrol"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"  
Xmlns: controlstoolkit = "CLR-namespace: system. Windows. controls; Assembly = system. Windows. Controls. toolkit"  
>
< Grid X: Name = "Layoutroot" >
< Controlstoolkit: dockpanel >
</ Controlstoolkit: dockpanel >
</ Grid >
</ Usercontrol >

 

Finally, add the control we just created in the application:

< Usercontrol X: Class = "Testsl. mainpage"
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: Lib = "CLR-namespace: sllib; Assembly = sllib" >

< Grid X: Name = "Layoutroot" >
< Lib: testcontrol />
</ Grid >

</ Usercontrol >

 

Such a simple program (one lineCodeNo). Is it impossible to have a problem? Unfortunately, this is not actually the case:

 

 

Why? Open the. xap file and you will find that the Toolkit assembly is not included! In this way, the dockpanel class cannot be found during the control operation, and the program will naturally go wrong.

 

 

We can verify this error from other aspects. Delete the original control (in fact, you can do it without deleting it) and create a control from the code:

Public   Class Testcontrol2: contentcontrol
{
Public Testcontrol2 ()
{
This . Content =   New Dockpanel ();
}
}

Then, replace testcontrol in the program with testcontrol2. How can I try again? Running properly !. The xap file also contains the Toolkit:

 

 

On the other hand, if we manually add system. Windows. Controls. toolkit in the application reference, the program can also run normally.

 

These indications indicate that the Silverlight compiler is a bit self-intelligent. Even if we explicitly specify the assembly to be referenced in the class library reference, the compiler will ignore these instructions and only look for the ones used in the code. The compiler does not care about the Assembly you reference in. XAML. To make the situation worse, if the class cannot be found during the runtime, The Silverlight runtime will only throw a bad-name ag_e_parser_bad_type. This error message without content is of no help in searching for problems. The strange thing is that for an application-type project, the Silverlight compiler practices are completely different-as long as any assembly is added to the project reference, whether it is actually used or not, it will be compiled to the final. xap file. You should be careful with such inconsistent behaviors.

 

The simplest work around und of this problem is: as long as the Assembly is referenced in the class library and the same assembly is referenced in the application, errors can be avoided. Obviously, this is not an ideal solution. Not only does it force programmers to repeat some meaningless work, but it also forces the class library users to care about the internal mechanism of the class library, this greatly reduces the significance of the existence of class libraries.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.