【发布时间】: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