首页 > 解决方案 > Xamarin 形成 UWP 工具提示

问题描述

我想通过使用本机视图和 Windows.UI.Xaml Nuget 包在 Xamarin for UWP 中添加工具提示。我已将以下引用添加到 xaml 页面以获取 Windows 本机视图:

<ContentPage 

             xmlns:win="clr-namespace:Windows.UI.Xaml.Controls;assembly=Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime;targetPlatform=Windows"   

</ContentPage>

我可以成功访问本机 Windows 控件,例如 TextBlock:

<    win: TextBlock Text="S"/>

但是,当我尝试将工具提示添加到控件时:

<win:TextBlock Text="S" ToolTipService.ToolTip="Service agreement"/>

在编译过程中出现以下异常:

“Xamarin.Forms.Xaml.XamlParseException:'位置 56:45。在 xmlns http://xamarin.com/schemas/2014/forms中找不到类型 ToolTipService '”

系统是否在标准 Windows.UI.Xaml.Controls 命名空间和 Nuget 包中的扩展命名空间之间感到困惑?

标签: c#xamarin.formsuwp

解决方案


ToolTipService是附加属性,在 Forms 客户端中找不到 ToolTipService 程序集,更好的方法是使用Effect创建自己的提示服务并在 UWP 平台中呈现。您可以参考以下步骤。

  • 创建 PlatformEffect 类的子类。

  • 重写 OnAttached 方法并编写逻辑来自定义控件。

  • 重写 OnDetached 方法并编写逻辑以清理控件自定义(如果需要)。

  • ResolutionGroupName属性添加到效果类。此属性为效果设置公司范围的命名空间,防止与同名的其他效果发生冲突。请注意,每个项目只能应用此属性一次。

  • 将 ExportEffect 属性添加到效果类。此属性使用 Xamarin.Forms 使用的唯一 ID 以及组名称注册效果,以便在将效果应用到控件之前定位效果。该属性有两个参数——效果的类型名称,以及在将效果应用到控件之前用于定位效果的唯一字符串。

UWP 代码部分

[assembly: ResolutionGroupName("Microsoft")]
[assembly: ExportEffect(typeof(UWPToolTipEffect), nameof(TooltipEffect))]

namespace NativeSwitch.UWP
{
    public class UWPToolTipEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            var control = Control ?? Container;

            if (control is DependencyObject)
            {
                ToolTip toolTip = new ToolTip();
                toolTip.Content = TooltipEffect.GetText(Element);
                switch (TooltipEffect.GetPosition(Element))
                {
                    case TooltipPosition.Bottom:
                        toolTip.Placement = Windows.UI.Xaml.Controls.Primitives.PlacementMode.Bottom;
                        break;
                    case TooltipPosition.Top:
                        toolTip.Placement = Windows.UI.Xaml.Controls.Primitives.PlacementMode.Top;
                        break;
                    case TooltipPosition.Left:
                        toolTip.Placement = Windows.UI.Xaml.Controls.Primitives.PlacementMode.Left;
                        break;
                    case TooltipPosition.Right:
                        toolTip.Placement = Windows.UI.Xaml.Controls.Primitives.PlacementMode.Right;
                        break;
                    default:
                        return;
                }
                ToolTipService.SetToolTip(control, toolTip);
            }

        }

        protected override void OnDetached()
        {

        }
    }
}

表单代码部分

public static class TooltipEffect
{

    public static readonly BindableProperty TextProperty =
      BindableProperty.CreateAttached("Text", typeof(string), typeof(TooltipEffect), string.Empty, propertyChanged: OnTextChanged);

    public static readonly BindableProperty PositionProperty =
      BindableProperty.CreateAttached("Position", typeof(TooltipPosition), typeof(TooltipEffect), TooltipPosition.Bottom);


    public static string GetText(BindableObject view)
    {
        return (string)view.GetValue(TextProperty);
    }

    public static void SetText(BindableObject view, string value)
    {
        view.SetValue(TextProperty, value);
    }

    public static TooltipPosition GetPosition(BindableObject view)
    {
        return (TooltipPosition)view.GetValue(PositionProperty);
    }

    public static void SetPosition(BindableObject view, TooltipPosition value)
    {
        view.SetValue(PositionProperty, value);
    }

    static void OnTextChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var view = bindable as View;
        if (view == null)
        {
            return;
        }

        string text = (string)newValue;
        if (!string.IsNullOrEmpty(text))
        {
            view.Effects.Add(new ControlTooltipEffect());
        }
        else
        {
            var toRemove = view.Effects.FirstOrDefault(e => e is ControlTooltipEffect);
            if (toRemove != null)
            {
                view.Effects.Remove(toRemove);
            }
        }
    }

}

public enum TooltipPosition
{
    Bottom,

    Right,

    Left,

    Top
}

class ControlTooltipEffect : RoutingEffect
{
    public ControlTooltipEffect() : base($"Microsoft.{nameof(TooltipEffect)}")
    {

    }
}

用法

// forms project namesapce
xmlns:effects="clr-namespace:ToolTipTestApp"
......

<win:TextBlock
    effects:TooltipEffect.Position="Right"
    effects:TooltipEffect.Text="Hello"
    Text="Hello Wrold"
    />

推荐阅读