【问题标题】:How can we change the button background color in UWP xbox app on selecting (focusing)我们如何在选择(聚焦)时更改 UWP xbox 应用程序中的按钮背景颜色
【发布时间】:2018-09-11 00:49:07
【问题描述】:

我希望我的按钮颜色在 xbox 选择框位于按钮上时发生变化。我怎样才能做到这一点?我在 Xbox UWP 应用中没有关注焦点事件。

【问题讨论】:

  • 请提供您尝试过的内容并显示您的按钮的 xaml 代码。

标签: button uwp xbox


【解决方案1】:

您需要在您尝试执行此操作的按钮上设置 GotFocusLostFocus 事件。

<Button x:Name="MyButton" GotFocus="ButtonGotFocus" LostFocus="ButtonLostFocus"/>

在后面的代码中,您可以相应地更改背景颜色。如果您只想在 xbox 上使用该行为,也可以选择先检查 DeviceFamily

private void GotFocus(object sender, object args)
{
    if(AnalyticsVersionInfo.DeviceFamily == "Windows.Xbox")
    {
        //change the color when the button gets focus
        MyButton.BackgroundColor = new SolidColorBrush(Colors.Blue);
    }
}

private void LostFocus(object sender, object args)
{
    if(AnalyticsVersionInfo.DeviceFamily == "Windows.Xbox")
    {
        //change the color when the button looses focus
        MyButton.BackgroundColor = new SolidColorBrush(Colors.Green);
    }
}

更多关于 DeviceFamily 属性:https://docs.microsoft.com/en-us/uwp/api/windows.system.profile.analyticsversioninfo.devicefamily#Windows_System_Profile_AnalyticsVersionInfo_DeviceFamily

更新

如果您想对所有或多个按钮产生相同的效果,只需将事件分配给您想要影响的每个按钮,如下所示:

<Button x:Name="MyButton" GotFocus="ButtonGotFocus" LostFocus="ButtonLostFocus"/>
<Button x:Name="MyButton2" GotFocus="ButtonGotFocus" LostFocus="ButtonLostFocus"/>

在后端只需将 MyButton 替换为 (作为按钮发送者)

(sender as Button).BackgroundColor = new SolidColorBrush(Colors.Green);

【讨论】:

  • 谢谢,它的工作。无论如何,我们可以将其作为一个全局函数,以便可以在整个应用程序中使用?
  • 是的,只需将 gotfocus 和 lostfocus 事件分配给 xaml 中的每个按钮。这样每个按钮都会得到这种行为,你只需要编写一次后端事件,但不要使用 MyButton,只需使用 sender 作为按钮,查看更新的答案
猜你喜欢
  • 2016-07-04
  • 1970-01-01
  • 2016-07-04
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 2011-09-10
  • 1970-01-01
相关资源
最近更新 更多