Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin形成UWP工具提示_C#_Xamarin.forms_Uwp - Fatal编程技术网

C# Xamarin形成UWP工具提示

C# Xamarin形成UWP工具提示,c#,xamarin.forms,uwp,C#,Xamarin.forms,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,

我想通过使用本机视图和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>

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

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

我在编译过程中遇到以下异常:

“Xamarin.Forms.Xaml.XamlParseException:'位置56:45。键入 在xmlns中找不到ToolTipService ""


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

ToolTipService
是attached属性,它不会在Forms client中找到ToolTipService程序集,更好的方法是创建自己的tip服务并在UWP平台中呈现它。您可以参考以下步骤

  • 创建PlatformEffect类的子类

  • 重写OnAttached方法并写入逻辑以自定义控件

  • 如果需要,重写OnAttached方法并写入逻辑以清除控件自定义

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

  • 将ExportEffect属性添加到effect类。此属性使用唯一ID注册效果,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"
    />
//表单项目名称apce
xmlns:effects=“clr命名空间:ToolTipTestApp”
......

我搜索了一些信息,但我找不到任何方法为表单中的本机视图添加工具提示,如果您想实现这一点,我建议您可以为任何要显示工具提示的控件自定义渲染,这与您可以查看的线程相同:@CherryBu MSFT:谢谢。您是否建议我自定义一个条目,如下所述:?或者在UWP中是否应该使用特定于文本框的渲染器?在上面的示例中,如何支持多行工具提示?
// forms project namesapce
xmlns:effects="clr-namespace:ToolTipTestApp"
......

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