简体   繁体   English

从外部类访问XAML元素

[英]Access XAML elements from outside class

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: 因此,我制作了一个新的更改程序类(视图模型),该类访问每个xaml文件并更改背景色:

TheView1.xaml: (partial) TheView1.xaml :(部分)

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

TheView1.xaml.cs: (partial) TheView1.xaml.cs :(部分)

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

ChangeBackgrounds.cs 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". VS然后说“ tv1不包含myScrollView和myButton的定义”。 But they are clearly there, why does´nt it work? 但是它们显然在那里,为什么不起作用?

If you are working with WPF try it with the MVVM Pattern and bind the background color. 如果您使用的是WPF,请尝试使用MVVM模式并绑定背景色。

I found an almost similar question here on stackoverflow: Change Button Background color through MVVM pattern in WPF 我在stackoverflow上发现了一个几乎类似的问题: 通过WPF中的MVVM模式更改按钮背景颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM