1

I have a really simple WPF converter:

internal class FlatTreeMarginConverter : IValueConverter
{
    #region Fields

    private static readonly Thickness _emptyThickness = new Thickness(0, 0, 0, 0);

    #endregion

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        FlatTreeViewItem tvi = value as FlatTreeViewItem;

        if (tvi == null)
            return _emptyThickness;

        int nLevel = -1;

        while (tvi != null)
        {
            tvi = tvi.GetVisualAncestor<FlatTreeViewItem>();
            nLevel++;
        }

        return new Thickness(19 * nLevel, 0, 0, 0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

For some reason, the declaration in the Xaml has a blue squiggle under it with the exception "Object reference not set to instance.". The declaration just looks like:

I don't get it... everything in my code is safe. Any ideas? Or just some random VS bug?

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86

2 Answers2

1

I don't see where the "Object reference not set to instance." could come from in the code you are showing. So it must come from somewhere else...

There is a neet trick to debug and understand where the "Object reference not set to instance." comes from.

You can attach one instance of visual studio to the your current visual studio instance using the "Attach to Process" (and selecting devenv.exe).
Take a look at Attach debugger onto another Visual Studio instance.
Don't forget to tick "Thrown" for all exceptions in DEBUG/Exeptions after doing "Attach to Process"

Once you have done that you open your Xaml file.
The exception "Object reference not set to instance." will be thrown and caught by the other instance of visual studio allowing you to see where it came from. Allowing you to debug your code.

PilouPili
  • 2,601
  • 2
  • 17
  • 31
  • Hmm... interesting... I'll try this today and report back. – SledgeHammer Dec 21 '18 at 14:21
  • Didn't work. No exceptions caught even though the blue squiggle appears. Only thing I saw in the output was: devenv.exe Warning: 0 : Returning NoOpUIThread instance from GetUIThread – SledgeHammer Dec 21 '18 at 16:00
  • Are you trying with the designer or is it just thé intellisense that gives you the exception. Also are you sure you checked "thrown" everywhere in debug\exceptions in the debugging instance of visual studio (not thé one throwing thé exception) – PilouPili Dec 21 '18 at 17:09
  • the blue squiggle is shown in the XAML editor (not the visual designer, this is a resource dictionary, so hand edit as I do with all my XAML). Yes, I checked all the C# and C++ exceptions on the 2nd instance. Nothing except for the debug output I posted above. – SledgeHammer Dec 21 '18 at 17:38
  • as I mentioned to the other responder, if I make the converter public, the squiggle goes away, but its a converter that's for internal use only. I have dozens of other internal converters and none of them squiggle. – SledgeHammer Dec 21 '18 at 17:40
0

You have public methods in an internal class. Make the class public, clean and rebuild. Public methods/properties are downgraded to internal in an internal class.

Whatever processes xaml is not totally reliable and it's fairly common to see spurious blue squigglies like that.
It uses reflection to check whether converters and the like are in an exe. This is why you need to compile before a converter will lose squigglies. It's my understanding that this means converters should be public.

It's also fairly common for VS to hold on to old definitions of stuff unless you explicitly clean.

Once you change to public clean and rebuild... does it spin up OK?

BTW I also recommend the markupextension approach which allows you to use converters without making them a resource in xaml.

For example:

public class IsOfTypeConverter : MarkupExtension, IValueConverter
{
    public Type TypeToCompare { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.GetType() == TypeToCompare;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

Can be used:

   <DataTrigger Binding="{Binding ., Converter={ui:IsOfTypeConverter TypeToCompare={x:Type ui:PlaceVM}}}" Value="True">
Andy
  • 11,864
  • 2
  • 17
  • 20
  • I have public properties? I have a private property. This is a converter I use internally in a custom control in a DLL. I don't want it visible outside of the DLL. I have dozens of other converters that are set up EXACTLY the same way and this is the only one that squiggles. Odd though, if I make it public the squiggle goes away :(. – SledgeHammer Dec 21 '18 at 14:21