文本框.在Windows Phone 8.1应用程序中,ContextMenuOpening不触发
本文关键字:ContextMenuOpening 应用程序 Windows Phone 文本 | 更新日期: 2023-09-27 18:10:48
我正在写一个通用的windows应用程序,我试图得到我自己的文本框上下文菜单。在商店应用程序中一切都如预期的那样工作,但在手机应用程序中,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 !!");
}
}
就像我说的,它在商店应用程序中工作得很好(当我按住选定的文本或右键单击它时菜单会显示),但在电话应用程序中甚至不会触发事件。
EDIT这是TextBox的xaml代码的一部分(其余部分只是页面+ hub的标准代码):
<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>
答案很简单,TextBox在WindowsPhone中没有ContextMenuOpening事件。
所以即使你把代码放在通用应用程序中,它也不会发生。
通用应用程序只尝试匹配windows 8.1与windows phone。如果没有找到事件或属性,并且没有找到对应,则直接忽略它。
EDIT:要完成这个答案,你要做的是,想想当你在windows手机应用程序中的另一个行为。通用应用程序项目定义预处理变量,所以你可以使用像
这样的代码 #if WINDOWSPHONE
var myWindowsPhoneVar = "windowsPhone";
#else
var myWindowsPhoneVar = "!windowsPhone";
#endif
我不太确定windows phone的预处理变量到底是"WINDOWSPHONE",但你不会有麻烦找到它