Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

c# - Xamarin: how do I change the color of a button added in code?

My Xamarin app creates buttons in code. I can change the color of a button I click, as the button is referenced in its Clicked handler's sender argument, but I want to change the color of buttons that I didn't click. The problem is, how do I find the buttons I want to change?

I thought about using FindByName, but Name doesn't appear to be an attribute.

One way I can think of: loop through all of the buttons until I find the one with the StyleId of the desired button. Is there an easier way than that?

question from:https://stackoverflow.com/questions/65830584/xamarin-how-do-i-change-the-color-of-a-button-added-in-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

when you create a button in code you need to keep a reference to it, like you would with any C# object you want to reference later

Button MyButton = new Button { ... };
MyButton.BackgroundColor = Color.Purple;

if you need to access it from multiple places in your code, you should declare it as a class level variable to that it has scope throughout your class


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...