Skip to content

Overview

github-actions[bot] edited this page Oct 15, 2025 · 2 revisions

The ElectronNET.Core API provides comprehensive access to Electron's native desktop functionality through a .NET interface. This section documents all the available API classes and their methods, events, and usage patterns.

🎯 API Classes

Core Application Management

  • Electron.App - Control your application's event lifecycle, manage app metadata, and handle system-level operations
  • Electron.WindowManager - Create and manage browser windows, control window behavior and appearance
  • Electron.Menu - Create application menus, context menus, and menu items with full keyboard shortcut support

User Interface & Interaction

  • Electron.Dialog - Display native system dialogs for opening/saving files, showing messages and alerts
  • Electron.Notification - Show native desktop notifications with custom content and actions
  • Electron.Tray - Create system tray icons with context menus and tooltip support
  • Electron.Dock - macOS dock integration for bounce effects and badge counts

System Integration

  • Electron.Shell - Desktop integration for opening files, URLs, and accessing system paths
  • Electron.Clipboard - Read from and write to the system clipboard with multiple data formats
  • Electron.Screen - Access display and screen information for responsive layouts
  • Electron.NativeTheme - Detect and respond to system theme changes (light/dark mode)

Communication & Automation

System Monitoring

🚀 Common Usage Patterns

Application Lifecycle

// Handle app startup
Electron.App.Ready += () =>
{
    Console.WriteLine("App is ready!");
};

// Handle window management
Electron.App.WindowAllClosed += () =>
{
    if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
    {
        Electron.App.Quit();
    }
};

Window Management

// Create main window
var mainWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
{
    Width = 1200,
    Height = 800,
    Show = false,
    WebPreferences = new WebPreferences
    {
        NodeIntegration = false,
        ContextIsolation = true
    }
});

// Load content
await mainWindow.WebContents.LoadURLAsync("https://example.com");

// Show when ready
mainWindow.OnReadyToShow += () => mainWindow.Show();

Dialog Operations

// Show open file dialog
var mainWindow = Electron.WindowManager.BrowserWindows.First();
var result = await Electron.Dialog.ShowOpenDialogAsync(mainWindow, new OpenDialogOptions
{
    Properties = new[] { OpenDialogProperty.OpenFile },
    Filters = new[]
    {
        new FileFilter { Name = "Text Files", Extensions = new[] { "txt" } }
    }
});

if (result.Length > 0)
{
    Console.WriteLine($"Selected file: {result[0]}");
}

System Integration

// Open file with default application
Electron.Shell.OpenPathAsync(filePath);

// Show item in file manager
Electron.Shell.ShowItemInFolderAsync(filePath);

// Open external URL
Electron.Shell.OpenExternalAsync("https://electron.net");

🎨 Best Practices

Async Operations

All Electron APIs are asynchronous and return Task or Task<T>:

// ✅ Correct - await async operations
var result = await Electron.Dialog.ShowOpenDialogAsync(window, options);

// ❌ Incorrect - blocking call
var result = Electron.Dialog.ShowOpenDialogAsync(window, options).Result;

Event Handling

Use proper event subscription patterns:

// ✅ Correct - proper event subscription
Electron.App.WindowAllClosed += () =>
{
    Electron.App.Quit();
};

// ❌ Incorrect - missing null check
Electron.App.WindowAllClosed(); // This would throw

Error Handling

Always handle potential errors:

try
{
    var result = await Electron.Dialog.ShowSaveDialogAsync(window, options);
    if (!string.IsNullOrEmpty(result))
    {
        // Process the selected file path
    }
}
catch (Exception ex)
{
    // Handle dialog errors
    Console.WriteLine($"Dialog error: {ex.Message}");
}

🔗 API Relationships

Window and Dialog Integration

  • Use BrowserWindow instances as parent windows for dialogs
  • Dialogs automatically become modal when parent window is provided
  • Window events coordinate with application lifecycle events

IPC Communication

  • IpcMain handles communication from renderer processes
  • Use with Electron.WindowManager for window-specific messaging
  • Coordinate with Electron.App events for application-wide communication

System Integration

  • Shell operations work with file paths from Dialog operations
  • Screen information helps create properly sized windows
  • Notification and Tray provide complementary user interaction methods

🚀 Next Steps

📚 Additional Resources

Clone this wiki locally