Adding Global Style for Avalonia Controls in XPF
Add Global Styles Dynamically in Code
Adding global styles for Avalonia controls dynamically in code-behind provides flexibility and allows you to apply styles at runtime. Use the following C# code to achieve this:
// Retrieve the current Avalonia application instance
var avaloniaApp = Avalonia.Controls.Application.Current;
// Dynamically add a global style for Button controls
avaloniaApp.Styles.Add(new StyleInclude()
{
Source = new Uri("avares://YourNamespace/Styles/CustomStyles.xaml") // Adjust the URI accordingly
});
Here, "CustomStyles.xaml" is the XAML file containing the Avalonia styles you want to apply globally.