0

I made a switch on which the program should change all backgrounds of gieven elements. Therefore I made a new changer-class (View Model) which accesses every xaml file and changes the background color:

TheView1.xaml: (partial)

<ScrollView
    x:Name="myScrollView" [...]

TheView1.xaml.cs: (partial)

void switch_Toggled(object sender, ToggledEventArgs e)
    {
        if (themeSwitch.IsToggled)
        {
           ChangeBackground.ChangeColor();
        }
    }

ChangeBackgrounds.cs

public static class ChangeBackground
{
    TheView1 tv1 = new TheView1();
    public static void ChangeColor()
    {            
        tv1.myScrollView.BackgroundColor = [...];
        tv1.myButton.BackgroundColor = [...];
    }
}

VS then says "tv1 does not contain a definition for myScrollView and myButton". But they are clearly there, why does´nt it work?

Best_Where_Gives
  • 481
  • 2
  • 22
  • In this way you got a new instance of TheView1 in your ChangeBackground class, but you need a reference to existing TheView1 – Luca Oct 19 '15 at 08:37
  • yes Pass it over (even it's not a good approach) – Med.Amine.Touil Oct 19 '15 at 08:43
  • Why not share your approach as possible answer? – Best_Where_Gives Oct 19 '15 at 08:47
  • If you are not using MVVM pattern, then you have to create a public method which will set the background colour and then use the public method in your ChangeBackground.cs to change it. Your scroll view is a private object and so it cannot be accessed from outside the code behind file. – Krishna Oct 19 '15 at 10:44

1 Answers1

1

If you are working with WPF try it with the MVVM Pattern and bind the background color.

I found an almost similar question here on stackoverflow: Change Button Background color through MVVM pattern in WPF

Community
  • 1
  • 1
Helvetios
  • 76
  • 8