|
public class ThemeGenerator
{
private IHostingEnvironment env;
private NavigationManager uriHelper;
private string InputScssFile { get; set; }
private string OutputCssFile { get; set; }
public ThemeGenerator(IHostingEnvironment hostingEnvironment, NavigationManager navigationManager)
{
env = hostingEnvironment;
uriHelper = navigationManager;
InputScssFile = env.WebRootPath + @"\scss\custom-material.scss";
OutputCssFile = env.WebRootPath + @"\css\custom-theme.css";
}
public string GetValue(string prop)
{
string value = null;
string themeContent = File.ReadAllText(InputScssFile);
if (matchedValue.Groups[1] != null)
{
value = matchedValue.Groups[1].Value.Trim();
}
return value;
}
public void RefreshTheme(string prop, string value)
{
string themeContent = File.ReadAllText(InputScssFile);
CompilationOptions compilationOptions = new LibSassHost.CompilationOptions()
{
IncludePaths = new[] { "node_modules/@syncfusion/" }
};
CompilationResult result = SassCompiler.Compile(output, compilationOptions);
if (result.CompiledContent != null)
{
//Write compiled content to output css file.
File.WriteAllText(OutputCssFile, result.CompiledContent);
//Write updated content to input scss file.
File.WriteAllText(InputScssFile, output);
}
uriHelper.NavigateTo(uriHelper.Uri, forceLoad: true);
}
} |
|
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddSyncfusionBlazor();
services.AddScoped<DynamicTheming.Shared.ThemeGenerator>();
} |
|
@page "/"
@inject DynamicTheming.Shared.ThemeGenerator themeHelper
//...
<label>Change material accent color: </label>
<SfColorPicker Value="@accentColor"
ValueChange='((ColorPickerEventArgs args) => themeHelper.RefreshTheme("$accent", args.CurrentValue.Hex))'></SfColorPicker>
//... |