How to draw a rectangle in xamarin.mac










2















I'm trying to draw a rectangle in the xamarin.mac framework. It seems like this can be accomplished with the CoreGrpahics namespace but I'm not sure how it hooks up with xamarin. For example



 NSColor.Black.Set();
NSBezierPath.StrokeLine(new CGPoint(-10.0f, 0.0f), new CGPoint(10.0f, 0.0f));


Does not make anything appear on the screen, when i believe it should make a single line appear. This seems trivial in the other Xamarin. frameworks as there are built in functions available, but the xamarin.mac documentation is very sparse.










share|improve this question






















  • What is the context of your StrokeLine? i.e. is it being done in a DrawRect override?

    – SushiHangover
    Nov 14 '18 at 23:40















2















I'm trying to draw a rectangle in the xamarin.mac framework. It seems like this can be accomplished with the CoreGrpahics namespace but I'm not sure how it hooks up with xamarin. For example



 NSColor.Black.Set();
NSBezierPath.StrokeLine(new CGPoint(-10.0f, 0.0f), new CGPoint(10.0f, 0.0f));


Does not make anything appear on the screen, when i believe it should make a single line appear. This seems trivial in the other Xamarin. frameworks as there are built in functions available, but the xamarin.mac documentation is very sparse.










share|improve this question






















  • What is the context of your StrokeLine? i.e. is it being done in a DrawRect override?

    – SushiHangover
    Nov 14 '18 at 23:40













2












2








2








I'm trying to draw a rectangle in the xamarin.mac framework. It seems like this can be accomplished with the CoreGrpahics namespace but I'm not sure how it hooks up with xamarin. For example



 NSColor.Black.Set();
NSBezierPath.StrokeLine(new CGPoint(-10.0f, 0.0f), new CGPoint(10.0f, 0.0f));


Does not make anything appear on the screen, when i believe it should make a single line appear. This seems trivial in the other Xamarin. frameworks as there are built in functions available, but the xamarin.mac documentation is very sparse.










share|improve this question














I'm trying to draw a rectangle in the xamarin.mac framework. It seems like this can be accomplished with the CoreGrpahics namespace but I'm not sure how it hooks up with xamarin. For example



 NSColor.Black.Set();
NSBezierPath.StrokeLine(new CGPoint(-10.0f, 0.0f), new CGPoint(10.0f, 0.0f));


Does not make anything appear on the screen, when i believe it should make a single line appear. This seems trivial in the other Xamarin. frameworks as there are built in functions available, but the xamarin.mac documentation is very sparse.







c# xamarin xamarin.mac






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 23:20









Dave FolDave Fol

132




132












  • What is the context of your StrokeLine? i.e. is it being done in a DrawRect override?

    – SushiHangover
    Nov 14 '18 at 23:40

















  • What is the context of your StrokeLine? i.e. is it being done in a DrawRect override?

    – SushiHangover
    Nov 14 '18 at 23:40
















What is the context of your StrokeLine? i.e. is it being done in a DrawRect override?

– SushiHangover
Nov 14 '18 at 23:40





What is the context of your StrokeLine? i.e. is it being done in a DrawRect override?

– SushiHangover
Nov 14 '18 at 23:40












2 Answers
2






active

oldest

votes


















0














Welcome! Glad to see more Xamarin.Mac users.



@SushiHangover hinted at it but you need to be in a valid draw context. Pardon if I'm over-explaining, but custom drawing like you're discussing is often done in a NSView, typically by overriding a view's DrawRect(CGRect dirtyRect) method. That method is inherently called by AppKit within the proper graphics context. So your code would work fine if called within that method of a view. Keep in mind that those drawing methods are called very often and must be efficient.



If you were to use a CGPath instead of a NSBezierPath, you need to add that path to the context by calling NSGraphicsContext.CurrentContext.CGContext.addPath(path).



I've made a little Xamarin workbook titled "MacOS Custom Drawing" for ya here that shows both ways: https://github.com/NickSpag/Workbooks. I'd also recommend Workbooks for drawing practice and testing, as they make it very easy to continually and quickly reload your code.






share|improve this answer
































    1














    this code from the Xamarin docs draws a triangle, but should give you the basic idea. The CoreGraphics API should be the same for iOS and Mac, so an example for one should easily translate to the other



    //get graphics context
    using (CGContext g = UIGraphics.GetCurrentContext ())

    //set up drawing attributes
    g.SetLineWidth (10);
    UIColor.Blue.SetFill ();
    UIColor.Red.SetStroke ();

    //create geometry
    var path = new CGPath ();

    path.AddLines (new CGPoint
    new CGPoint (100, 200),
    new CGPoint (160, 100),
    new CGPoint (220, 200));

    path.CloseSubpath ();

    //add geometry to graphics context and draw it
    g.AddPath (path);
    g.DrawPath (CGPathDrawingMode.FillStroke);






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310230%2fhow-to-draw-a-rectangle-in-xamarin-mac%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Welcome! Glad to see more Xamarin.Mac users.



      @SushiHangover hinted at it but you need to be in a valid draw context. Pardon if I'm over-explaining, but custom drawing like you're discussing is often done in a NSView, typically by overriding a view's DrawRect(CGRect dirtyRect) method. That method is inherently called by AppKit within the proper graphics context. So your code would work fine if called within that method of a view. Keep in mind that those drawing methods are called very often and must be efficient.



      If you were to use a CGPath instead of a NSBezierPath, you need to add that path to the context by calling NSGraphicsContext.CurrentContext.CGContext.addPath(path).



      I've made a little Xamarin workbook titled "MacOS Custom Drawing" for ya here that shows both ways: https://github.com/NickSpag/Workbooks. I'd also recommend Workbooks for drawing practice and testing, as they make it very easy to continually and quickly reload your code.






      share|improve this answer





























        0














        Welcome! Glad to see more Xamarin.Mac users.



        @SushiHangover hinted at it but you need to be in a valid draw context. Pardon if I'm over-explaining, but custom drawing like you're discussing is often done in a NSView, typically by overriding a view's DrawRect(CGRect dirtyRect) method. That method is inherently called by AppKit within the proper graphics context. So your code would work fine if called within that method of a view. Keep in mind that those drawing methods are called very often and must be efficient.



        If you were to use a CGPath instead of a NSBezierPath, you need to add that path to the context by calling NSGraphicsContext.CurrentContext.CGContext.addPath(path).



        I've made a little Xamarin workbook titled "MacOS Custom Drawing" for ya here that shows both ways: https://github.com/NickSpag/Workbooks. I'd also recommend Workbooks for drawing practice and testing, as they make it very easy to continually and quickly reload your code.






        share|improve this answer



























          0












          0








          0







          Welcome! Glad to see more Xamarin.Mac users.



          @SushiHangover hinted at it but you need to be in a valid draw context. Pardon if I'm over-explaining, but custom drawing like you're discussing is often done in a NSView, typically by overriding a view's DrawRect(CGRect dirtyRect) method. That method is inherently called by AppKit within the proper graphics context. So your code would work fine if called within that method of a view. Keep in mind that those drawing methods are called very often and must be efficient.



          If you were to use a CGPath instead of a NSBezierPath, you need to add that path to the context by calling NSGraphicsContext.CurrentContext.CGContext.addPath(path).



          I've made a little Xamarin workbook titled "MacOS Custom Drawing" for ya here that shows both ways: https://github.com/NickSpag/Workbooks. I'd also recommend Workbooks for drawing practice and testing, as they make it very easy to continually and quickly reload your code.






          share|improve this answer















          Welcome! Glad to see more Xamarin.Mac users.



          @SushiHangover hinted at it but you need to be in a valid draw context. Pardon if I'm over-explaining, but custom drawing like you're discussing is often done in a NSView, typically by overriding a view's DrawRect(CGRect dirtyRect) method. That method is inherently called by AppKit within the proper graphics context. So your code would work fine if called within that method of a view. Keep in mind that those drawing methods are called very often and must be efficient.



          If you were to use a CGPath instead of a NSBezierPath, you need to add that path to the context by calling NSGraphicsContext.CurrentContext.CGContext.addPath(path).



          I've made a little Xamarin workbook titled "MacOS Custom Drawing" for ya here that shows both ways: https://github.com/NickSpag/Workbooks. I'd also recommend Workbooks for drawing practice and testing, as they make it very easy to continually and quickly reload your code.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 17:53

























          answered Nov 15 '18 at 0:57









          NickSpagNickSpag

          1967




          1967























              1














              this code from the Xamarin docs draws a triangle, but should give you the basic idea. The CoreGraphics API should be the same for iOS and Mac, so an example for one should easily translate to the other



              //get graphics context
              using (CGContext g = UIGraphics.GetCurrentContext ())

              //set up drawing attributes
              g.SetLineWidth (10);
              UIColor.Blue.SetFill ();
              UIColor.Red.SetStroke ();

              //create geometry
              var path = new CGPath ();

              path.AddLines (new CGPoint
              new CGPoint (100, 200),
              new CGPoint (160, 100),
              new CGPoint (220, 200));

              path.CloseSubpath ();

              //add geometry to graphics context and draw it
              g.AddPath (path);
              g.DrawPath (CGPathDrawingMode.FillStroke);






              share|improve this answer



























                1














                this code from the Xamarin docs draws a triangle, but should give you the basic idea. The CoreGraphics API should be the same for iOS and Mac, so an example for one should easily translate to the other



                //get graphics context
                using (CGContext g = UIGraphics.GetCurrentContext ())

                //set up drawing attributes
                g.SetLineWidth (10);
                UIColor.Blue.SetFill ();
                UIColor.Red.SetStroke ();

                //create geometry
                var path = new CGPath ();

                path.AddLines (new CGPoint
                new CGPoint (100, 200),
                new CGPoint (160, 100),
                new CGPoint (220, 200));

                path.CloseSubpath ();

                //add geometry to graphics context and draw it
                g.AddPath (path);
                g.DrawPath (CGPathDrawingMode.FillStroke);






                share|improve this answer

























                  1












                  1








                  1







                  this code from the Xamarin docs draws a triangle, but should give you the basic idea. The CoreGraphics API should be the same for iOS and Mac, so an example for one should easily translate to the other



                  //get graphics context
                  using (CGContext g = UIGraphics.GetCurrentContext ())

                  //set up drawing attributes
                  g.SetLineWidth (10);
                  UIColor.Blue.SetFill ();
                  UIColor.Red.SetStroke ();

                  //create geometry
                  var path = new CGPath ();

                  path.AddLines (new CGPoint
                  new CGPoint (100, 200),
                  new CGPoint (160, 100),
                  new CGPoint (220, 200));

                  path.CloseSubpath ();

                  //add geometry to graphics context and draw it
                  g.AddPath (path);
                  g.DrawPath (CGPathDrawingMode.FillStroke);






                  share|improve this answer













                  this code from the Xamarin docs draws a triangle, but should give you the basic idea. The CoreGraphics API should be the same for iOS and Mac, so an example for one should easily translate to the other



                  //get graphics context
                  using (CGContext g = UIGraphics.GetCurrentContext ())

                  //set up drawing attributes
                  g.SetLineWidth (10);
                  UIColor.Blue.SetFill ();
                  UIColor.Red.SetStroke ();

                  //create geometry
                  var path = new CGPath ();

                  path.AddLines (new CGPoint
                  new CGPoint (100, 200),
                  new CGPoint (160, 100),
                  new CGPoint (220, 200));

                  path.CloseSubpath ();

                  //add geometry to graphics context and draw it
                  g.AddPath (path);
                  g.DrawPath (CGPathDrawingMode.FillStroke);







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 0:44









                  JasonJason

                  52k1290118




                  52k1290118



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310230%2fhow-to-draw-a-rectangle-in-xamarin-mac%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto