【发布时间】:2018-09-11 00:49:07
【问题描述】:
我希望我的按钮颜色在 xbox 选择框位于按钮上时发生变化。我怎样才能做到这一点?我在 Xbox UWP 应用中没有关注焦点事件。
【问题讨论】:
-
请提供您尝试过的内容并显示您的按钮的 xaml 代码。
我希望我的按钮颜色在 xbox 选择框位于按钮上时发生变化。我怎样才能做到这一点?我在 Xbox UWP 应用中没有关注焦点事件。
【问题讨论】:
您需要在您尝试执行此操作的按钮上设置 GotFocus 和 LostFocus 事件。
<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);
【讨论】: