WPF 在从手机 8 上的 xaml 文本和 XamlReader 创建的数据模板中使用转换器 [英] WPF using converter in datatemplate created from xaml text and XamlReader on phone 8
问题描述
我正在使用这样的文本 xaml 生成 DataTemplate:
I'm generating a DataTemplate using a text xaml like this:
var dataTemplate = (DataTemplate)XamlReader.Load(xaml.ToString());
其中一个元素是一个 CheckBox,它的转换器指向一个 StaticResource:
One of the elements is a CheckBox bound with it's converter pointing to a StaticResource:
StringBuilder xaml = new StringBuilder();
xaml.AppendFormat(@" <CheckBox");
xaml.AppendFormat(@" IsChecked=""{{Binding [{0}], Converter={{StaticResource stringValueToBoolConverter}}, Mode=TwoWay}}""", header.ID);
xaml.AppendFormat(@" />");
文本中存在此转换器引用会导致在电话上的 Application_UnhandledException 中捕获异常.消息如下所示:
The presence of this converter reference in the text causes an exception caught in the Application_UnhandledException on phone. The message looks like this:
- 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
模板用于在列表框中创建一行,并且控件在其资源中声明了转换器:
The template is used to create a row in a listbox and the control has the converter declared in it's resources:
<UserControl.Resources>
<converters:StringValueToBoolConverter x:Key="stringValueToBoolConverter" />
</UserControl.Resources>
我想在某个创建步骤中,xaml->objects 的对象创建过程找不到转换器 - 有没有办法绕过它?
I suppose that at some creation step, the object creation process of xaml->objects can't find the converter - is there a way to get around it?
**添加
我也刚刚测试了为复选框添加加载 - 与添加转换器相同的断言
I also just tested adding a Loaded for the check box - same assert as adding the converter
StringBuilder xaml = new StringBuilder();
xaml.AppendFormat(@" <CheckBox");
xaml.AppendFormat(@" IsChecked=""{{Binding [{0}], Mode=TwoWay}}""", header.ID);
xaml.AppendFormat(@" Loaded=""FrameworkElement_OnLoaded""");
也许有一种方法可以向加载器提供此上下文?
Maybe there's a way to provide this context to the loader?
推荐答案
模板用于在列表框中创建一行,并且控件在其资源中声明了转换器:
The template is used to create a row in a listbox and the control has the converter declared in it's resources:
为什么不将数据加载到预加载的列表框中并使用 ItemsSource 绑定到从您的文件加载的集合?并使用可见性转换器来显示/隐藏日期.您正在做的事情对于它实际所做的事情来说似乎不必要地复杂.
Why not instead load the data into a preloaded listbox and use ItemsSource to bind to a collection loaded from your file? And use a visibility converter to show/hide date. What you're doing seems needlessly complicated for what it actually does.
这篇关于WPF 在从手机 8 上的 xaml 文本和 XamlReader 创建的数据模板中使用转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!