如何在 Xamarin.Forms 页面中使用 Spotlight Xamarin.Android 库 [英] How to use Spotlight Xamarin.Android Library in Xamarin.Forms Pages
问题描述
我想安装一个库来在用户打开应用程序时首次聚焦首页
.为此,我发现了一个名为 AndroidSpotlight 的惊人库,但问题是我不能t 将其安装在 Xamarin.Forms
项目中,因为它仅适用于 Xamarin.Android
.
I wanted to install a library to Spotlight the First Page
for the first time when users opens the app. For that I found this amazing library called AndroidSpotlight, but the problem is that I can't install that in Xamarin.Forms
project as it's only for Xamarin.Android
.
当我尝试为 Xamarin.Forms
项目安装它时,它给了我这个错误.
When I try to install it for Xamarin.Forms
project, it gives me this error.
包 Android.Spotlight 2019.11.14.1 与 netstandard2.1 (.NETStandard,Version=v2.1) 不兼容.包 Android.Spotlight 2019.11.14.1 支持:monoandroid10 (MonoAndroid,Version=v1.0)
但是第一页在 Xamarin.Forms
中,那么我如何使用他们的这个库?
But the first page is in Xamarin.Forms
, so how could I use this library their?
Views/IntroPage.xml
(我想突出显示ImageButton
)
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Mobile.App.Views.IntroPage"
xmlns:local="clr-namespace:Mobile.App.Control"
NavigationPage.HasNavigationBar="False">
<ContentPage.Content>
<StackLayout>
<StackLayout VerticalOptions="Start">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<ImageButton x:Name="SettingsButton"
Source="drawable/icon.png"
WidthRequest="20"
HeightRequest="20"
HorizontalOptions="StartAndExpand"
VerticalOptions="CenterAndExpand"
Command="{Binding SettingsButtonCommand}">
</ImageButton>
</Grid>
</Grid>
</StackLayout>
<StackLayout HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand">
// Code
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
更新
@Jason 推荐使用 DependencyService.
@Jason recommended to use DependencyService.
它运行良好,但现在 Spotlight 代码出现问题.在 Control\SpotLightService.cs
中,Target()
需要一个名为 view
的参数.当我使用 Xamarin.Forms.View 视图
时,它说 Cannot convert form 'Xamarin.Forms.ImageButton' to 'Android.Views.View'
.
It is working fine, but now their is a problem with Spotlight Code. In Control\SpotLightService.cs
, the Target()
required a Parameter called view
. When I use the Xamarin.Forms.View view
, it says Cannot convert form 'Xamarin.Forms.ImageButton' to 'Android.Views.View'
.
但是当我使用 Android.View
时,在 Views/IntroPage.xml.cs
中我无法访问我想要的 SettingsButton
聚焦.
But when I use Android.View
, in Views/IntroPage.xml.cs
I can't access SettingsButton
, which I want to Spotlight.
视图/IntroPage.xml.cs
public partial class IntroPage : ContentPage
{
public IntroPage()
{
DependencyService.Get<ISpotLight>().ShowIntro(SettingsButton, "settings");
InitializeComponent();
}
}
Services\ISpotLight.cs
using Xamarin.Forms;
namespace Mobile.App.Services
{
public interface ISpotLight
{
void ShowIntro(View view, string usageId);
}
}
Control\SpotLightService.cs
使用来自 AndroidSpotlight
[assembly: Xamarin.Forms.Dependency(typeof(SpotLightService))]
namespace Mobile.App.Droid.Control
{
public class SpotLightService : ISpotLight
{
private bool isRevealEnabled = true;
private SpotlightView spotLight;
public void ShowIntro(Xamarin.Forms.View view, string usageId)
{
spotLight = new SpotlightView.Builder((Activity)Application.Context)
.IntroAnimationDuration(400)
.EnableRevealAnimation(isRevealEnabled)
.PerformClick(true)
.FadeinTextDuration(400)
.HeadingTvColor(Color.ParseColor("#eb273f"))
.HeadingTvSize(32)
.HeadingTvText("Love")
.SubHeadingTvColor(Color.ParseColor("#ffffff"))
.SubHeadingTvSize(16)
.SubHeadingTvText("Like the picture?\nLet others know.")
.MaskColor(Color.ParseColor("#dc000000"))
.Target(view)
.LineAnimDuration(400)
.LineAndArcColor(Color.ParseColor("#eb273f"))
.DismissOnTouch(true)
.DismissOnBackPress(true)
.EnableDismissAfterShown(true)
.UsageId(usageId)
.ShowTargetArc(true)
.Show();
}
}
}
推荐答案
Convert
Xamarin.Forms.View
到Android.Views.View
,你可以试试下面的代码.Convert
Xamarin.Forms.View
toAndroid.Views.View
, you can try the following code .public View ConvertFormsToNative(Xamarin.Forms.View view) { var vRenderer = Platform.CreateRendererWithContext(view, MainActivity.Instance); var Androidview = vRenderer.View; vRenderer.Tracker.UpdateLayout(); var size = view.Bounds; var layoutParams = new ViewGroup.LayoutParams((int)size.Width, (int)size.Height); Androidview.LayoutParameters = layoutParams; view.Layout(size); Androidview.Layout(0, 0, (int)view.WidthRequest, (int)view.HeightRequest); Androidview.SetBackgroundColor(Color.Red); return Androidview; }
并且
target
应该设置为.Target(ConvertFormsToNative(view))
and the
target
should be set as.Target(ConvertFormsToNative(view))
更改
new SpotlightView.Builder((Activity)Application.Context)
到new SpotlightView.Builder(MainActivity.Instance)
.Instance
是在MainActivity
中定义的字段.Instance
is a field defined inMainActivity
.public static MainActivity Instance; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); Instance = this;
移动
依赖服务
的调用从构造函数到LayoutChanged
事件.Move the call of
Dependency service
from constructor toLayoutChanged
event.public Page1() { InitializeComponent(); this.LayoutChanged += Page1_LayoutChanged; } bool isShown = false; private void Page1_LayoutChanged(object sender, EventArgs e) { if (!isShown) { DependencyService.Get<ISpotLight>().ShowIntro(SettingsButton, "settings"); isShown = true; } }
这篇关于如何在 Xamarin.Forms 页面中使用 Spotlight Xamarin.Android 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!