【问题标题】:How can I remove a color from a button in Xamarin?如何从 Xamarin 中的按钮中删除颜色?
【发布时间】:2018-01-24 12:33:19
【问题描述】:

在我的 C# 中,我正在更改按钮的颜色,如下所示:

aButton.BackgroundColor = Color.FromHex("#e9e9e9");

有没有办法去除这种颜色。我知道我可以将其设置回以前的颜色,但想知道是否有办法删除分配的任何内容?

【问题讨论】:

  • 删除是什么意思?如果您的意思是透明的,您可以将其分配给Color.Transparent
  • 我将其从默认值更改,因此我希望将其恢复为原来的样子。我想知道它是在哪里设置的,因为我假设颜色是用 Apple 或 Xamarin 代码设置的。
  • 根据文档,应该是Color.Defaultdeveloper.xamarin.com/api/property/…
  • 完成。我还添加了透明颜色的提示,因为如果其他人发现这个问题,这可能就是他们正在寻找的内容:)

标签: c# xamarin xamarin.forms


【解决方案1】:

根据 Xamarin 文档,the BackgroundColor of a VisualElement is Color.Default

所以您可以将您的 Button.BackgroundColor 属性分配给 Color.Default

aButton.BackgroundColor = Color.Default;

您无法移除颜色,因为它是不可为空的值类型。如果您打算使其透明,请将其分配给Color.Transparent

aButton.BackgroundColor = Color.Transparent;

【讨论】:

  • 这是一个比我更好的答案。从来不知道Color.Default 存在!
【解决方案2】:

您正在更改Button 的背景颜色。没有办法“移除”颜色。我不认为这存在于任何语言中(如果我错了,请纠正我)

最好的办法是重置颜色:

public Color PreviousColour { get; set;}

public void SetColour()
{
    PreviousColour = aButton.BackgroundColor;
    aButton.BackgroundColor = Color.FromHex("#e9e9e9");
}

public void ResetColour()
{
    aButton.BackgroundColor = PreviousColour;
}

【讨论】:

  • 我在想 Xamarin 源代码中的某个地方设置了默认颜色。知道是不是这样吗?
  • @Alan 我不知道。 this 将是开始的地方。但上面的代码会更容易/更简单
猜你喜欢
  • 2016-04-24
  • 2023-04-11
  • 1970-01-01
  • 2017-01-09
  • 2021-12-05
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 2021-04-19
相关资源
最近更新 更多