我需要通过Xamarin Forms中创建的按钮打开安装在手机上的Slack应用程序。 我所拥有的是在共享项目中创建的界面以及“ MainPage”中按钮的clicked事件。 后来我在project.android中创建了一个类,该类进行了依赖注入,以通过“ Intent”创建实现以打开特定应用程序
/////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有什么我的“意图”吗?
答案 0 :(得分:2)
您可以通过Xamarin.Essentials NuGet Package使用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? ....");
}
re:https://api.slack.com/docs/deep-linking(跳至“使用slack://进行深度链接”部分)
答案 1 :(得分:0)
Device.OpenUri(new Uri("slack://open"));