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

迁移至.NET MAUI后,如何访问原Xamarin.Forms的Application.Properties?

解决.NET MAUI迁移中无法访问旧Xamarin.Forms Application.Properties 的问题

方法一:临时禁用过时错误(快速迁移)

MAUI中Application.Properties的过时标记是IsError=true,触发的是CS0619编译错误,而非普通警告。之前的#pragma指令失效是因为未指定错误码,正确用法如下:

局部代码块禁用

在迁移代码的前后添加指令,仅临时允许访问旧API:

#pragma warning disable CS0619
// 读取旧Properties数据
var oldProperties = Application.Current.Properties;
#pragma warning restore CS0619

// 迁移至Preferences
foreach (var kvp in oldProperties)
{
    // 根据实际数据类型适配转换
    if (kvp.Value is string strVal)
        Preferences.Set(kvp.Key, strVal);
    else if (kvp.Value is bool boolVal)
        Preferences.Set(kvp.Key, boolVal);
    // 其他类型(int、double等)按需处理
}

全局项目禁用(不推荐,仅临时用)

如果需要多处访问,可在项目文件(.csproj)中添加全局禁用规则,迁移完成后务必删除:

<PropertyGroup>
  <NoWarn>$(NoWarn);CS0619</NoWarn>
</PropertyGroup>

方法二:直接读取底层存储(绕过API限制)

如果反射或禁用错误仍触发NotSupportedException,可直接读取Xamarin.Forms存储Properties的底层文件/系统存储:

Android平台

Xamarin.Forms的Properties存储在IsolatedStorage的XML文件中,路径为AppDataDirectory/.config/.isolated-storage/PropertyStore.forms,直接解析即可:

#if ANDROID
using System.Xml.Serialization;

var propFilePath = Path.Combine(FileSystem.AppDataDirectory, ".config", ".isolated-storage", "PropertyStore.forms");
if (File.Exists(propFilePath))
{
    var serializer = new XmlSerializer(typeof(Dictionary<string, object>));
    using var reader = new StreamReader(propFilePath);
    var oldProperties = (Dictionary<string, object>)serializer.Deserialize(reader);

    // 迁移逻辑
    foreach (var kvp in oldProperties)
    {
        // 类型适配存储到Preferences
        Preferences.Set(kvp.Key, kvp.Value?.ToString() ?? string.Empty);
    }

    // 迁移完成后删除旧文件,避免重复迁移
    File.Delete(propFilePath);
}
#endif

iOS平台

Xamarin.Forms的Properties通过NSUserDefaults存储,键为Xamarin.Forms.Properties,用NSKeyedUnarchiver反序列化:

#if IOS
using Foundation;

var defaults = NSUserDefaults.StandardUserDefaults;
var serializedProps = defaults.StringForKey("Xamarin.Forms.Properties");
if (!string.IsNullOrEmpty(serializedProps))
{
    var oldPropsDict = NSKeyedUnarchiver.UnarchiveObject(NSData.FromString(serializedProps)) as NSDictionary;
    if (oldPropsDict != null)
    {
        foreach (var key in oldPropsDict.Keys)
        {
            var value = oldPropsDict[key];
            // 根据NSObject类型转换存储
            if (value is NSString strVal)
                Preferences.Set(key.ToString(), strVal.ToString());
            else if (value is NSNumber numVal)
            {
                if (numVal.Type == NSNumberType.Boolean)
                    Preferences.Set(key.ToString(), numVal.BoolValue);
                else if (numVal.Type == NSNumberType.Int32)
                    Preferences.Set(key.ToString(), numVal.Int32Value);
            }
        }

        // 迁移完成后清理旧数据
        defaults.RemoveObject("Xamarin.Forms.Properties");
        defaults.Synchronize();
    }
}
#endif

注意事项

  1. 迁移逻辑建议仅在应用首次启动MAUI版本时执行,可通过一个标记(如Preferences.Get("IsPropertiesMigrated", false))判断是否已完成迁移。
  2. 迁移完成后,务必删除所有访问Application.Properties的代码,确保仅使用Microsoft.Maui.Storage.Preferences。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 14:25:47