xamarin - 如何使用 Xamarin Forms 中的按钮打开手机上安装的 Slack 应用程序?
问题描述
我需要通过在 Xamarin Forms 中创建的按钮打开手机上安装的 Slack 应用程序。我所拥有的是在共享项目中创建的界面和“MainPage”中按钮的单击事件。后来我在 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 有没有我的“意图”?
解决方案
您可以通过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:// 深度链接”部分)
推荐阅读
- c++ - 模板功能图
- laravel - 未找到存储数据 RESTfull API 404
- reactjs - React JS 多个提交按钮 react-hook-form
- python - 如何在python中将不同年份之间的距离分成12个相等的部分
- python - 通过 Google Docs API Python 客户端库创建新的 Google 文档的命令是什么?
- angular - 从 Api 服务调用角度选择默认下拉列表
- c++ - 编译器找不到 std::variant 声明
- python - python 元组以逗号结尾
- php - 在 Laravel 8 中将图表刀片视图转换为 pdf
- c# - c#和XPath,需要帮助提取2段文本