我正在使用 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 包的工作示例?
最佳答案
这是我为 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;
}}
关于c# - Xamarin 形式 : tooltip in windows app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978916/