【问题标题】:TextBox.ContextMenuOpening not firing in Windows Phone 8.1 AppTextBox.ContextMenuOpening 未在 Windows Phone 8.1 应用程序中触发
【发布时间】:2014-06-30 20:49:07
【问题描述】:

我正在编写一个通用的 Windows 应用程序,并试图为一个 TextBox 获取我自己的上下文菜单。商店应用程序中的一切都按预期工作,但在电话应用程序上,ContextMenuOpening 事件没有触发。我试过按住并点击选定的文本,但它不起作用,唯一发生的事情是出现了复制的小圆圈。

这里是我注册事件处理程序的地方:(该方法在页面加载时调用)

public void FlipViewLoaded()
{
    TextBox textBox = GetChildControl<TextBox>
                          (_imagesFlipView, "ReadOnlyTextBox");

    textBox.ContextMenuOpening +=
        new ContextMenuOpeningEventHandler(Open);
}

这是处理程序:

private async void Open(object sender, DoubleTappedRoutedEventArgs e)
{
    e.Handled = true;
    TextBox textbox = (TextBox)sender;
    if (textbox.SelectionLength > 0)
    {
        var menu = new PopupMenu();
        menu.Commands.Add(new UICommand("Get Word", null, 1));
        menu.Commands.Add(new UICommand("Get Text", null, 2));

        var chosenCommand = await menu.ShowAsync(new Point());
        if (chosenCommand != null)
        {
            switch (chosenCommand.Id.ToString())
            {
                 // different commands implementations
            }
         }
         else
         {
            Debug.WriteLine("The chosen command is null !!");
         }
      }
      else
      {
            Debug.WriteLine("The selected _text is null !!");
      }
}

正如我所说,它在 Store App 中完美运行(当我按住所选文本或右键单击它时会显示菜单),但该事件甚至不会在 Phone App 中触发。

编辑这是带有TextBox的部分xaml代码(其余只是页面附带的标准代码+集线器):

     <HubSection>
            <DataTemplate>
                <FlipView x:Name="ImagesFlipView" ItemsSource="{Binding Images}"
                          viewmodel:ImagesPageViewModel.FlipView="{Binding ElementName=ImagesFlipView}">
                    <FlipView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Image Source="{Binding ImageURL}" />
                                <StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Stretch" >
                                    <TextBox x:Name="TranslationTextBox" Visibility="Visible" 
                                             Height="80" IsReadOnly="True" TextWrapping="Wrap"
                                             BorderThickness="0" Margin="5"
                                             Style="{StaticResource MyTextBoxStyle}"
                                             Background="{StaticResource TextBoxButtonBackgroundThemeBrush}" 
                                             Foreground="White" FontSize="25" VerticalAlignment="Bottom" />
                                    <TextBox x:Name="ReadOnlyTextBox" FontSize="25" IsReadOnly="True" 
                                             Height="80" TextWrapping="Wrap" Text="{Binding Path=Translations[english]}" 
                                             BorderThickness="0" Foreground="White" Margin="5" 
                                             Style="{StaticResource MyTextBoxStyle}"
                                             Background="{StaticResource TextBoxButtonBackgroundThemeBrush}"
                                             VerticalAlignment="Bottom" />
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </FlipView.ItemTemplate>
                </FlipView>
            </DataTemplate>
        </HubSection>

【问题讨论】:

  • 能否也提供一些 UI 方面的代码?
  • 完成。与商店应用程序的唯一区别是,我在这里使用集线器,而我不使用集线器,但集线器不会导致问题(我也尝试过不使用它)。

标签: c# xaml windows-phone-8


【解决方案1】:

答案很简单,TextBox 在 WindowsPhone 中没有 ContextMenuOpening 事件。

因此,即使您将该代码放入通用应用程序中,也不会发生。

通用应用程序仅尝试将 windows 8.1 与 windows phone 匹配。如果未找到事件或属性,也未找到对应关系,则直接将其忽略。

编辑:要完成答案,您必须做的是考虑您在 Windows Phone 应用程序中的另一种行为。 通用应用项目定义了预处理变量,因此您可以使用类似的代码

    #if WINDOWSPHONE
var myWindowsPhoneVar = "windowsPhone";
#else
var myWindowsPhoneVar = "!windowsPhone";
#endif

我不太确定 windows phone 的预处理变量是否完全是“WINDOWSPHONE”,但您可以轻松找到它。

【讨论】:

  • 感谢您的回答,我开始认为这也可能是它,即使它不在文档中(它实际上说这个事件在两个平台上都存在)。这两个变量是 WINDOWS_APP 和 WINDOWS_PHONE_APP,我已经在几个地方使用过它们,只是在这种特殊情况下我想不出更好的方法。
  • 你的情况如何?为什么要显示在上下文菜单中?
  • 我有一张带有两个文本框的图片 - 一个带有原始文本,一个带有翻译(开头不可见)。这个想法是,当用户选择一个单词并按住它时,会显示一个上下文菜单,让他可以选择翻译单词(连接到在线词典)或整个文本(使其他文本框可见)。双击还可以让他翻译整个文本,但不能翻译单词本身。我可以在 AppBar 中添加一个搜索按钮,当按下它时直接翻译搜索到的单词,但我希望两个版本具有相同的用户体验。
  • 好的,我明白了。无论如何,来自 windows phone 和 windows 8 的用户不一定有相同的反应。所以尽量保持应用之间的统一性和同一性,但是根据平台对 UI 做一些细微的调整也不是坏事
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多