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 ;}
}