仅在设计时使用 DataTemplate 中的 ValueConverter 获取 XamlObjectWriterException(Visual Studio 2015)
Getting an XamlObjectWriterException only at design time with ValueConverter inside DataTemplate (Visual Studio 2015)
我有一个应用程序 运行 在 运行 时完美运行,但在设计时因 集合 属性 'System.Windows.Data.Binding' 而失败。'ConverterParameter' 是空异常。
我想我已经将范围缩小到我在 DataTemplate 内的值转换器中引用 x:array 静态资源(在资源字典中声明,合并到 App.xaml 中)这一事实。
如果我在 DataTemplate 之外使用相同的代码,我就不会遇到这个问题。
我怀疑 具有相同的根本原因。
我重现了问题:
在资源词典中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:trial_app_for_x_array_issue.Resources">
<x:Array x:Key="VisibilityArrayFalseCollapsed" Type="Visibility">
<Visibility>Visible</Visibility>
<Visibility>Collapsed</Visibility>
</x:Array>
在数据转换器中:
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace trial_app_for_x_array_issue.Converters
{
public class BoolToVisibilityMultiParamConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Check for design mode.
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
{
return Visibility.Visible;
}
if (value is bool && targetType == typeof(Visibility))
{
Array arr = parameter as Array;
if (null != arr && arr.Length == 2)
{
bool ValueEqTrue = (bool)value;
if (ValueEqTrue)
{
return arr.GetValue(0);
}
return arr.GetValue(1);
}
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
在主窗口中:
<Window x:Class="trial_app_for_x_array_issue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:trial_app_for_x_array_issue.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<converters:BoolToVisibilityMultiParamConverter x:Key="MultiParamBoolToVisibilityConverter" />
</Window.Resources>
<Grid>
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<TextBlock Name="CurrentActivityPercentageTextBlock"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Hello World"
Visibility="{Binding CurrentActivities.IsIndeterminate,
Converter={StaticResource MultiParamBoolToVisibilityConverter},
ConverterParameter={StaticResource VisibilityArrayFalseCollapsed}}" />
</DataTemplate>
</Expander.HeaderTemplate>
</Expander>
</Grid>
如果我从 DataTemplate 中取出 TextBox,则没有错误。
顺便说一下,如果我使用另一种类型而不是 x:array,也不例外,所以它似乎(至少对我而言)与使用 x:array 资源有关数据模板。
我现在 运行 没主意了...
不幸的是,这看起来像是 XamlLoader 中的错误。它似乎很难链接其各种解析上下文。
不是很干,但是,您可以通过将数组移动到 DataTemplate 资源来修复它。我不确定您对可见性绑定做了什么(如我的评论中所述)所以我将 Expander.Header 属性 设置为适当的值以便为 DataTemplate 建立正确的数据上下文...
<Window x:Class="SO_41650679_2670182.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:SO_41650679_2670182.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<RelativeSource x:Key="View" Mode="FindAncestor"
AncestorType="{x:Type Window}" />
<converters:BoolToVisibilityMultiParamConverter x:Key="MultiParamBoolToVisibilityConverter" />
</Window.Resources>
<StackPanel>
<Expander Header="{Binding RelativeSource={StaticResource View}, Path=CurrentActivities.IsIndeterminate}">
Hello World
<Expander.HeaderTemplate>
<DataTemplate>
<DataTemplate.Resources>
<x:Array x:Key="VisibilityArrayFalseCollapsed" Type="Visibility">
<Visibility>Visible</Visibility>
<Visibility>Collapsed</Visibility>
</x:Array>
</DataTemplate.Resources>
<TextBlock Name="CurrentActivityPercentageTextBlock"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Header"
Visibility="{Binding Converter={StaticResource MultiParamBoolToVisibilityConverter},
ConverterParameter={StaticResource VisibilityArrayFalseCollapsed}}" />
</DataTemplate>
</Expander.HeaderTemplate>
</Expander>
<ToggleButton Name="ToggleButton" Height="30"
IsChecked="{Binding CurrentActivities.IsIndeterminate,
RelativeSource={StaticResource View}}" />
</StackPanel>
</Window>
虽然这消除了设计时错误,但它会导致有关的警告
No default constructor found for type 'System.Windows.Visibility[]'
(我不知道这是什么问题...)并且您的设计时转换器代码路径似乎也不起作用。
我有一个应用程序 运行 在 运行 时完美运行,但在设计时因 集合 属性 'System.Windows.Data.Binding' 而失败。'ConverterParameter' 是空异常。
我想我已经将范围缩小到我在 DataTemplate 内的值转换器中引用 x:array 静态资源(在资源字典中声明,合并到 App.xaml 中)这一事实。
如果我在 DataTemplate 之外使用相同的代码,我就不会遇到这个问题。
我怀疑
我重现了问题:
在资源词典中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:trial_app_for_x_array_issue.Resources">
<x:Array x:Key="VisibilityArrayFalseCollapsed" Type="Visibility">
<Visibility>Visible</Visibility>
<Visibility>Collapsed</Visibility>
</x:Array>
在数据转换器中:
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace trial_app_for_x_array_issue.Converters
{
public class BoolToVisibilityMultiParamConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Check for design mode.
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
{
return Visibility.Visible;
}
if (value is bool && targetType == typeof(Visibility))
{
Array arr = parameter as Array;
if (null != arr && arr.Length == 2)
{
bool ValueEqTrue = (bool)value;
if (ValueEqTrue)
{
return arr.GetValue(0);
}
return arr.GetValue(1);
}
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
在主窗口中:
<Window x:Class="trial_app_for_x_array_issue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:trial_app_for_x_array_issue.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<converters:BoolToVisibilityMultiParamConverter x:Key="MultiParamBoolToVisibilityConverter" />
</Window.Resources>
<Grid>
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<TextBlock Name="CurrentActivityPercentageTextBlock"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Hello World"
Visibility="{Binding CurrentActivities.IsIndeterminate,
Converter={StaticResource MultiParamBoolToVisibilityConverter},
ConverterParameter={StaticResource VisibilityArrayFalseCollapsed}}" />
</DataTemplate>
</Expander.HeaderTemplate>
</Expander>
</Grid>
如果我从 DataTemplate 中取出 TextBox,则没有错误。
顺便说一下,如果我使用另一种类型而不是 x:array,也不例外,所以它似乎(至少对我而言)与使用 x:array 资源有关数据模板。
我现在 运行 没主意了...
不幸的是,这看起来像是 XamlLoader 中的错误。它似乎很难链接其各种解析上下文。
不是很干,但是,您可以通过将数组移动到 DataTemplate 资源来修复它。我不确定您对可见性绑定做了什么(如我的评论中所述)所以我将 Expander.Header 属性 设置为适当的值以便为 DataTemplate 建立正确的数据上下文...
<Window x:Class="SO_41650679_2670182.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:SO_41650679_2670182.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<RelativeSource x:Key="View" Mode="FindAncestor"
AncestorType="{x:Type Window}" />
<converters:BoolToVisibilityMultiParamConverter x:Key="MultiParamBoolToVisibilityConverter" />
</Window.Resources>
<StackPanel>
<Expander Header="{Binding RelativeSource={StaticResource View}, Path=CurrentActivities.IsIndeterminate}">
Hello World
<Expander.HeaderTemplate>
<DataTemplate>
<DataTemplate.Resources>
<x:Array x:Key="VisibilityArrayFalseCollapsed" Type="Visibility">
<Visibility>Visible</Visibility>
<Visibility>Collapsed</Visibility>
</x:Array>
</DataTemplate.Resources>
<TextBlock Name="CurrentActivityPercentageTextBlock"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Header"
Visibility="{Binding Converter={StaticResource MultiParamBoolToVisibilityConverter},
ConverterParameter={StaticResource VisibilityArrayFalseCollapsed}}" />
</DataTemplate>
</Expander.HeaderTemplate>
</Expander>
<ToggleButton Name="ToggleButton" Height="30"
IsChecked="{Binding CurrentActivities.IsIndeterminate,
RelativeSource={StaticResource View}}" />
</StackPanel>
</Window>
虽然这消除了设计时错误,但它会导致有关的警告
No default constructor found for type 'System.Windows.Visibility[]'
(我不知道这是什么问题...)并且您的设计时转换器代码路径似乎也不起作用。