【问题标题】:Xamarin forms UWP tooltipsXamarin 形成 UWP 工具提示
【发布时间】:2020-07-13 19:37:33
【问题描述】:

我想通过使用本机视图和 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 控件,例如一个文本块:

<    win: TextBlock Text="S"/>

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

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

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

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

系统是否混淆了标准的 Windows.UI.Xaml.Controls 命名空间和 Nuget 包中的扩展命名空间?

【问题讨论】:

标签: c# xamarin.forms uwp


【解决方案1】:

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"
    />

【讨论】:

  • 上例中如何支持多行工具提示?
猜你喜欢
  • 1970-01-01
  • 2018-06-27
  • 2016-08-17
  • 2019-03-29
  • 1970-01-01
  • 1970-01-01
  • 2020-03-08
  • 2017-08-01
  • 1970-01-01
相关资源
最近更新 更多