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

Xamarin应用关联自定义扩展名文件并跳转新建页面的实现咨询

Xamarin 跨平台自定义扩展名关联及跳转导入页面实现方案

一、Android 平台配置(针对*.dinero扩展名)

  1. 找到Android项目下的MainActivity.cs文件,在MainActivity类上方添加如下IntentFilter声明,用于注册文件关联:
[IntentFilter(new[] { Intent.ActionView },
    Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataScheme = "content",
    DataHost = "*",
    DataMimeType = "application/octet-stream",
    DataPathPattern = ".*\\.dinero")]
[IntentFilter(new[] { Intent.ActionView },
    Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataScheme = "file",
    DataHost = "*",
    DataMimeType = "application/octet-stream",
    DataPathPattern = ".*\\.dinero")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    // 原有代码保持不变
}
  1. 继续在MainActivity中重写OnCreate和OnNewIntent方法,接收外部打开的文件数据:
protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    // 处理启动应用时直接打开dinero文件的场景
    HandleOpenDineroFile(Intent);
    LoadApplication(new App());
}

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    // 处理应用已在后台运行时打开dinero文件的场景
    HandleOpenDineroFile(intent);
}

private void HandleOpenDineroFile(Intent intent)
{
    if (intent.Action == Intent.ActionView && intent.Data != null)
    {
        // 读取文件内容,可根据自定义格式解析为支出条目对象
        var fileUri = intent.Data;
        using var stream = ContentResolver.OpenInputStream(fileUri);
        using var reader = new StreamReader(stream);
        var fileContent = reader.ReadToEnd();
        
        // 通知共享项目触发导入逻辑
        MessagingCenter.Send("OpenDineroFile", "ImportNewItem", fileContent);
    }
}

二、iOS 平台配置

  1. 找到iOS项目下的Info.plist文件,右键选择用XML编辑器打开,添加如下配置注册自定义扩展名:
<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>Dinero支出文件</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.你的应用包名.dinero</string>
        </array>
    </dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeIdentifier</key>
        <string>com.你的应用包名.dinero</string>
        <key>UTTypeDescription</key>
        <string>Dinero支出文件</string>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>dinero</string>
            </array>
        </dict>
    </dict>
</array>

注意替换配置中的com.你的应用包名为你自己应用的实际包名。

  1. 找到iOS项目下的AppDelegate.cs文件,重写OpenUrl方法接收文件数据:
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
    if (url.PathExtension.ToLower() == "dinero")
    {
        // 读取文件内容
        var fileContent = File.ReadAllText(url.Path);
        // 通知共享项目触发导入逻辑
        MessagingCenter.Send("OpenDineroFile", "ImportNewItem", fileContent);
        return true;
    }
    return base.OpenUrl(app, url, options);
}

三、共享项目跳转NewItemPage逻辑

在共享项目的App.xaml.cs构造函数中订阅消息,收到导入请求后直接跳转NewItemPage:

public App()
{
    InitializeComponent();
    // 你的原有主页面配置保持不变,示例:
    MainPage = new NavigationPage(new MainPage());
    
    // 订阅导入文件的消息
    MessagingCenter.Subscribe<string, string>("OpenDineroFile", "ImportNewItem", async (sender, fileContent) => 
    {
        // 按你自定义的格式解析文件内容为支出条目对象
        var importItem = ParseDineroFile(fileContent);
        // 导航到NewItemPage并传入解析好的条目数据
        await MainPage.Navigation.PushAsync(new NewItemPage(importItem));
    });
}

// 实现你自己的文件解析逻辑
private 你的条目实体类 ParseDineroFile(string content)
{
    // 按*.dinero的存储规则解析返回条目对象即可
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 21:36:04