gpt4 book ai didi

c# - Xamarin 表单 UWP 工具提示

转载 作者:行者123 更新时间:2023-12-04 02:37:30 24 4
gpt4 key购买 nike

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

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

我可以成功访问 native Windows 控件,例如一个文本 block :

<    win: TextBlock Text="S"/>

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

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

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

"Xamarin.Forms.Xaml.XamlParseException: 'Position 56:45. Type ToolTipService not found in xmlns http://xamarin.com/schemas/2014/forms'"

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

最佳答案

ToolTipService 是附加属性,它不会在Forms客户端中找到ToolTipService组件,更好的方法是使用Effect创建您自己的小费服务并将其呈现在 UWP 平台中。您可以引用以下步骤。

  • Create a subclass of the PlatformEffect class.

  • Override the OnAttached method and write logic to customize the control.

  • Override the OnDetached method and write logic to clean up the control customization, if required.

  • Add a ResolutionGroupName attribute to the effect class. This attribute sets a company wide namespace for effects, preventing collisions with other effects with the same name. Note that this attribute can only be applied once per project.

  • Add an ExportEffect attribute to the effect class. This attribute registers the effect with a unique ID that's used by Xamarin.Forms, along with the group name, to locate the effect prior to applying it to a control. The attribute takes two parameters – the type name of the effect, and a unique string that will be used to locate the effect prior to applying it to a control.

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

关于c# - Xamarin 表单 UWP 工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60981303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com