Hi,@Ajay Gera. Welcome to Microsoft Q&A.
WebView2 is equivalent to a built-in browser in WPF. It accesses other websites through URL. It is highly unlikely that you could change the theme of other websites through local programs. To access local websites through URL and change the theme of local websites, you could refer to the following design scheme.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeWebView();
}
private async void InitializeWebView()
{
// Initialize the WebView2 control
await WebView2.EnsureCoreWebView2Async();
// Assuming the theme color obtained from Visual Studio is gray, set the color of the corresponding control under this theme.
var buttonBackground = Color.FromRgb(128, 128, 128);
var buttonForeground = Color.FromRgb(255,255,255);
// Create HTML and CSS and dynamically apply theme colors
string htmlContent = $@"
<html>
<head>
<style>
body {{
background-color: {ToCssColor(buttonBackground)};
color: {ToCssColor(buttonForeground)};
}}
button {{
background-color: {ToCssColor(buttonBackground)};
color: {ToCssColor(buttonForeground)};
}}
</style>
</head>
<body>
<h1>WebView2 Theme Example</h1>
<button>Button</button>
</body>
</html>";
// Loading HTML content into WebView2
WebView2.NavigateToString(htmlContent);
}
private string ToCssColor(Color color)
{
return $"#{color.R:X2}{color.G:X2}{color.B:X2}";
}
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.