S7Explorer/App.xaml.cs

88 lines
3.4 KiB
C#

using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
namespace S7Explorer
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Asegurarnos de que existe la carpeta Resources para los íconos
EnsureResourcesExist();
// Configurar manejo de excepciones no controladas
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
}
private void EnsureResourcesExist()
{
try
{
// Directorio de recursos
string resourcesDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources");
if (!Directory.Exists(resourcesDir))
{
Directory.CreateDirectory(resourcesDir);
}
// Crear íconos básicos si no existen
CreateDefaultIconIfNotExists(resourcesDir, "project.png");
CreateDefaultIconIfNotExists(resourcesDir, "device.png");
CreateDefaultIconIfNotExists(resourcesDir, "folder.png");
CreateDefaultIconIfNotExists(resourcesDir, "db.png");
CreateDefaultIconIfNotExists(resourcesDir, "fb.png");
CreateDefaultIconIfNotExists(resourcesDir, "fc.png");
CreateDefaultIconIfNotExists(resourcesDir, "ob.png");
CreateDefaultIconIfNotExists(resourcesDir, "symbol.png");
CreateDefaultIconIfNotExists(resourcesDir, "default.png");
}
catch (Exception)
{
// Ignorar errores en la creación de recursos - no son críticos
}
}
private void CreateDefaultIconIfNotExists(string directory, string filename)
{
string filePath = Path.Combine(directory, filename);
if (!File.Exists(filePath))
{
// Crear un ícono simple - en una aplicación real, incluirías recursos reales
BitmapSource bmp = BitmapSource.Create(16, 16, 96, 96, System.Windows.Media.PixelFormats.Bgr32, null, new byte[16 * 16 * 4], 16 * 4);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(fileStream);
}
}
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
HandleException(e.ExceptionObject as Exception);
}
private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
HandleException(e.Exception);
e.Handled = true;
}
private void HandleException(Exception ex)
{
if (ex == null) return;
MessageBox.Show($"Ha ocurrido un error inesperado: {ex.Message}\n\nDetalles: {ex.StackTrace}",
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
// Aquí podrías añadir registro de errores
}
}
}