【发布时间】:2021-10-13 04:47:33
【问题描述】:
我想安装一个库以在用户打开应用程序时首次聚焦First Page。为此,我找到了一个名为 AndroidSpotlight 的神奇库,但问题是我无法在 Xamarin.Forms 项目中安装它,因为它仅适用于 Xamarin.Android。
当我尝试为Xamarin.Forms 项目安装它时,它给了我这个错误。
Package Android.Spotlight 2019.11.14.1 is not compatible with netstandard2.1 (.NETStandard,Version=v2.1). Package Android.Spotlight 2019.11.14.1 supports: monoandroid10 (MonoAndroid,Version=v1.0)
但是第一页在Xamarin.Forms,那我怎么能用他们的这个库呢?
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。
它工作正常,但现在他们是Spotlight Code 的问题。在Control\SpotLightService.cs 中,Target() 需要一个名为view 的参数。当我使用Xamarin.Forms.View view 时,它会显示Cannot convert form 'Xamarin.Forms.ImageButton' to 'Android.Views.View'。
但是当我在Views/IntroPage.xml.cs 中使用Android.View 时,我无法访问我想要聚焦的SettingsButton。
Views/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();
}
}
}
【问题讨论】:
-
使用 DependencyService 从核心 Forms 项目访问特定于平台的库
-
谢谢,我找到了这篇文章alexdunn.org/2017/02/21/… .. 你指的是这个吗?
-
顺便说一句,当您查看@Jason 提到的文档时,不要犯我犯的错误。我点击了“注册和解决”,因为我没有意识到简介是一个单独的页面;我以为只有那一段。我直接点击了低级细节!请务必先点击"Introduction"。 (也可以从页面左侧的导航层次结构到达那里)。
-
谢谢,我使用了 DependencyService,但是他们使用它是一个小问题。我更新了上面的代码,请看一下。谢谢
标签: c# .net xamarin xamarin.forms