【问题标题】:How to use Spotlight Xamarin.Android Library in Xamarin.Forms Pages如何在 Xamarin.Forms 页面中使用 Spotlight Xamarin.Android 库
【发布时间】: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


【解决方案1】:
  • 转换 Xamarin.Forms.ViewAndroid.Views.View ,你可以试试下面的代码。

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

  • 更改 new SpotlightView.Builder((Activity)Application.Context)new SpotlightView.Builder(MainActivity.Instance)

    InstanceMainActivity 中定义的字段。

       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;
    
  • 移动Dependency service的调用从构造函数到LayoutChanged事件。

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

【讨论】:

  • 你是最棒的......谢谢......只是一件小事,我基本上有这 3 个按钮(左上角的第一个,右上角的第二个,底部中心的第三个)。 .我希望这条白色动画线从组件所在的位置开始..现在,所有(其中3个)都从左上角开始..为什么?
  • 能否展示运行图像来详细说明一下?
  • 我在上面添加了一张图片,请看一下
  • 尝试将Androidview.Layout(0, 0, (int)view.WidthRequest, (int)view.HeightRequest);改为Androidview.Layout((int)size.X, (int)size.Y, (int)view.WidthRequest, (int)view.HeightRequest);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 2018-08-03
  • 2020-05-24
  • 1970-01-01
  • 2017-08-31
相关资源
最近更新 更多