C# 修改文本框上下文菜单/菜单输出

C# 修改文本框上下文菜单/菜单输出,c#,windows,xaml,uwp,uwp-xaml,C#,Windows,Xaml,Uwp,Uwp Xaml,我试图使用此代码修改文本框上下文菜单/MenuFlyout,但它不起作用(附加菜单项不会出现并且myFlyout始终null)(UWP/C#) private void菜单\u打开(对象发送方,对象e) { MenuFlyout myFlyout=发送方为MenuFlyout; if(myFlyout!=null&&myFlyout.Target==TextBox) { MenuFlyoutSubItem searchWith=新建MenuFlyoutSubItem(); searchWith.

我试图使用此代码修改文本框上下文菜单/MenuFlyout,但它不起作用(附加菜单项不会出现并且
myFlyout
始终
null
)(UWP/C#)

private void菜单\u打开(对象发送方,对象e)
{
MenuFlyout myFlyout=发送方为MenuFlyout;
if(myFlyout!=null&&myFlyout.Target==TextBox)
{
MenuFlyoutSubItem searchWith=新建MenuFlyoutSubItem();
searchWith.Icon=newsymbolicon(Symbol.Find);
searchWith.Text=“使用搜索”;
MenuFlyoutItem googles=新的MenuFlyoutItem();
googles.Text=“谷歌”;
谷歌。点击+=谷歌点击;
searchWith.Items.Add(谷歌);
MenuFlyoutItem bings=新建MenuFlyoutItem();
bings.Text=“Bing”;
bings.Click+=bings\u Click;
searchWith.Items.Add(bings);
myFlyout.Items.Add(searchWith);
}
}
私有异步void Googles\u单击(对象发送方,路由目标)
{
如果(TextBox.SelectedText!=null)
{
var uri=新的uri(@“https://google.com/search?q=“+文本框。选择文本);
var success=wait Launcher.launchurisync(uri);
}
}
私有异步无效Bings\u单击(对象发送方,路由目标)
{
如果(TextBox.SelectedText!=null)
{
var uri=新的uri(@“https://bing.com/search?q=“+文本框。选择文本);
var success=wait Launcher.launchurisync(uri);
}
}
已加载私有无效文本框(对象发送方,路由目标)
{
TextBox.SelectionFlyout.Opening+=菜单\打开;
TextBox.ContextFlyout.Opening+=菜单\打开;
}
私有无效文本框\u已卸载(对象发送方,路由目标)
{
TextBox.SelectionFlyout.Opening-=菜单\打开;
TextBox.ContextFlyout.Opening-=菜单\打开;
}

问题在于,您没有为
选择弹出按钮
上下文弹出按钮
提供
菜单输出
实例。请参考以下代码添加
MenuFlyout

<TextBox x:Name="TextBox" Loaded="TextBox_Loaded" Unloaded="TextBox_Unloaded">
    <TextBox.ContextFlyout>
        <MenuFlyout>
        </MenuFlyout>
    </TextBox.ContextFlyout>
</TextBox>

你能分享相关的xaml代码吗?@NicoZhu MSFT我将其添加到我的问题中,但这将删除内置的上下文菜单UI想要修改上下文菜单而不是替换它,但是你知道如何让用户在上下文菜单中选择要搜索的搜索引擎吗?根据我的经验,您需要使用本地设置来记录所选引擎,并在调用搜索api时获得该选项。
private void Menu_Opening(object sender, object e)
{
    TextCommandBarFlyout myFlyout = sender as TextCommandBarFlyout;

    if (myFlyout != null && myFlyout.Target == TextBox)
    {
        AppBarButton searchCommandBar = new AppBarButton() { Icon = new SymbolIcon(Symbol.Find), Label = "Search With" };
        searchCommandBar.Click += SearchCommandBar_Click;
        myFlyout.PrimaryCommands.Add(searchCommandBar);

    }
}

private void SearchCommandBar_Click(object sender, RoutedEventArgs e)
{

}