Problem:
I need to load loose xaml dynamically and create control that will contain UserControl defined in loose xaml
Solution:
uc.xaml
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><UserControl.Resources> <ControlTemplate ><Border /></ControlTemplate></UserControl.Resources><Grid></Grid></UserControl>
loading method:
using (var sr = new StreamReader("uc.xaml")) { var xamlSchemaContext = new XamlSchemaContext(); var xmlReaderSettings = new XamlXmlReaderSettings(); var xamlReader = new XamlXmlReader(sr, xamlSchemaContext, xmlReaderSettings); var activityBuilder = XamlServices.Load(xamlReader); }
the error I get:
InnerException: System.ArgumentException
Message=Object of type 'System.Windows.Controls.Border' cannot be converted to type 'System.Windows.FrameworkElementFactory'.
Source=mscorlib
StackTrace:
at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Xaml.Schema.SafeReflectionInvoker.InvokeMethodCritical(MethodInfo method, Object instance, Object[] args)
at System.Xaml.Schema.SafeReflectionInvoker.InvokeMethod(MethodInfo method, Object instance, Object[] args)
at System.Xaml.Schema.XamlMemberInvoker.SetValueSafeCritical(Object instance, Object value)
at System.Xaml.Schema.XamlMemberInvoker.SetValue(Object instance, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
does anybody have any idea how to make this code work or overcome anyhow?
thanks in advance