【问题标题】:How can I programatically tap an AppBarButton?如何以编程方式点击 AppBarButton?
【发布时间】:2015-01-04 14:18:17
【问题描述】:

我有这个代码:

async private void InformUserOfRenameOption(string platypusName)
{
    CoreWindowDialog cwd = new CoreWindowDialog(String.Format(
        "This platypus has been given the default name {0}. Do you want to change it?", platypusName));
    cwd.Commands.Add(new UICommand { Label = "Yes", Id = 0 });
    cwd.Commands.Add(new UICommand { Label = "No", Id = 1 });
    cwd.Commands.Add(new UICommand { Label = "Cancel", Id = 2 });
    cwd.CancelCommandIndex = 2;
    IUICommand cmd = await cwd.ShowAsync();
    //if (cmd.Id == 0) <= 'Operator '==' cannot be applied to operands of type 'object' and 'int'
    if (Convert.ToInt32(cmd.Id) == 0)
    {
        appbarbtnRenamePlatypus.Tapped();
    }
} 

...但是我尝试以编程方式点击 AppBarButton 失败并出现编译时错误:“事件 'Windows.UI.Xaml.UIElement.Tapped' 只能出现在 += 的左侧或 -="

那么如何我以编程方式点击按钮,或者更具体地说,让它的 Button.Flyout Flyout 飞出?

【问题讨论】:

  • 我相信你可以使用Automation Peers。或者您可以创建一个从AppBarButton 扩展的自定义控件,因为它不是sealed

标签: c# windows-runtime appbar winrt-component


【解决方案1】:

通常,您会将 AppBar 按钮的单击处理程序中的代码重构为它自己的方法,然后您可以从任何地方调用它。

如果您更喜欢冒险,也可以考虑使用ICommand - 这使您可以拥有任意数量的 UI 元素(例如 ButtonBase.Command)或直接从代码中调用它。

【讨论】:

    猜你喜欢
    • 2015-02-09
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2016-07-27
    相关资源
    最近更新 更多