我需要通过Xamarin Forms中创建的按钮打开安装在手机上的Slack应用程序。我拥有的是在共享项目中创建的界面和"主页"中按钮的单击事件。后来我在project.android中创建了一个类,该类进行依赖注入以创建一个实现,以通过"意图"打开特定应用程序
/////Interface
namespace PracticaXamarin
{
public interface IOpenAppService
{
Task<bool> Launch(string stringUri);
}
}
/////Event click of the button on MainPage
namespace PracticaXamarin
{
public partial class MainPage : ContentPage
{
void Handle_Clicked(object sender, System.EventArgs e)
{
DependencyService.Get<IOpenAppService>().Launch("slack");
//DependencyService.Get<IOpenAppService> ().Launch("com.whatsapp");
}
public MainPage()
{
InitializeComponent();
}
}
}
/////The class created in the project.android
[assembly: Xamarin.Forms.Dependency(typeof(OpenAppService))]
namespace PracticaXamarin.Droid
{
public class OpenAppService : Activity, IOpenAppService
{
public Task<bool> Launch(string stringUri)
{
try
{
Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(stringUri);
if (intent != null)
{
intent.AddFlags(ActivityFlags.NewTask);
Forms.Context.StartActivity(intent);
}
else
{
intent = new Intent(Intent.ActionView);
intent.AddFlags(ActivityFlags.NewTask);
intent.SetData(Android.Net.Uri.Parse("market://details?id=" + stringUri)); // This launches the PlayStore and search for the app if it's not installed on your device
Forms.Context.StartActivity(intent);
}
return Task.FromResult(true);
}
catch{
return Task.FromResult(false);
}
}
}
}
我能做的是传递一些应用程序的"意图",例如WhatSapp的"意图",如注释行所示,这将打开WhatSapp应用程序
//DependencyService.Get<IOpenAppService>().Launch("com.whatsapp");
但是我找不到打开Slack应用程序的方法。Slack 是否有任何我"意图"?
可以通过 Xamarin.Essentials NuGet 包使用 Slack "深层链接",并跳过依赖项服务:
await Xamarin.Essentials.Launcher.OpenAsync("slack://open");
用法示例:
if (await Xamarin.Essentials.Launcher.CanOpenAsync("slack://"))
{
await Xamarin.Essentials.Launcher.OpenAsync("slack://open");
}
else
{
// Load the Store page for the Stack App... or you could just open the device's browser to https://slack.com....
if (Xamarin.Essentials.DeviceInfo.Platform == Xamarin.Essentials.DevicePlatform.iOS)
await Xamarin.Essentials.Browser.OpenAsync("https://itunes.apple.com/app/slack-app/id618783545?ls=1&mt=8");
else if (Xamarin.Essentials.DeviceInfo.Platform == Xamarin.Essentials.DevicePlatform.Android)
await Xamarin.Essentials.Browser.OpenAsync("https://play.google.com/store/apps/details?id=com.Slack&hl=en_US");
else
Console.WriteLine("Something else..? Launch the device browser? ....");
}
回复:https://api.slack.com/docs/deep-linking(向下跳至"深度链接 slack://"部分(
使用 Slack 的自定义 uri 方案
Device.OpenUri(new Uri("slack://open"));