8,757
社区成员
发帖
与我相关
我的任务
分享
行: 1
错误: Unhandled Error in Silverlight 2 Application Could not create instance of requested type. 位于 XapPreloader.XapLoader.wc_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
位于 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
位于 System.Net.WebClient.OpenReadOperationCompleted(Object arg)
public class XapLoader
{
public event EventHandler<XapLoaderEventArgs> XapLoaded;
private string m_rootAssembly;
private string m_typeName;
public void LoadXap(Uri xapUri, string typeName)
{
m_rootAssembly = Path.GetFileNameWithoutExtension(xapUri.ToString());
m_typeName = typeName;
WebClient wc = new WebClient();
wc.OpenReadCompleted += wc_OpenReadCompleted;
wc.OpenReadAsync(xapUri);
}
private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var manifestStream = Application.GetResourceStream(
new StreamResourceInfo(e.Result, null),
new Uri("AppManifest.xaml", UriKind.Relative));
string appManifest = new StreamReader(manifestStream.Stream).ReadToEnd();
string assemblyName = m_rootAssembly + ".dll";
XmlReader reader = XmlReader.Create(new StringReader(appManifest));
Assembly asm = null;
while (reader.Read())
{
if (reader.IsStartElement("AssemblyPart"))
{
reader.MoveToAttribute("Source");
reader.ReadAttributeValue();
if (reader.Value == assemblyName)
{
var assemblyStream = new StreamResourceInfo(e.Result, "application/binary");
var si = Application.GetResourceStream(assemblyStream, new Uri(reader.Value, UriKind.Relative));
AssemblyPart p = new AssemblyPart();
asm = p.Load(si.Stream);
break;
}
}
}
if (asm == null)
throw new InvalidOperationException("Could not find specified assembly.");
var o = asm.CreateInstance(m_typeName);
if (o == null)
throw new InvalidOperationException("Could not create instance of requested type.");
RaiseXapLoadedEvent(o);
}
private void RaiseXapLoadedEvent(object instance)
{
if (XapLoaded != null)
{
XapLoaded(this, new XapLoaderEventArgs(instance));
}
}
}
public class XapLoaderEventArgs : EventArgs
{
public object Instance { get; set; }
public XapLoaderEventArgs(object instance)
{
this.Instance = instance;
}
}