locked
How do I access a method/control on another page? RRS feed

  • Question

  • I have added a Settings Pane to my app it has it own Page called Settings.XAML  This shows as a popup up via the charms.

    I have toggle switch in the settings and when it is toggled I want it to run a method in the MainPage.XAML that will immediately change the colour of some items on that page but I cannot access the method or any controls on the MainPage from the Settings.XAML.

    I have tried making the method static and then the Settings.XAML can see it but then I get warning for every object in that method saying "An object reference is required for the non-static field, method, or property"

    The method in question is:

    public void ChangeClapperboardColour()
    {
        SolidColorBrush BlackBrush = new SolidColorBrush(Colors.Black);
        SolidColorBrush WhiteBrush = new SolidColorBrush(Colors.White);
    
        MainGrid.Background = BlackBrush;
        textblockCamNumber.Foreground = WhiteBrush;
        textblockDirector.Foreground = WhiteBrush;
        textblockDP.Foreground = WhiteBrush;
        textblockFrameRate.Foreground = WhiteBrush;
        textblockMovieName.Foreground = WhiteBrush;
        textblockRoll.Foreground = WhiteBrush;
        textblockScene.Foreground = WhiteBrush;
        textblockTake.Foreground = WhiteBrush;
        textboxShootDate.Foreground = WhiteBrush;
        rect1.Stroke = WhiteBrush;
        rect2.Stroke = WhiteBrush;
        rect3.Stroke = WhiteBrush;
        rect4.Stroke = WhiteBrush;
        rect5.Stroke = WhiteBrush;
        rect6.Stroke = WhiteBrush;
        rect7.Stroke = WhiteBrush;
        rect8.Stroke = WhiteBrush;
        rect9.Stroke = WhiteBrush;
        rectTimecode.Stroke = WhiteBrush;
        textblockEXT.Foreground = WhiteBrush;
        textblockINT.Foreground = WhiteBrush;
       textblockMOS.Foreground = WhiteBrush;
        textblockNITE.Foreground = WhiteBrush;
      }


    • Edited by qualar1 Sunday, December 23, 2012 10:59 AM typo
    Sunday, December 23, 2012 10:58 AM

Answers

  • static things (methods/properties/events) are at the class level. There is only one instance of them at a time and they can be accessed by everyone (an instance of the class, other classes...).

    non-static things on the other hand (like a 'private int number;' property) can only be accessed from the instance they are defined in. That's why you can access a non-static field or property (like rect8.Stroke) from a static method.

    The easiest solution would be to define a static event on your SettingsPane class:

    //OnValueChanged
    public delegate void AddOnValueChangedDelegate(string parameter);
    public static event AddOnValueChangedDelegate OnValueChanged;
    OnMyValueChanged;
    public void OnValueChangedEvent(string parameter)
    {
         if (OnValueChanged != null)
             OnValueChanged(parameter);
    }

    As the event is static, it can be called from inside the SettingsPane instance. (OnValueChangedEvent("foo");)

    In your MainPage constructor you can add an event handler to the static event:

    public MainPage()
    {
        this.InitializeComponent();
        SettingsPane.OnValueChanged += SettingsPane_OnValueChanged;
    }
    
    private void SettingsPane_OnValueChanged(string parameter)
    {
        //do something
    }

    have fun^

    ~theCake


    Life is unsure - always eat the dessert first!

    • Marked as answer by Jesse Jiang Tuesday, December 25, 2012 7:37 AM
    Sunday, December 23, 2012 2:28 PM

All replies

  • You would need a reference to the displayed form to be able to run a method on it.  An easier method might be to bind the foreground and strokes to a property in a class that does the INotifyPropertyChanged interface.  Once the property changes the colors should update on the form   

    Sunday, December 23, 2012 2:13 PM
  • static things (methods/properties/events) are at the class level. There is only one instance of them at a time and they can be accessed by everyone (an instance of the class, other classes...).

    non-static things on the other hand (like a 'private int number;' property) can only be accessed from the instance they are defined in. That's why you can access a non-static field or property (like rect8.Stroke) from a static method.

    The easiest solution would be to define a static event on your SettingsPane class:

    //OnValueChanged
    public delegate void AddOnValueChangedDelegate(string parameter);
    public static event AddOnValueChangedDelegate OnValueChanged;
    OnMyValueChanged;
    public void OnValueChangedEvent(string parameter)
    {
         if (OnValueChanged != null)
             OnValueChanged(parameter);
    }

    As the event is static, it can be called from inside the SettingsPane instance. (OnValueChangedEvent("foo");)

    In your MainPage constructor you can add an event handler to the static event:

    public MainPage()
    {
        this.InitializeComponent();
        SettingsPane.OnValueChanged += SettingsPane_OnValueChanged;
    }
    
    private void SettingsPane_OnValueChanged(string parameter)
    {
        //do something
    }

    have fun^

    ~theCake


    Life is unsure - always eat the dessert first!

    • Marked as answer by Jesse Jiang Tuesday, December 25, 2012 7:37 AM
    Sunday, December 23, 2012 2:28 PM
  • Thank you very much for your help.   Just what I needed.
    Monday, December 24, 2012 12:45 AM