需要使用类Intent
来做不同的事情。在网上找到了使用示例using Android.Content
。这不适用于Xamarin.Forms(我认为)。它需要什么NuGet软件包才能工作?
我从移动应用程序开发开始,所以我对任何事情一无所知。希望在这里得到一个简洁的答案。
我的应用程序主要是WebView
带有其他一些次要功能的容器。我正在使用Visual Studio 2019和Xamarin.Forms,但此刻我正在单独编译为Android。iOS和UWP曾经是我的目标,至少一个应用程序已完全针对Android完成。
现在,我需要添加两件事。一个是“为我们评分”,另一个是“共享”链接,两者均在主菜单中(从左侧滑动或单击汉堡包图标将其打开)。我已经在菜单中有三个列表项,其中每个列表项都会在应用程序中打开一个页面。
我想出了如何向菜单中添加其他项目,但是目前我将此列表项链接到PlayStoremarket://details?id=com.mycompany.myapp
上我的应用程序的网址。它可以在PlayStore中很好地打开,但是按返回按钮后,它将返回主屏幕,而不是返回到应用程序。
using MyApp.Models;
using Plugin.Geolocator;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MyApp.Views {
...
public partial class MainPage : MasterDetailPage {
...
public async Task NavigateFromMenu(int id) {
if (!MenuPages.ContainsKey(id) {
switch(id) {
...
case (int)MenuItemType.RateUs:
await Browser.OpenAsync("market://details?id=com.mycompany.myapp", BrowserLaunchMode.SystemPreferred);
// startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.mycompany.myapp")));
break;
...
}
}
...
}
}
}
using MyApp.Models;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
namespace MyApp.Views {
...
public partial class MenuPage : ContentPage {
...
public MenuPage() {
InitializeComponent();
menuItems = new List<HomeMenuItem> {
...
new HomeMenuItem { id = MenuItemType.RateUs, Title="Rate Us", Icon="\uf005" },
...
}
}
}
}
namespace MyApp.Models {
public enum MenuItemType {
...
RateUs,
...
}
public class HomeMenuItem {
public MenuItemType Id { get; set; }
public string Title { get; set; }
public string Icon { get; set; }
}
}
正如您//
在MainPage.xaml.cs的注释中看到的那样,我找到了一个不同的解决方案,从PlayStore返回后,这也许会使我返回到我的应用程序。
此特定解决方案在(startActivity
工具提示中)指出了(显然?)
名称“ startActivity”在当前上下文中不存在
但也有其他类似的更复杂的解决方案这一个已经startActivity
包裹在public void function()
其中-我猜-帮助。
我需要在侧菜单“共享”中添加另一个列表项,设备将询问您要与哪个应用共享(或复制到剪贴板),然后使用该应用共享一些文本和我选择的链接(相同适用于所有用户)。同样,我发现的任何LINK都使用Intent
。
在这里完全不知所措。如果您不能给我工具和使用手册,请至少向我指明正确的方向。
有两种方法可以在您的App中调用Intent类,其中一种方法,最好的方法是使用DependencyService
。
从文档中:
DependencyService类是一个服务定位器,使Xamarin.Forms应用程序可以从共享代码中调用本机平台功能。
从文档中可以看到,首先要做的是创建一个接口来定义要实现的方法。在我们的例子中,我将创建一个简单的接口,该接口定义一个称为的方法LaunchAppInPlayStore
(您可以调用任意方法!):
namespace App2
{
public interface Interface1
{
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")));
}
}
}
您应该注意,在我的示例中,我使用了@JamesMontemagnoContext
的CurrentActivity插件提供的功能(这只是偶然的情况,在我自己的应用中,我自己使用了另一种SO Post中介绍的不同方法(请参阅添加MainApplication
类的部分)获取Current Context
,或查看@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>
在后面的代码中,我只是LaunchAppOnPlayStore
按如下方式调用方法:
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();
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句