我想添加一个弹出窗口到我的TextBlock
,当我选择 TextBlock 中的文本时,弹出窗口将显示在所选位置(有点像 Microsoft Edge 中的阅读模式,当您在阅读模式下选择文本时,将会有一个弹出窗口显示单词的定义)。但我不知道怎么办。我尝试过使用SelectionChanged
,但是此事件传递的参数没有我可以用来设置的位置flyout
。那么我该怎么做呢?
除此之外,我想知道什么SelectionFlyout
是为了?我认为它可以帮助我。
这是我的代码:
<TextBlock x:Name="webviewtest" Grid.Row="1" Text="This is a select-flyout test." FontSize="300" IsTextSelectionEnabled="true" >
<TextBlock.SelectionFlyout>
<Flyout>
<TextBlock Text="this is the flyout"></TextBlock>
</Flyout>
</TextBlock.SelectionFlyout>
</TextBlock>
当我选择文本时,弹出窗口从未出现。很明显我一直用错了。所以我检查了微软文档 https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.textblock.selectionflyout#Windows_UI_Xaml_Controls_TextBlock_SelectionFlyout它说
获取或设置选择文本时显示的浮出控件,如果未显示浮出控件,则为 null。
我在网上找不到任何关于此的示例。
这可以通过设置来实现TextBlock IsTextSelectionEnabled
to True
并通过使用MenuFlyout
显示选定的文本。
XAML
<TextBlock x:Name="webviewtest" Text="This is a select-flyout test." FontSize="100" IsTextSelectionEnabled="True" RightTapped="webviewtest_RightTapped">
<FlyoutBase.AttachedFlyout>
<MenuFlyout x:Name="Flyout">
<MenuFlyout.Items>
<MenuFlyoutItem x:Name="FlyItem" Text="">
</MenuFlyoutItem>
</MenuFlyout.Items>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
</TextBlock>
C#
private void webviewtest_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
TextBlock tb = sender as TextBlock;
if (tb.SelectedText.Length > 0)
{
Item.Text = tb.SelectedText;
}
// Show at cursor position
Flyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)