1

I have this in a toggle switch event
LevelPage.change_color() = this.ChangeColorToggle.IsOn;

I am pulling a method with the following code from another page, named LevelPage Moving_Ellipse.Fill = new SolidColorBrush(Colors.Black);

I want the toggle switch even handler to access the method, but it would say that it needs an object for a non-static method. I am pretty new to this stuff, can anyone help me?

JavaCafe01
  • 162
  • 1
  • 9
  • as it says, you need to have an object before you can call a non static method. Only static method can be called without constructing an object of the class. You can use static method or create an object. – Rohit Garg Oct 24 '16 at 04:15

1 Answers1

0

Static vs. Instance

class Human {
    public static readonly string SpeciesName = "Homo sapiens";
    private string Name { get; set; }
}

Static means belonging to a class, intrinsically. For instance, the SpeciesName of Human is "Homo sapiens". This contrasts with the non-static (or instance) property Name. A human, an instance of the Human class, can have a name. An instance of class Human with name Bob makes sense; a Human class with a static Name of Bob would be... interesting.

How to pass state between pages in C# UWP

2 recommended options: Reference

  • Pass data when changing from one page to another via Frame.Navigate().
  • Store your data statically for global access.

    Personally, I prefer the second option. I usually have an object called Model that holds all of my application's persistent state. I store my only instance of Model as a static property of the App class.

    For example, let's say on page1 the user must choose red or blue. Page2 will be that color. So the code would be something like:

    class App : Application {
        public static Model GlobalModel { get; set; }
        //...
    }
    
    class Model {
        public Color UserSelectedColor { get; set; }
    }
    
    class Page1 {
        //...
        private void StoreSelectedColor(Color selectedColor) {
            App.GlobalModel.UserSelectedColor = selectedColor;
        }
        //...
    }
    
    class Page2 {
        //...
        private Color GetSelectedColor() {
            return App.GlobalModel.UserSelectedColor;
        }
        //...
    }
    
  • Community
    • 1
    • 1
    C. McCoy IV
    • 887
    • 7
    • 14