TextBox.ContextMenuOpening不在Windows Phone 8.1 App中触发

时间:2014-06-30 20:49:08

标签: c# xaml windows-phone-8

我正在编写一个通用的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 !!");
      }
}

正如我所说,它在商店应用程序中完美运行(当我按住所选文本或右键单击它时菜单显示),但事件甚至没有在电话应用程序中被触发。

编辑以下是带有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>

1 个答案:

答案 0 :(得分:1)

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

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

通用应用程序仅尝试将Windows 8.1与Windows Phone匹配。如果找不到事件或属性,并且找不到对应关系,则会忽略它。

编辑:要完成答案,您需要做的就是在使用Windows Phone应用时考虑其他行为。 通用应用程序项目定义了预处理变量,因此您可以使用

之类的代码
    #if WINDOWSPHONE
var myWindowsPhoneVar = "windowsPhone";
#else
var myWindowsPhoneVar = "!windowsPhone";
#endif

我不太确定Windows手机的预处理变量是&#34; WINDOWSPHONE&#34;但是你很难找到它。

相关问题