You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET MAUI Android中处理IntentFilter并传递内容到MainPage

.NET MAUI Android接收分享内容传递到MainPage解决方案

步骤1:完善MainActivity接收分享数据并发送消息

在MainActivity.cs中重写OnCreate和OnNewIntent方法,提取分享内容后,通过MessagingCenter将数据传递给MainPage:

using Android.Content;
using Microsoft.Maui;
using Microsoft.Maui.Controls;

namespace YourAppNamespace; // 替换为你的应用实际命名空间

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[IntentFilter(new[] { Intent.ActionSend },
          Categories = new[] {
            Intent.CategoryDefault
          },
          DataMimeType = "*/*")]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // 处理应用首次启动时的分享请求
        HandleShareIntent(Intent);
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        // 处理应用已启动状态下的分享请求
        HandleShareIntent(intent);
    }

    private void HandleShareIntent(Intent intent)
    {
        if (intent.Action != Intent.ActionSend) return;

        string sharedContent = null;
        // 处理文本类分享(比如YouTube的链接)
        if (intent.Type?.StartsWith("text/") == true)
        {
            sharedContent = intent.GetStringExtra(Intent.ExtraText);
        }
        // 如果需要处理图片/文件分享,可添加以下逻辑
        // else if (intent.Type?.StartsWith("image/") == true)
        // {
        //     var fileUri = (Android.Net.Uri)intent.GetParcelableExtra(Intent.ExtraStream);
        //     // 转换为MAUI可访问的路径并处理
        // }

        if (!string.IsNullOrEmpty(sharedContent))
        {
            // 发送消息到MainPage
            MessagingCenter.Send(this, "ShareReceived", sharedContent);
        }
    }
}

步骤2:在MainPage中订阅消息并处理数据

修改MainPage.xaml.cs,订阅MessagingCenter的消息,接收并展示分享内容:

using Microsoft.Maui.Controls;

namespace YourAppNamespace; // 替换为你的应用实际命名空间

public partial class MainPage : ContentPage
{
    int count = 0;

    public MainPage()
    {
        InitializeComponent();
        // 订阅分享消息
        MessagingCenter.Subscribe<MainActivity, string>(this, "ShareReceived", (sender, content) =>
        {
            // 确保在UI线程更新界面
            MainThread.BeginInvokeOnMainThread(() =>
            {
                // 这里替换为你的业务逻辑,比如显示分享内容
                CounterBtn.Text = $"收到分享:{content}";
            });
        });
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        // 取消订阅,避免内存泄漏
        MessagingCenter.Unsubscribe<MainActivity, string>(this, "ShareReceived");
    }

    private void OnCounterClicked(object sender, EventArgs e)
    {
        count++;

        if (count == 1)
            CounterBtn.Text = $"Clicked {count} time";
        else
            CounterBtn.Text = $"Clicked {count} times";

        SemanticScreenReader.Announce(CounterBtn.Text);
    }

    private void btnDemo_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new DemoPage());
    }
}

补充说明

  • 如果需要处理文件类分享,可在HandleShareIntent中解析Intent.ExtraStream获取文件URI,再转换为MAUI可访问的本地路径。
  • 除了MessagingCenter,也可以使用依赖注入或弱引用实现跨组件通信,但MessagingCenter是当前场景下最直接的方案。
  • 务必在页面消失时取消MessagingCenter订阅,防止内存泄漏。

内容的提问来源于stack exchange,提问作者carlos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 11:15:31