Xamarin 表单:Windows 应用程序中的工具提示 [英] Xamarin forms: tooltip in windows app

查看:28
本文介绍了Xamarin 表单:Windows 应用程序中的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building an app using Xamarin forms PCL. My app will work on android, ios, windows 10 and windows 8.1.

我只需要为 Windows 操作系统实现点击事件的工具提示控件.我像这样尝试了 tooltip-

I need to implement a tooltip control on tap event for windows os only. I tried tooltip like this-

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 包的工作示例?

Is there any working sample for tooltip control or any nuget package?

推荐答案

这是我为 Xamarin UWP 应用实现的解决方案.

Here is solution which I implemented for Xamarin UWP app.

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

Created custom stacklayout because I wanted a tooltip on whole section.In that you can add different controls. Main part is I'm using ClassId to get what text is needed to display in tooltip, you can add your custom property if you wish.

希望有帮助:-)

添加自定义 StackLayout :

Added custom StackLayout :

public class ToolTipStacklayout :StackLayout
{
}

在 UWP 中添加渲染器:

Added renderer in 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 桌面应用实现这个.

Also came to implement this for MAC desktop application .

这是MAC的解决方案:

Here is the solution for 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;

    }}

这篇关于Xamarin 表单:Windows 应用程序中的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆