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

请求分步指导:在MAUI Blazor项目中实现DeepLinking及相关资源

.NET MAUI Blazor 实现DeepLinking 分步指南

一、基础项目准备

  • 创建新的.NET MAUI Blazor项目(使用Visual Studio官方模板)
  • 确保项目目标框架为.NET 7及以上版本

二、平台配置(Android + iOS)

Android 配置

  1. 打开Platforms/Android/AndroidManifest.xml,在<activity>标签内添加Intent过滤器,用于捕获自定义Scheme的DeepLink:
<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- 替换为你的自定义Scheme,例如myapp:// -->
    <data android:scheme="myapp" />
</intent-filter>
  1. 将Activity的LaunchMode设置为SingleTop,避免重复启动应用:
<activity android:name=".MainActivity"
          android:launchMode="singleTop"
          ...>

iOS 配置

  1. 打开Platforms/iOS/Info.plist,添加URL Types配置,注册自定义Scheme:
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <!-- 使用和Android一致的自定义Scheme -->
            <string>myapp</string>
        </array>
    </dict>
</array>

三、MAUI 核心层传递DeepLink到Blazor

在App.xaml.cs中重写OnAppLinkRequestReceived方法,捕获外部传入的DeepLink并传递给Blazor WebView:

protected override async void OnAppLinkRequestReceived(Uri uri)
{
    base.OnAppLinkRequestReceived(uri);
    
    if (MainPage is BlazorWebView blazorWebView)
    {
        // 通过JS桥接将Uri传递给Blazor前端
        await blazorWebView.WebView.EvaluateJavaScriptAsync($"handleDeepLink('{Uri.EscapeDataString(uri.ToString())}')");
    }
}

四、Blazor 前端处理路由跳转

  1. 在wwwroot/index.html中添加全局JS函数,接收MAUI传递的DeepLink并触发Blazor导航:
function handleDeepLink(encodedUri) {
    const uri = decodeURIComponent(encodedUri);
    const url = new URL(uri);
    // 提取路径部分,例如myapp://products/123的路径为/products/123
    const path = url.pathname;
    // 调用Blazor内置导航方法跳转对应页面
    window.Blazor.navigateTo(path);
}
  1. 创建对应路由的Razor页面,例如处理/products/{id}的页面:
@page "/products/{id}"
<h3>产品详情页</h3>
<p>产品ID: @Id</p>

@code {
    [Parameter]
    public string Id { get; set; }
}
  • Android:使用ADB命令测试
    adb shell am start -W -a android.intent.action.VIEW -d "myapp://products/123" 你的应用包名
    
  • iOS:在模拟器中通过Safari输入myapp://products/123,或使用Xcode命令:
    xcrun simctl openurl booted "myapp://products/123"
    

六、入门参考资源

  • .NET MAUI官方文档「Deep Linking」章节:覆盖平台配置、核心逻辑的基础讲解,适合入门
  • 官方MAUI示例仓库:包含完整的MAUI Blazor DeepLinking实现项目,可直接参考项目结构与代码

内容的提问来源于stack exchange,提问作者P Sharath Chandra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 08:28:30