Problem when loading XAP file dynamically in during the runtime.-Collection of common programming errors
msdnHi all,
I am developing an application with multiple XAP files. I have two XAP filesc – A.xap and B.xap.
I am loading A.xap first and want to load B.xap dynamically. But it results in an exception – “Exception has been thrown by the target of an invocation.” at the line marked as bold and italics.
I pasting below the code I am using and the exception details that I got.
Code
——-
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
CallSharepointSvcClient loginSvc = new CallSharepointSvcClient();loginSvc.IsAuthenticatedCompleted += new EventHandler(loginSvc_IsAuthenticatedCompleted);
loginSvc.IsAuthenticatedAsync(txtUID.Text, txtPwd.Password);
}
void loginSvc_IsAuthenticatedCompleted(object sender, IsAuthenticatedCompletedEventArgs e)
{
if (e.Result == “Authorized”)
{
WebClient wb = new WebClient();
wb.OpenReadAsync(
new Uri(“B.xap”,UriKind.Relative));
wb.OpenReadCompleted +=
new OpenReadCompletedEventHandler(wb_OpenReadCompleted);
}
}
void wb_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
StreamResourceInfo sri = new StreamResourceInfo(e.Result, null); StreamResourceInfo mainAppSRI = Application.GetResourceStream(sri, new Uri(“B.dll”, UriKind.Relative));AssemblyPart loader = new AssemblyPart(); Assembly assembly = loader.Load(mainAppSRI.Stream);this.LayoutRoot.Children.Clear(); this.Content = assembly.CreateInstance(“B.Page”) as System.Windows.UIElement;
}
The exception detais – Stack trace
System.Reflection.TargetInvocationException was unhandled by user code Message=”Exception has been thrown by the target of an invocation.” StackTrace: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Reflection.Assembly.CreateInstance(String typeName) at LoginApp.Page.wb_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e) at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) at System.Net.WebClient.OpenReadOperationCompleted(Object arg) InnerException: System.IO.FileNotFoundException Message=”Could not load file or assembly ‘CairngormFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The system cannot find the file specified.” StackTrace: at VideoLibrary.Page..ctor() InnerException:
Please throw some light into this matter and help me.
Thanks,
msdn1Hi,
I figured out the problem. There was two dependencies for my project. I had to load them manually before creating the instance of the main page. That was the problem.
Thanks,
msdn2Hi,
Thank u for the reply. I tried that as well with the following code…
string appManifest = new StreamReader(Application.GetResourceStream(new StreamResourceInfo(e.Result, null), new Uri(“AppManifest.xaml”, UriKind.Relative)).Stream).ReadToEnd();XElement deploymentRoot = XDocument.Parse(appManifest).Root; List deploymentParts = (from assemblyParts in deploymentRoot.Elements().Elements()select assemblyParts).ToList();UIElement dashShell = null; foreach (XElement xElement in deploymentParts)
{
string source = xElement.Attribute(“Source”).Value;StreamResourceInfo streamInfo = Application.GetResourceStream(new StreamResourceInfo(e.Result, “application/binary”), new Uri(source, UriKind.Relative));AssemblyPart asmPart = new AssemblyPart();if (source == “VideoLibrary.dll”)
{
Assembly asm = asmPart.Load(streamInfo.Stream);
dashShell = asm.CreateInstance(
“VideoLibrary.Page”) as UIElement;
}
else
{
asmPart.Load(streamInfo.Stream);
}
}
this.LayoutRoot.Children.Clear(); this.LayoutRoot.Children.Add(dashShell);
But the line which is marked in bold and italics creates the same problem.
If anybody could provide a light into this…
Thanks
msdn3Hi,
I figured out the problem. There was two dependencies for my project. I had to load them manually before creating the instance of the main page. That was the problem.
Thanks,