diff --git a/App.xaml b/App.xaml
index 80f30c2..7625f43 100644
--- a/App.xaml
+++ b/App.xaml
@@ -22,6 +22,12 @@
+
+
+
diff --git a/CtrEditor.csproj b/CtrEditor.csproj
index 0ee2c26..98e787c 100644
--- a/CtrEditor.csproj
+++ b/CtrEditor.csproj
@@ -85,8 +85,9 @@
-
+
+
diff --git a/ObjetosSim/Decorativos/ucCustomImage.xaml.cs b/ObjetosSim/Decorativos/ucCustomImage.xaml.cs
index 8525fb1..6b62d43 100644
--- a/ObjetosSim/Decorativos/ucCustomImage.xaml.cs
+++ b/ObjetosSim/Decorativos/ucCustomImage.xaml.cs
@@ -1,7 +1,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using LibS7Adv;
using System.Windows;
-using System.Windows.Controls;
+using System.Windows.Controls;
using System.Windows.Media;
using CtrEditor.FuncionesBase;
using System.ComponentModel;
@@ -27,11 +27,42 @@ namespace CtrEditor.ObjetosSim
}
- [ObservableProperty]
- [NotifyPropertyChangedFor(nameof(ImageSource_oculta))]
- [property: Description("Path to the image file")]
- [property: Category("Image:")]
- private string imagePath;
+ private string _imagePath;
+
+ [Description("Path to the image file")]
+ [Category("Image:")]
+ public string ImagePath
+ {
+ get => _imagePath;
+ set
+ {
+ try
+ {
+ if (SetProperty(ref _imagePath, value))
+ {
+ OnImagePathChanged(value);
+ OnPropertyChanged(nameof(ImageSource_oculta));
+ }
+ }
+ catch
+ {
+ // Si hay error al establecer la propiedad, simplemente ignorarlo
+ // y establecer la imagen por defecto
+ try
+ {
+ SetProperty(ref _imagePath, value);
+ ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
+ OnPropertyChanged(nameof(ImageSource_oculta));
+ }
+ catch
+ {
+ // En caso de error total, establecer valores seguros
+ _imagePath = value;
+ ImageSource_oculta = null;
+ }
+ }
+ }
+ }
[ObservableProperty]
[property: Description("Flip the image horizontally")]
@@ -48,16 +79,32 @@ namespace CtrEditor.ObjetosSim
[ObservableProperty]
public ImageSource imageSource_oculta;
- partial void OnImagePathChanged(string value)
+ private void OnImagePathChanged(string value)
{
- if (!string.IsNullOrEmpty(value))
+ try
{
- ImageSource_oculta = ImageFromPath(value);
+ if (!string.IsNullOrEmpty(value))
+ {
+ ImageSource_oculta = ImageFromPath(value);
+ }
+ else
+ {
+ // Si no hay path, usar la imagen por defecto
+ ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
+ }
}
- else
+ catch
{
- // Si no hay path, usar la imagen por defecto
- ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
+ // Si hay cualquier error, usar la imagen por defecto
+ try
+ {
+ ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
+ }
+ catch
+ {
+ // Si incluso la imagen por defecto falla, establecer como null
+ ImageSource_oculta = null;
+ }
}
}
@@ -82,14 +129,30 @@ namespace CtrEditor.ObjetosSim
public override void ucLoaded()
{
base.ucLoaded();
- if (!string.IsNullOrEmpty(ImagePath))
+ try
{
- ImageSource_oculta = ImageFromPath(ImagePath);
+ if (!string.IsNullOrEmpty(ImagePath))
+ {
+ ImageSource_oculta = ImageFromPath(ImagePath);
+ }
+ else
+ {
+ // Si no hay path al cargar, usar la imagen por defecto
+ ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
+ }
}
- else
+ catch
{
- // Si no hay path al cargar, usar la imagen por defecto
- ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
+ // Si hay cualquier error, usar la imagen por defecto
+ try
+ {
+ ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
+ }
+ catch
+ {
+ // Si incluso la imagen por defecto falla, establecer como null
+ ImageSource_oculta = null;
+ }
}
}
}
diff --git a/ObjetosSim/TagsSignals/ucBoolTag.xaml b/ObjetosSim/TagsSignals/ucBoolTag.xaml
index c80696b..8b937d9 100644
--- a/ObjetosSim/TagsSignals/ucBoolTag.xaml
+++ b/ObjetosSim/TagsSignals/ucBoolTag.xaml
@@ -27,7 +27,7 @@
Width="16" Height="25" VerticalAlignment="Center" HorizontalAlignment="Center"
Background="{Binding Color, Converter={StaticResource ColorToBrushConverter}}" />
diff --git a/ObjetosSim/osBase.cs b/ObjetosSim/osBase.cs
index 11c9501..a2c9e6c 100644
--- a/ObjetosSim/osBase.cs
+++ b/ObjetosSim/osBase.cs
@@ -1642,7 +1642,31 @@ namespace CtrEditor.ObjetosSim
{
if (value is string stringValue)
{
- return new BitmapImage(new Uri(stringValue, UriKind.RelativeOrAbsolute));
+ try
+ {
+ var bitmapImage = new BitmapImage();
+ bitmapImage.BeginInit();
+ bitmapImage.UriSource = new Uri(stringValue, UriKind.RelativeOrAbsolute);
+ bitmapImage.EndInit();
+ return bitmapImage;
+ }
+ catch
+ {
+ // Si falla la carga de la imagen, usar imagen por defecto como fallback
+ try
+ {
+ var defaultImage = new BitmapImage();
+ defaultImage.BeginInit();
+ defaultImage.UriSource = new Uri("/Icons/unselect.png", UriKind.RelativeOrAbsolute);
+ defaultImage.EndInit();
+ return defaultImage;
+ }
+ catch
+ {
+ // Si incluso la imagen por defecto falla, devolver null
+ return null;
+ }
+ }
}
return null;
}
diff --git a/Serialization/StateSerializer.cs b/Serialization/StateSerializer.cs
index 6e3e492..3fa305b 100644
--- a/Serialization/StateSerializer.cs
+++ b/Serialization/StateSerializer.cs
@@ -6,6 +6,7 @@ using System.Windows;
using CtrEditor.Simulacion;
using System.IO;
using System.Linq;
+using System.Windows.Media;
namespace CtrEditor.Serialization
{
@@ -254,8 +255,89 @@ namespace CtrEditor.Serialization
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto,
ObjectCreationHandling = ObjectCreationHandling.Replace,
- ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
+ ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
+ Error = HandleDeserializationError,
+ Converters = { new SafeImageSourceConverter() }
};
}
+
+ private void HandleDeserializationError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs e)
+ {
+ // Si es un error relacionado con ImagePath, ImageSource o osCustomImage, simplemente ignorarlo y continuar
+ if (e.ErrorContext.Error.Message.Contains("ImagePath") ||
+ e.ErrorContext.Error.Message.Contains("osCustomImage") ||
+ e.ErrorContext.Error.Message.Contains("ImageSource") ||
+ e.ErrorContext.Error.Message.Contains("lmageSource") || // posible typo en el mensaje de error
+ (e.ErrorContext.Path != null && e.ErrorContext.Path.Contains("ImageSource")) ||
+ (e.ErrorContext.Path != null && e.ErrorContext.Path.Contains("imagePath")) ||
+ (e.ErrorContext.Member != null && e.ErrorContext.Member.ToString().Contains("Image")))
+ {
+ e.ErrorContext.Handled = true;
+
+ // Si el contexto actual es un objeto osCustomImage y hay un error,
+ // intentar establecer valores por defecto
+ if (e.ErrorContext.OriginalObject is osCustomImage customImage)
+ {
+ try
+ {
+ // Establecer valores seguros por defecto
+ if (e.ErrorContext.Member?.ToString() == "ImagePath" ||
+ e.ErrorContext.Member?.ToString() == "imagePath")
+ {
+ customImage.ImagePath = ""; // Esto activará el fallback a imagen por defecto
+ }
+ }
+ catch
+ {
+ // Si incluso esto falla, simplemente ignorar
+ }
+ }
+ }
+ }
+ }
+
+ ///
+ /// Convertidor JSON seguro para ImageSource que nunca lanza excepciones
+ ///
+ public class SafeImageSourceConverter : JsonConverter
+ {
+ public override ImageSource ReadJson(JsonReader reader, Type objectType, ImageSource existingValue, bool hasExistingValue, JsonSerializer serializer)
+ {
+ try
+ {
+ // Si el valor es null o string vacío, devolver null
+ if (reader.Value == null || reader.Value.ToString() == "")
+ return null;
+
+ // Intentar crear ImageSource desde el valor
+ string value = reader.Value.ToString();
+
+ // Limpiar caracteres problemáticos
+ value = value.Trim();
+ if (value.Contains("•"))
+ value = value.Replace("•", "");
+ if (value.StartsWith("filet///"))
+ value = value.Replace("filet///", "file:///");
+
+ // Si el valor limpio está vacío, devolver null
+ if (string.IsNullOrWhiteSpace(value))
+ return null;
+
+ // No intentar crear ImageSource aquí, simplemente devolver null
+ // La lógica de carga de imagen se manejará en el código de la aplicación
+ return null;
+ }
+ catch
+ {
+ // En caso de cualquier error, devolver null silenciosamente
+ return null;
+ }
+ }
+
+ public override void WriteJson(JsonWriter writer, ImageSource value, JsonSerializer serializer)
+ {
+ // Nunca serializar ImageSource, siempre escribir null
+ writer.WriteNull();
+ }
}
}
\ No newline at end of file