向 CommandBar 动态添加 AppBarButton 时 IsEnabled 属性发生变化

yangyi_2009 2015-10-04 01:13:07

public ResponsableCommandBar()
{
this.DefaultStyleKey = typeof(ResponsableCommandBar);
this.SizeChanged += (sender, e) =>
{
if (e.NewSize.Width == e.PreviousSize.Width)
{

}
else
{
var addedWidth = e.NewSize.Width - (PrimaryCommands.Count * ButtonWidth + MoreWidth);
int buttonCount;
if (addedWidth > 0)
{
buttonCount = (int)(addedWidth / ButtonWidth);
for (int i = 0; i < buttonCount && SecondaryCommands.Count > 0; i++)
{
var command = SecondaryCommands[0];
var button = command as Button;
System.Diagnostics.Debug.WriteLine(button.IsEnabled);

SecondaryCommands.RemoveAt(0);
System.Diagnostics.Debug.WriteLine(button.IsEnabled);

PrimaryCommands.Add(command);
System.Diagnostics.Debug.WriteLine(button.IsEnabled);
}
System.Diagnostics.Debug.WriteLine("");
}
else if (addedWidth < 0)
{
buttonCount = (int)(-addedWidth / ButtonWidth);
for (int i = 0; i < buttonCount && PrimaryCommands.Count > 0; i++)
{
var index = PrimaryCommands.Count - 1;
var command = PrimaryCommands[index];
var button = command as Button;
System.Diagnostics.Debug.WriteLine(button.IsEnabled);

PrimaryCommands.RemoveAt(index);
System.Diagnostics.Debug.WriteLine(button.IsEnabled);

SecondaryCommands.Insert(0, command);
System.Diagnostics.Debug.WriteLine(button.IsEnabled);
}
System.Diagnostics.Debug.WriteLine("");
}
}
};
};

const double ButtonWidth = 68;
const double MoreWidth = 48;


在 SizeChanged 事件中动态调整 AppBarButton 使之在 PrimaryCommands 和 SecondaryCommands 之间移动,来实现自适应的 CommandBar。在宽度增加时将 SecondaryCommands 中的 AppBarButton 移动到 PrimaryCommands,在宽度减小时反之。

问题:在 AppBarButton 从 SecondaryCommands 移动到 PrimaryCommands 后 IsEnabled 属性会自动变为 false 而不能点击,从 PrimaryCommands 移动到 SecondaryCommands 后 IsEnabled 属性又会自动变为 true。需要找出 IsEnabled 属性发生变化的原因,解决这个问题。
...全文
438 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangyi_2009 2015-10-09
  • 打赏
  • 举报
回复
引用 1 楼 BEYONDMA 的回复:
System.Diagnostics.Debug.WriteLine(button.IsEnabled); 这些的输出是什么?
从 SecondaryCommands 移动到 PrimaryCommands 是三个 false, 从 PrimaryCommands 移动到 SecondaryCommands 是三个 true。
beyondma 2015-10-07
  • 打赏
  • 举报
回复
System.Diagnostics.Debug.WriteLine(button.IsEnabled); 这些的输出是什么?

7,657

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧