Xamarin forms: tooltip in windows app

一笑奈何 提交于 2021-02-07 09:27:23

问题


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

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;    
}

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


回答1:


Here is solution which I implemented for Xamarin UWP app.

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.

Hope it helps :-)

Added custom StackLayout :

public class ToolTipStacklayout :StackLayout
{
}

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);
        }         
    }}

Also came to implement this for MAC desktop application .

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;

    }}


来源:https://stackoverflow.com/questions/41978916/xamarin-forms-tooltip-in-windows-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!