【问题标题】:Xamarin forms: tooltip in windows appXamarin 表单:Windows 应用程序中的工具提示
【发布时间】:2017-06-18 03:34:39
【问题描述】:

我正在使用 Xamarin 表单 PCL 构建应用程序。我的应用可以在 android、ios、windows 10 和 windows 8.1 上运行。

我只需要在 Windows 操作系统的点击事件上实现工具提示控件。我试过这样tooltip-

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

是否有任何工具提示控件或任何 nuget 包的工作示例?

【问题讨论】:

  • 你能澄清什么不起作用吗?
  • @Sefe 没有任何效果。没有工具提示出现。
  • 你正在创建一个工具提示,但你没有将它附加到任何东西上。难怪你看不到它。检查工具提示的 Parent 属性。如果是null,请不要惊讶您的工具提示没有显示。
  • @Sefe 如何添加其父级。
  • 你应该先做自己的研究。好的起点是herehere。我找到这些参考资料的时间比你写问题的时间要少。

标签: c# xamarin.forms tooltip xamarin.uwp


【解决方案1】:

这是我为 Xamarin UWP 应用程序实施的解决方案。

创建自定义堆栈布局,因为我想要整个部分的工具提示。您可以添加不同的控件。 主要部分是我使用 ClassId 来获取需要在工具提示中显示的文本,您可以根据需要添加自定义属性。

希望对你有帮助:-)

添加了自定义 StackLayout:

public class ToolTipStacklayout :StackLayout
{
}

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

也来为 MAC 桌面应用程序实现这个。

这是 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;

    }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-16
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 2020-07-13
    相关资源
    最近更新 更多