将鼠标悬停在带有蓝色下划线的文本上时,XAML 设计器会显示一个奇怪的错误。它还拒绝在预览中正确显示组件。文字很简单
Object reference not set to an instance of an object.
这看起来像一个NullReferenceException
,但我不知道它来自哪里。它在启动的应用程序中正确显示。
它实际上似乎与继承List<string>
AND 公开可设置属性有关。如果我删除其中任何一个,它就会起作用。但我想要两者都用于我的转换器。
要重现它,只需创建一个空的 WPF .NET Framework 项目,并将其粘贴MainWindow.xaml.cs
到命名空间内的代码下方:
public class BuggyConverter : List<string>, IMultiValueConverter
{
public object Value { get; set; }
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> Visibility.Visible;
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) => null;
}
这进入MainWindow.xaml
:
<Window...>
<Window.Resources>
<local:BuggyConverter x:Key="conv" Value="{x:Static Brushes.Yellow}" />
</Window.Resources>
<Grid>
<Border Visibility="{MultiBinding Converter={StaticResource conv}}" />
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Visibility="{MultiBinding Converter={StaticResource conv}}" Width="100" Height="100" Background="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsSource>
<x:Array Type="{x:Type Brush}">
<SolidColorBrush Color="Green" />
<SolidColorBrush Color="Red" />
</x:Array>
</ItemsControl.ItemsSource>
</ItemsControl>
</Grid>
</Window>
保持MainWindow.xaml
打开状态,然后通过“开始”按钮启动应用程序。你可以看到一个绿色和一个红色的方块。由于这在 XAML 中是硬编码的,因此我希望设计人员能够展示这一点。
相反,当您退出应用程序时,设计器会显示颜色十六进制代码而不是彩色方块,这似乎是因为转换器设置存在问题。
有什么问题?