Solve the problem of image path loading in the xap package through reflection

Source: Internet
Author: User

Image Path Problems

The recently developed Silverlight project framework design is to make each module into a separate Silverlight project. The xap packages generated by these independent projects are stored in the parent SilverlightProgramMedium

Reflection implements loading. In actual development, the default path for adding images with blend is "\ 123.jpg ". After compilation, the image is packaged into xap as the content.In the image path

Path starting with "\". When Silverlight is running, files are searched from the parent Silverlight xap package and displayed.The image in the xap package loaded by reflection cannot find the path.

Solution

To solve this problem, we need to set the image in the reflected xap package as a resource, and then change the image path "\ 123.jpg" to "123.jpg ".

If "\" is not found in the image path, the image will be searched from the resource when Silverlight is running.

 

Xap package reflection loading class

Code

 Public class xaploader
{
Public static usercontrol loadstream (Stream E, string mainpage, string maindll)
{
String appmainfest = new streamreader (application. getresourcestream (
New streamresourceinfo (E, null)
, New uri ("appmanifest. XAML", urikind. Relative). Stream
). Readtoend ();

Xelement Xe = (xdocument. parse (appmainfest). root;

VaR Results = from deploymentpart IN Xe. Elements (). Elements ()
Where deploymentpart. attributes ("Source"). Count ()> 0
Select deploymentpart. Attribute ("Source"). value;

Assembly ASSE = NULL;

Foreach (VAR deploymentpart in results)
{
String source = deploymentpart;

Streamresourceinfo streaminfo = application. getresourcestream (New streamresourceinfo (E, "application/binary "),
New uri (source, urikind. Relative ));

Assemblypart ASSP = new assemblypart ();

If (Source = maindll)
{
ASSE = ASSP. Load (streaminfo. Stream );
}
Else
{
ASSP. Load (streaminfo. Stream );
}
}

Return ASSE. createinstance (mainpage) as usercontrol;
}

Public void loaduri (URI Xapuri, string mainpage, string maindll)
{
WebClient WC = new WebClient ();
WC. openreadcompleted + = new openreadcompletedeventhandler (S, e) => {
String appmainfest = new streamreader (application. getresourcestream (
New streamresourceinfo (E. Result, null)
, New uri ("appmanifest. XAML", urikind. Relative). Stream
). Readtoend ();

Xelement Xe = (xdocument. parse (appmainfest). root;

VaR Results = from deploymentpart IN Xe. Elements (). Elements ()
Where deploymentpart. attributes ("Source"). Count ()> 0
Select deploymentpart. Attribute ("Source"). value;

Assembly ASSE = NULL;

Foreach (VAR deploymentpart in results)
{
String source = deploymentpart;

Streamresourceinfo streaminfo = application. getresourcestream (New streamresourceinfo (E. result, "application/binary "),
New uri (source, urikind. Relative ));

Assemblypart ASSP = new assemblypart ();

If (Source = maindll)
{
ASSE = ASSP. Load (streaminfo. Stream );
}
Else
{
ASSP. Load (streaminfo. Stream );
}
}

Uielement uie = ASSE. createinstance (mainpage) as uielement;


If (onloadcomplete! = NULL)
Onloadcomplete (this, new xapeventargs () {xaprootuielement = uie });
});
WC. openreadasync (Xapuri );
}

Public event eventhandler < Xapeventargs > Onloadcomplete;
}

Public class xapeventargs: eventargs
{
Public uielement xaprootuielement {Get; set ;}
}

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.