diff --git a/common.props b/common.props
index f38de64..7d58532 100644
--- a/common.props
+++ b/common.props
@@ -7,7 +7,7 @@
10.0.19041.41
- 1.0.9
+ 1.0.10
ar_128.png
en
https://github.com/AndreasReitberger/MauiSettings
diff --git a/src/MauiSettings/Helper/MauiSettingsHelper.cs b/src/MauiSettings/Helper/MauiSettingsHelper.cs
index 50de3df..12566f1 100644
--- a/src/MauiSettings/Helper/MauiSettingsHelper.cs
+++ b/src/MauiSettings/Helper/MauiSettingsHelper.cs
@@ -1,9 +1,18 @@
using Newtonsoft.Json;
+#if WINDOWS
+using System.Text;
+#endif
namespace AndreasReitberger.Maui.Helper
{
internal class MauiSettingsHelper
{
+ /*
+ LocalSettings restricts the preference key names to 255 characters or less. Each preference value can be up to 8K bytes in size,
+ and each composite setting can be up to 64 K bytes in size.
+ */
+ public static int MaxKeyLength { get; set; } = 255;
+ public static int MaxContentSize { get; set; } = 8 * 1024;
#region Methods
// Docs: https://docs.microsoft.com/en-us/dotnet/maui/platform-integration/storage/preferences
/*
@@ -18,6 +27,10 @@ internal class MauiSettingsHelper
*/
public static T? GetSettingsValue(string key, Type? targetType, T? defaultValue)
{
+#if WINDOWS
+ ArgumentOutOfRangeException.ThrowIfNullOrEmpty(key, nameof(key));
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(key.Length, MaxKeyLength, nameof(key));
+#endif
object? returnValue = null;
if (targetType != defaultValue?.GetType())
{
@@ -81,6 +94,10 @@ internal class MauiSettingsHelper
[Obsolete("Use the new method with the `targetType` parameter instead")]
public static T? GetSettingsValue(string key, T defaultValue)
{
+#if WINDOWS
+ ArgumentOutOfRangeException.ThrowIfNullOrEmpty(key, nameof(key));
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(key.Length, MaxKeyLength, nameof(key));
+#endif
object? returnValue = null;
try
{
@@ -150,6 +167,10 @@ public static async Task GetSecureSettingsValueAsync(string key, string?
public static void SetSettingsValue(string key, object? value)
{
+#if WINDOWS
+ ArgumentOutOfRangeException.ThrowIfNullOrEmpty(key, nameof(key));
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(key.Length, MaxKeyLength, nameof(key));
+#endif
switch (value)
{
case bool b:
@@ -176,7 +197,15 @@ public static void SetSettingsValue(string key, object? value)
default:
// For all other types try to serialize it as JSON
string? jsonString = JsonConvert.SerializeObject(value, Formatting.Indented);
- Preferences.Set(key, jsonString);
+ if (!string.IsNullOrWhiteSpace(jsonString))
+ {
+#if WINDOWS && DEBUG
+ // For testing, at the moment only for debugging
+ byte[] bytes = Encoding.Default.GetBytes(jsonString);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(bytes.LongLength, MaxContentSize, nameof(value));
+#endif
+ Preferences.Set(key, jsonString);
+ }
break;
}
}
@@ -194,6 +223,6 @@ public static async Task SetSecureSettingsValueAsync(string key, string value)
public static void ClearSecureSettings() => SecureStorage.Default.RemoveAll();
- #endregion
+ #endregion
}
}