简体   繁体   中英

XAML designer shows error after program has run and does not display correctly

I have a weird error that the XAML designer displays when hovering over the text that gets the blue underline. It also refuses to show the components correctly in the preview. The text is simply

Object reference not set to an instance of an object.

That looks like a NullReferenceException , but I have no clue where it comes from. It displays correctly in the launched app.

It actually seems to be related to inheriting from List<string> AND exposing a settable property. If I remove either of that it works. But I want both for my converter.

To reproduce it, simply create an empty WPF .NET Framework project, and paste this below the MainWindow.xaml.cs code inside the namespace:

    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;
    }

and this into the 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>

Keep MainWindow.xaml open and then launch the app via the Start button. You can see a green and a red square. Since this is hardcoded in the XAML, I'd expect the designer to show just that.

Instead, when you exit the app, the designer shows the color hexcodes instead of the colored square, seemingly because it has problems with the converter setup.

在此处输入图片说明

What's the problem?

The code provided by you is working fine at my end.

Just try to build your project to see if that null reference error goes way.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM