Search code examples
c#visual-studioappkitxamarin.mac

How to change background color of xamarin.mac app?


I have basic xamarin.mac app and I don't know how to change his background color.

// viewcontroller.cs (c#)

using System;

using AppKit;
using Foundation;

namespace Application
{
    public partial class ViewController : NSViewController
    {
        public ViewController (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            // Do any additional setup after loading the view.
            labelOnChange.StringValue = "";
            textFieldOnChange.Changed += LabelChanged;
        }

        public override NSObject RepresentedObject {
            get {
                return base.RepresentedObject;
            }
            set {
                base.RepresentedObject = value;
                // Update the view, if already loaded.
            }
        }

        void LabelChanged(object sender, EventArgs e)
        {
            // get the new value
            labelOnChange.StringValue = textFieldOnChange.StringValue;
        }

        partial void buttonClear(NSObject sender)
        {
            textFieldOnChange.StringValue = "";
            labelOnChange.StringValue = "";
        }
    }
}

Is there simple label which can change bg color, like in Xcode interface builder or I have to write it yourself in viewcontroller.cs (in VS)?

I was try to find NSWindow object which have relation to my project, and to use his backgroundcolor method, but I didn't find him


Solution

  • Just add

    public override void ViewWillAppear()
            {
                base.ViewWillAppear();
    
                this.View.Window.BackgroundColor = NSColor.FromRgb(84, 161, 184);
            }
    

    to your viewcontroller.cs file.

    You also can use FromRgba() if you wanna add alpha channel