创建Blazor组件内的类实例并使用其方法的过程是什么?

huangapple go评论64阅读模式
英文:

What is the process for creating an instance of a class within a Blazor component and utilizing its methods?

问题

关于Blazor服务,我在我的应用程序中有一个名为Logo-builder.razor的组件。

@page "/logo-builder"
@using BlazorApp_AI_1.Data
@inject LogoBuiderService logoBuilderService

<h1>Logo Builder</h1>

<label for="text-input">Text:</label>
<input type="text" id="text-input" @bind-value="@LogoText" />
<label for="color-picker">Color:</label>
<input type="color" id="color-picker" @bind-value="@LogoColor" />

<button @onclick="GenerateLogo">Generate Logo</button>

@if (LogoImage != null)
{
    <img src="@LogoImage" alt="Generated Logo" />
}

@code{
}

LogoBuilderService.cs:

以下是这个类的一些部分:

public string GenerateLogo(string text, string color)
{
    // Convert the color string to a Color object
    var colorObj = ColorTranslator.FromHtml(color);

    // Create a new LogoInput object with the user inputs
    var input = new LogoInput { Text = text, Color = colorObj };

    // Feed the LogoInput object to the prediction engine to generate a LogoOutput object
    var output = predictionEngine.Predict(input);

    // Create a new bitmap with the width and height of the generated logo
    var bitmap = new Bitmap(output.Width, output.Height);

    // Create a new Graphics object to draw on the bitmap
    var graphics = Graphics.FromImage(bitmap);

    // Set the background color of the bitmap
    graphics.Clear(output.BackgroundColor);

    // Draw the text on the bitmap using the font name, font size, font color, and positioning from the LogoOutput object
    graphics.DrawString(text, new Font(output.FontName, output.FontSize), new SolidBrush(output.FontColor), new PointF(output.X, output.Y));

    // Convert the bitmap to a PNG image and write it to a memory stream
    var stream = new MemoryStream();
    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
    stream.Seek(0, SeekOrigin.Begin);

    // Convert the memory stream to a base64-encoded string and return it
    return $"data:image/png;base64,{Convert.ToBase64String(stream.ToArray())}";
}

我已经在Program.cs文件中注册了LogoBuilder.cs文件。

builder.Services.AddSingleton<LogoBuiderService>();

builder.Services.AddScoped<LogoBuiderService>();

如何在组件中实例化LogoBuilder.cs并使用GenerateLogo方法?

我想在我的组件中使用这个服务类。
实例化应该如何进行?

英文:

Regarding Blazor service, I have a component called Logo-builder.razor in my application.

@page &quot;/logo-builder&quot;
@using BlazorApp_AI_1.Data
@inject LogoBuiderService logoBuilderService

&lt;h1&gt;Logo Builder&lt;/h1&gt;

&lt;label for=&quot;text-input&quot;&gt;Text:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;text-input&quot; @bind-value=&quot;@LogoText&quot; /&gt;
&lt;label for=&quot;color-picker&quot;&gt;Color:&lt;/label&gt;
&lt;input type=&quot;color&quot; id=&quot;color-picker&quot; @bind-value=&quot;@LogoColor&quot; /&gt;

&lt;button @onclick=&quot;GenerateLogo&quot;&gt;Generate Logo&lt;/button&gt;

@if (LogoImage != null)
{
    &lt;img src=&quot;@LogoImage&quot; alt=&quot;Generated Logo&quot; /&gt;
}

@code{
}

LogoBuilderService.cs:

Here are some parts of this class:

public string GenerateLogo(string text, string color)
{
	// Convert the color string to a Color object
	var colorObj = ColorTranslator.FromHtml(color);

	// Create a new LogoInput object with the user inputs
	var input = new LogoInput { Text = text, Color = colorObj };

	// Feed the LogoInput object to the prediction engine to generate a LogoOutput object
	var output = predictionEngine.Predict(input);

	// Create a new bitmap with the width and height of the generated logo
	var bitmap = new Bitmap(output.Width, output.Height);

	// Create a new Graphics object to draw on the bitmap
	var graphics = Graphics.FromImage(bitmap);

	// Set the background color of the bitmap
	graphics.Clear(output.BackgroundColor);

	// Draw the text on the bitmap using the font name, font size, font color, and positioning from the LogoOutput object
	graphics.DrawString(text, new Font(output.FontName, output.FontSize), new SolidBrush(output.FontColor), new PointF(output.X, output.Y));


	// Convert the bitmap to a PNG image and write it to a memory stream
	var stream = new MemoryStream();
	bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
	stream.Seek(0, SeekOrigin.Begin);

	// Convert the memory stream to a base64-encoded string and return it
	return $&quot;data:image/png;base64,{Convert.ToBase64String(stream.ToArray())}&quot;;
}

I have registered the LogoBuilder.cs file in the Program.cs file.

builder.Services.AddSingleton&lt;LogoBuiderService&gt;();

builder.Services.AddScoped&lt;LogoBuiderService&gt;();

How can I instantiate LogoBuilder.cs within a component and utilize the GenerateLogo method?

I want to use this service class in my component.
How should be the instantiation?

答案1

得分: 1

我认为你可以这样使用:

@page "/logo-builder"
@using BlazorApp_AI_1.Data
@inject LogoBuilderService logoBuilderService

<h1>Logo Builder</h1>

<label for="text-input">Text:</label>
<input type="text" id="text-input" @bind="LogoText" />
<label for="color-picker">Color:</label>
<input type="color" id="color-picker" @bind="LogoColor" />

<button @onclick="GenerateLogo">Generate Logo</button>

@if (!string.IsNullOrEmpty(LogoImage))
{
    <img src="@LogoImage" alt="Generated Logo" />
}

@code {
    private string LogoText;
    private string LogoColor;
    private string LogoImage;

    private async Task GenerateLogo()
    {
        LogoImage = await logoBuilderService.GenerateLogo(LogoText, LogoColor);
    }
}

也许如果你能让 GenerateLogo 返回一个 Task<string> 以允许异步操作会更好。

英文:

I think that you can use in this way:

@page &quot;/logo-builder&quot;
@using BlazorApp_AI_1.Data
@inject LogoBuilderService logoBuilderService

&lt;h1&gt;Logo Builder&lt;/h1&gt;

&lt;label for=&quot;text-input&quot;&gt;Text:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;text-input&quot; @bind=&quot;@LogoText&quot; /&gt;
&lt;label for=&quot;color-picker&quot;&gt;Color:&lt;/label&gt;
&lt;input type=&quot;color&quot; id=&quot;color-picker&quot; @bind=&quot;@LogoColor&quot; /&gt;

&lt;button @onclick=&quot;GenerateLogo&quot;&gt;Generate Logo&lt;/button&gt;

@if (!string.IsNullOrEmpty(LogoImage))
{
    &lt;img src=&quot;@LogoImage&quot; alt=&quot;Generated Logo&quot; /&gt;
}

@code {
    private string LogoText;
    private string LogoColor;
    private string LogoImage;

    private async Task GenerateLogo()
    {
        LogoImage = logoBuilderService.GenerateLogo(LogoText, LogoColor);
    }
}

Maybe it is better if you can make GenerateLogo to return a Task&lt;string&gt; to allow for asynchronous operations.

huangapple
  • 本文由 发表于 2023年6月16日 15:20:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定