2

我正在使用 Xamarin 表单 PCL 构建应用程序。我的应用可以在 android、ios、windows 10 和 windows 8.1 上运行。

我只需要在 Windows 操作系统的点击事件上实现工具提示控件。我试过这样的工具提示-

if(Device.OS == TargetPlatform.Windows)
{
    var tapGestureRecognizer1 = new TapGestureRecognizer();
    tapGestureRecognizer1.Tapped += TapGestureRecognizer1_Tapped1;
    icon1.GestureRecognizers.Add(tapGestureRecognizer1);
}

private void TapGestureRecognizer1_Tapped1(object sender, EventArgs e)
{
    Constant.UserActiveTime = DateTime.Now;
    Windows.UI.Xaml.Controls.ToolTip toolTip1 = new Windows.UI.Xaml.Controls.ToolTip();
    toolTip1.Content = "Hi";
    toolTip1.Visibility = Windows.UI.Xaml.Visibility.Visible;    
}

是否有任何用于工具提示控件或任何 nuget 包的工作示例?

4

1 回答 1

4

这是我为 Xamarin UWP 应用程序实现的解决方案。

创建自定义堆栈布局,因为我想要整个部分的工具提示。您可以添加不同的控件。主要部分是我使用 ClassId 来获取需要在工具提示中显示的文本,如果您愿意,可以添加自定义属性。

希望能帮助到你 :-)

添加了自定义 StackLayout :

public class ToolTipStacklayout :StackLayout
{
}

在 UWP 中添加了渲染器:

public class ToolTipRendererUWP : VisualElementRenderer<StackLayout, StackPanel>
{

    protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
    {
        base.OnElementChanged(e);

        if (Element == null)
            return;            

        if (!string.IsNullOrEmpty(Element.ClassId))
        {
            ToolTip toolTip = new ToolTip();
            toolTip.Content = Element.ClassId;
            ToolTipService.SetToolTip(this, toolTip);
        }         
    }}

也来为 MAC 桌面应用程序实现这一点。

这是 MAC 的解决方案:

 public class ToolTipRendererMAC : VisualElementRenderer<StackLayout>
{
    protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
    {
        base.OnElementChanged(e);
        if (Element == null)
            return;

        if (!string.IsNullOrEmpty(Element.ClassId))
            this.ToolTip = Element.ClassId;

    }}
于 2018-03-28T13:20:04.363 回答