Primary APIs:
TopLevel.GetTopLevel(...)TopLevel.LauncherILauncherLauncherExtensionsIStorageItemImportant members:
ILauncher.LaunchUriAsync(...)ILauncher.LaunchFileAsync(...)LauncherExtensions.LaunchFileInfoAsync(...)LauncherExtensions.LaunchDirectoryInfoAsync(...)Reference source files:
src/Avalonia.Controls/TopLevel.cssrc/Avalonia.Base/Platform/Storage/ILauncher.csAccess pattern:
TopLevel for current visual.TopLevel.Launcher.bool success/failure).Note:
NoopLauncher returns false when no platform launcher is available.using System;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
public static class LauncherWorkflows
{
public static async Task<bool> OpenWebsiteAsync(Control anchor, Uri uri)
{
TopLevel? top = TopLevel.GetTopLevel(anchor);
if (top is null)
return false;
return await top.Launcher.LaunchUriAsync(uri);
}
public static async Task<bool> OpenStorageItemAsync(Control anchor, IStorageItem item)
{
TopLevel? top = TopLevel.GetTopLevel(anchor);
if (top is null)
return false;
return await top.Launcher.LaunchFileAsync(item);
}
}
Default mode:
XAML-first complete example:
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:MyApp.ViewModels"
x:DataType="vm:LauncherViewModel">
<StackPanel Margin="12" Spacing="8">
<Button Content="Open Website" Command="{CompiledBinding OpenWebsiteCommand}" />
<Button Content="Reveal Export Folder" Command="{CompiledBinding OpenExportFolderCommand}" />
<TextBlock Text="{CompiledBinding LastLaunchStatus}" />
</StackPanel>
</UserControl>
Code-only alternative (on request):
using System;
using System.Threading.Tasks;
using Avalonia.Controls;
public static class CodeOnlyLauncherSample
{
public static async Task OpenDocsAsync(Control anchor)
{
TopLevel? top = TopLevel.GetTopLevel(anchor);
if (top is null)
return;
bool launched = await top.Launcher.LaunchUriAsync(new Uri("https://docs.avaloniaui.net"));
_ = launched;
}
}