简体   繁体   English

Xamarin:如何更改代码中添加的按钮的颜色?

[英]Xamarin: how do I change the color of a button added in code?

My Xamarin app creates buttons in code.我的 Xamarin 应用程序在代码中创建按钮。 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.我可以更改单击的按钮的颜色,因为该按钮在其 Clicked 处理程序的 sender 参数中引用,但我想更改未单击的按钮的颜色。 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.我考虑过使用 FindByName,但 Name 似乎不是一个属性。

One way I can think of: loop through all of the buttons until I find the one with the StyleId of the desired button.我能想到的一种方法:遍历所有按钮,直到找到具有所需按钮的 StyleId 的按钮。 Is there an easier way than that?还有比这更简单的方法吗?

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当您在代码中创建按钮时,您需要保留对它的引用,就像您以后要引用的任何 C# object 一样

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如果您需要从代码中的多个位置访问它,则应将其声明为class 级别变量,以使其在整个 class 中具有 scope

If you want to find the button created in code behind without XAML, like @Jason said, use the variable name of the Button you created or set it directly.如果您想找到在没有 XAML 的代码中创建的按钮,就像@Jason 说的那样,使用您创建的按钮的变量名或直接设置它。

 Button button = new Button { ClassId = "button1", BackgroundColor = Color.Red };

If you want to access a view with FindByName , you need to create the button with x:Name in XAML.如果要使用FindByName访问视图,则需要在 XAML 中创建带有x:Name的按钮。

There is an XAML example:有一个 XAML 示例:

<Button x:Name="button1" ></Button>

And in your code behind you could do something like:在您背后的代码中,您可以执行以下操作:

 button1.BackgroundColor = Color.Red;

If you need to find a view in xaml for some reason, do this:如果您出于某种原因需要在 xaml 中查找视图,请执行以下操作:

var button = this.FindByName<Button>("button1");
button.BackgroundColor = Color.Red;

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

相关问题 在 Xamarin 中,如何在 1 秒内更改按钮的背景颜色并再次将其恢复为原始颜色? - In Xamarin, how can I change the background color of a button for just 1 second and return it again to its original color? 如何更改C#Xamarin中以编程方式添加的按钮的背景? - How can i change background of programmatically added button in C# Xamarin? 我如何做按钮单击背景颜色的更改? - How do I do a button click background color change? 更改Xamarin表单按钮的颜色 - Change Xamarin forms button color 如何在另一个表单上实时使按钮更改颜色 - How do I make a button change color on another Form live 如何在Xamarin Android中更改SimpleListItemSingleChoice listview的单选按钮颜色 - How to change the radio button color of the SimpleListItemSingleChoice listview in xamarin android Xamarin 如何更改按钮单击 MVVM 的背景颜色 - Xamarin how to change background color on button click MVVM Xamarin.iOS没有导航栏时如何更改状态栏的颜色 - Xamarin.iOS how do I change the status status bar color when there is no Navigation bar 如何使用Xamarin.Forms.iOS在渲染器视图中更改UIPickerView文本颜色? - How do I change UIPickerView text color in renderer view using Xamarin.Forms.iOS? 如何更改Xamarin中的密钥库位置? - How do I change the keystore location in Xamarin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM