WPF使用datatemplate中的转换器从xaml文本和手机8上的XamlReader创建

我正在使用像这样的文本xaml生成DataTemplate:

var dataTemplate = (DataTemplate)XamlReader.Load(xaml.ToString()); 

其中一个元素是CheckBox与它的转换器绑定,指向StaticResource:

  StringBuilder xaml = new StringBuilder(); xaml.AppendFormat(@" "); 

文本中存在此转换器引用会导致手机上的Application_UnhandledException中捕获到exception。 消息如下所示:

 - InnerException {System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component. at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure_WithDesiredSize(UIElement element, Size availableSize) at System.Windows.UIElement.Measure_WithDesiredSize(Size availableSize) at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(UIElement child, Size layoutSlotSize) at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)} System.Exception 

该模板用于在列表框中创建一行,并且控件在其资源中声明了转换器:

    

我想在某些创建步骤中,xaml->对象的对象创建过程无法找到转换器 – 有没有办法解决它?

**加成

我刚刚测试了为复选框添加了一个Loaded – 与添加转换器相同的断言

 StringBuilder xaml = new StringBuilder(); xaml.AppendFormat(@" <CheckBox"); xaml.AppendFormat(@" IsChecked=""{{Binding [{0}], Mode=TwoWay}}""", header.ID); xaml.AppendFormat(@" Loaded=""FrameworkElement_OnLoaded"""); 

也许有一种方法可以为加载器提供这种上下文?

该模板用于在列表框中创建一行,并且控件在其资源中声明了转换器:

为什么不将数据加载到预加载的列表框中并使用ItemsSource绑定到从文件加载的集合? 并使用可见性转换器来显示/隐藏日期。 你正在做什么似乎在实际上做了什么不必要的复杂。