Forum Xamarin.iOS


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

NSBezierPath.FromOvalInRect(rect) throws InvalidCastException for NSLabelCellView (NSView subclass)?

pmhart83pmhart83 USMember ✭✭✭
edited July 2015 in Xamarin.iOS

What? This makes NO since to me at all. In fact when I add a Console.WriteLine("...") right before the call to NSBezierPath.FromOvalInRect(rect), it no longer crashes! I haven't seen errors like this since working with flash? The Console.WriteLine leads me to believe this is some kind of thread / timing issue?? The draw is invoked by SetNeedsDisplayInRect(Bounds). Even more confusing, the NSLabelCellView is only used in an entirely different window and this button object is only used in one other area not related to NSLabelCellView in any way.


  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Hmm. It is hard to tell with limited context, but it could be a few things:

    a) There could be an issue with the binding where an object not being kept alive. Dump this code somewhere before and see if it shakes things up:

    new System.Threading.Thread (() =>
    while (true) {
    System.Threading.Thread.Sleep (1000);
    GC.Collect ();
    }).Start ();

    b) Maybe you are doing something from the wrong thread? Confirm all of your drawing calls are on the main thread.

    c) What Console.WriteLine "fixes" the issue?

    d) Can you please post the full stack trace.

    e) Does your NSCellLabelView correctly have an IntPtr constructor and call base on all of its constructors?

    f) If none of the above, posting an example may help (less guessing).

  • pmhart83pmhart83 USMember ✭✭✭
    edited July 2015

    a) still crashes

    b) I did the following: InvokeOnMainThread(() => SetNeedsDisplayInRect(Bounds)); Also just tried adding InvokeOnMainThread(() => ...) around all the draw code.

    c) Console.WriteLine("rect = [{0},{1},{2},{3}]", rect.X, rect.Y, rect.Width, rect.Height);


    [External Code]

    Followed by:

    System.InvalidCastException: Unable to cast object of type 'Musicnotes.Mac.NSLabelCellView' to type 'AppKit.NSBezierPath'
    at ObjCRuntime.Runtime.GetNSObject[NSBezierPath] (IntPtr ptr) [0x00121] in /Users/builder/data/lanes/1880/248c95e2/source/maccore/src/ObjCRuntime/Runtime.cs:799
    at AppKit.NSBezierPath.FromOvalInRect (CGRect rect) [0x00016] in /Users/builder/data/lanes/1880/248c95e2/source/maccore/src/build/mac/mobile/AppKit/NSBezierPath.g.cs:648
    at Musicnotes.Mac.NSCircleImageButton.DrawRect (CGRect dirtyRect) [0x0005a] in /Users/Paul/Code/musicnotes_smv-xamarin/MusicnotesMac/Code/UI/Controls/NSCircleImageButton.cs:78
    at at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[])
    at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /Users/builder/data/lanes/1880/248c95e2/source/maccore/src/AppKit/NSApplication.cs:94
    at Musicnotes.Mac.MainClass.Main (System.String[] args) [0x00007] in /Users/Paul/Code/musicnotes_smv-xamarin/MusicnotesMac/Code/Main.cs:13

    e) yes, it's used in a table and was crashing during implemention before adding that

    f) its really hard to post an example given the windows are rich with UI! Sorry I know that is not much help. I will think about it some more.


  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai
    • Ok, so that stack trace shows that you are on the main thread, since it has AppKit.NSApplication.Main in it.
    • That CWL should have no difference, since it is just referencing rect, which is used on the path line anyway.
    • Posting a copy (or sending to me) of NSLabelCellView might be useful (I'd guess something is screwy there)?
  • pmhart83pmhart83 USMember ✭✭✭


    Well so I came up with a round about way of solving this it seems. I save the path to a member variable then only update it on the "ResizeSubviewsWithOldSize" event.

    I know it's not answering the question what the heck is going on but it works so far.

Sign In or Register to comment.