C# 哪个包应该用在Xamarin表单中而不是用在Android.Content中才能获得类的意图? TL;博士:

C# 哪个包应该用在Xamarin表单中而不是用在Android.Content中才能获得类的意图? TL;博士:,c#,android-intent,xamarin.forms,C#,Android Intent,Xamarin.forms,需要对不同的事物使用classIntent。在线找到使用Android.Content的示例。这不适用于Xamarin.Forms(我认为)。需要什么NuGet包才能工作 不。。我有时间: 简介(不必要的阅读) 我是从手机应用开发开始的,所以我对任何事情都一无所知。希望在这里得到一个简洁的答案 我的应用程序主要是WebView容器,还有一些其他次要功能。我正在使用Visual Studio 2019和Xamarin.Forms,但目前我正在编译AndroidiOS和UWP是我的目标有一次,至少有

需要对不同的事物使用class
Intent
。在线找到使用Android.Content的
示例。这不适用于Xamarin.Forms(我认为)。需要什么NuGet包才能工作

不。。我有时间: 简介(不必要的阅读) 我是从手机应用开发开始的,所以我对任何事情都一无所知。希望在这里得到一个简洁的答案

我的应用程序主要是
WebView
容器,还有一些其他次要功能。我正在使用Visual Studio 2019和Xamarin.Forms,但目前我正在编译AndroidiOSUWP是我的目标有一次,至少有一个应用程序在Android上完全完成

问题1我在哪里 现在我需要补充两件事。一个是“Rate us”,另一个是“Share”链接,两者都在主菜单中(从左侧滑动或单击汉堡图标打开)。我在菜单中已经有三个列表项,每个列表项在应用程序中打开一个页面

我知道如何在菜单中添加另一项,但我目前将此列表项链接到
market://details?id=com.mycompany.myapp
PlayStore上我的应用程序的url。它在PlayStore中很好地打开,但按下后退按钮后,它将进入主屏幕,而不是返回应用程序

当前代码,其中我添加了“给我们打分” MainPage.xaml.cs MenuPage.xaml.cs 我试过的 正如您在MainPage.xaml.cs中的
/
注释中所看到的,我找到了一个不同的解决方案,在从PlayStore返回后,它可能会将我返回到我的应用程序

这个特殊的解决方案在
startActivity
,工具提示中指出(显然?)存在问题

当前上下文中不存在名称“startActivity”

但还有其他更为复杂的解决方案,比如将
startActivity
包装在
public void function()
中,我想这会有所帮助

问题2 我需要将另一个列表项添加到标题为“共享”的侧菜单中,设备将询问您要与哪个应用程序共享(或复制到剪贴板),然后使用该应用程序共享我选择的一些文本和链接(对所有用户相同)。同样,我发现的任何内容都使用
Intent


在这里完全不知所措。如果你不能给我工具和如何使用它们的手册,请至少给我指出正确的方向。

在你的应用程序中调用Intent类有几种方法,其中之一,也许最好的方法就是利用

从docu:

DependencyService类是一个服务定位器,它使Xamarin.Forms应用程序能够从共享代码调用本机平台功能

您的用例的小示例 从docu中可以看到,首先要创建一个接口来定义要实现的方法。在本例中,我将创建一个简单的接口,该接口定义一个名为
LaunchAppInPlayStore
的方法(您可以随意调用它!):

名称空间App2
{
公共接口1
{
void LaunchAppOnPlayStore();
}
}
让我们继续在Android项目中实现它:为此,我在其中添加了一个名为
MyInterfaceImplementationAndroid
的类,并使其继承自
Interface1
。然后,我从接口实现该方法,如下所示:

using Xamarin.Forms;
using Android.Content;
using Plugin.CurrentActivity;

[assembly: Dependency(typeof(App2.Droid.MyInterfaceImplementationAndroid))]

namespace App2.Droid
{
    class MyInterfaceImplementationAndroid : Interface1
    {
        public void LaunchAppOnPlayStore()
        {
            CrossCurrentActivity.Current.AppContext.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=com.mycompany.myapp")));
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App2
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TestInyection : ContentPage
    {
        public TestInyection()
        {
            InitializeComponent();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            DependencyService.Get<Interface1>().LaunchAppOnPlayStore();
        }
    }
}
您应该注意到,在我的示例中,我使用@JamesMontemagno提供的CurrentActivity插件提供的
上下文
(这只是间接的,在我自己的应用程序中,我自己使用的方法与中所述的不同)(请参见我添加
main应用程序
类以获取当前
上下文
的部分,或者参见@DavidBritch(值得一读!))

我们几乎完成了!现在您只需在共享代码中调用该方法,如下所示: 在本例中,我创建了一个带有按钮的简单页面:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App2.TestInyection">
    <ContentPage.Content>
        <StackLayout>
            <Button Text="Go to shop!"
                    Clicked="Button_Clicked"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
using Xamarin.Forms;
using Android.Content;
using Plugin.CurrentActivity;

[assembly: Dependency(typeof(App2.Droid.MyInterfaceImplementationAndroid))]

namespace App2.Droid
{
    class MyInterfaceImplementationAndroid : Interface1
    {
        public void LaunchAppOnPlayStore()
        {
            CrossCurrentActivity.Current.AppContext.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=com.mycompany.myapp")));
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App2.TestInyection">
    <ContentPage.Content>
        <StackLayout>
            <Button Text="Go to shop!"
                    Clicked="Button_Clicked"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App2
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TestInyection : ContentPage
    {
        public TestInyection()
        {
            InitializeComponent();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            DependencyService.Get<Interface1>().LaunchAppOnPlayStore();
        }
    }
}