使用MudBlazor在图像上方添加文本。

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

Add a text above an image using mudbalzor

问题

我需要使用MudBlazor图像组件实现这种视图:

使用MudBlazor在图像上方添加文本。

我已经正确获取了图像,但如何在图像上方添加文本,就像上面的图片一样?这是我的代码:

<MudGrid>

@foreach (var item in tabscatitm)
{
  <MudItem xs="3" >

    <MudImage Src=@item.ItemImageUrl Height="120" Width="120" @onclick="ItemClick" 
      Class="rounded-lg" Style="border: thin; border-color: darkgray; border-radius: 10px"/>

    <MudText Typo="Typo.h6">@item.ItemName</MudText>

    <MudText Typo="Typo.body2">@item.ItemShortName</MudText>

  </MudItem>
}

</MudGrid>
英文:

I need to implement this kind of view using mudblazor image components:

使用MudBlazor在图像上方添加文本。

I got the images correct, but how can I add text on top of the image like in the picture above? Here is my code:

&lt;MudGrid&gt;

u/foreach (var item in tabscatitm)
{
  &lt;MudItem xs=&quot;3&quot; &gt;

    &lt;MudImage Src=@item.ItemImageUrl Height=&quot;120&quot; Width=&quot;120&quot; u/onclick=&quot;ItemClick&quot; 
      Class=&quot;rounded-lg&quot;Style=&quot;border:thin;border-color:darkgray;border-radius:10px&quot;/&gt;

    &lt;MudText Typo=&quot;Typo.h6&quot;&gt;@item.ItemName&lt;/MudText&gt;

    &lt;MudText Typo=&quot;Typo.body2&quot;&gt;@item.ItemShortName&lt;/MudText&gt;

  &lt;/MudItem&gt;
}

&lt;/MudGrid&gt;

答案1

得分: 0

以下是翻译好的部分:

你可以尝试类似这样的方法:

### CustomImage.razor

```cs
&lt;div style=&quot;position: relative; cursor: pointer&quot; @onclick=&quot;@this.Clicked&quot;&gt;

  @* 图像 *@
  &lt;MudImage Src=&quot;@this.Src&quot; Alt=&quot;@this.Alt&quot;/&gt;

  @* 文本的透明背景 *@
  &lt;MudPaper
    Elevation=&quot;0&quot;
    Class=&quot;rounded-0&quot;
    Style=&quot;position: absolute; bottom: 0; left: 0; padding: 25px; width: 100vw; opacity: 0.6&quot;&gt;

    @* Hack: 文本用于为透明部分提供正确的高度,但也会以0.6的透明度显示文本 *@
    &lt;MudText Typo=&quot;Typo.h4&quot;&gt;@this.Name&lt;/MudText&gt;
    &lt;MudText Typo=&quot;Typo.h6&quot;&gt;@this.ShortName&lt;/MudText&gt;
  &lt;/MudPaper&gt;

  @* 文本(黑色) *@
  &lt;span style=&quot;position: absolute; bottom: 0; left: 0; padding: 25px; width: 100vw; color: black&quot;&gt;
    &lt;MudText Typo=&quot;Typo.h4&quot;&gt;@this.Name&lt;/MudText&gt;
    &lt;MudText Typo=&quot;Typo.h6&quot;&gt;@this.ShortName&lt;/MudText&gt;
  &lt;/span&gt;

&lt;/div&gt;

@code {

  [Parameter]
  public string Src { get; set; } = default!;

  [Parameter]
  public string Alt { get; set; } = string.Empty;

  [Parameter]
  public string Name { get; set; } = default!;

  [Parameter]
  public string ShortName { get; set; } = default!;

  [Parameter]
  public EventCallback OnClick { get; set; }

  private async Task Clicked()
  {
    await this.OnClick.InvokeAsync();
  }
}

如果像这样使用:

&lt;MudGrid&gt;
  @foreach (var item in this.items)
  {
    &lt;MudItem md=&quot;6&quot;&gt;
      &lt;CustomImage
        Src=&quot;@item.Src&quot;
        Name=&quot;@item.Name&quot;
        ShortName=&quot;@item.ShortName&quot;
        OnClick=&quot;@(() =&gt; this.Click(item))&quot;/&gt;
    &lt;/MudItem&gt;
  }
&lt;/MudGrid&gt;

@code {
  private readonly List&lt;Item&gt; items = new()
  {
    new Item { Name = &quot;A tasty burger&quot;, ShortName = &quot;一些更多文本或价格...&quot;, Src = &quot;images/hamburger.jpg&quot; },
    new Item { Name = &quot;Another burger&quot;, ShortName = &quot;...&quot;, Src = &quot;images/hamburger.jpg&quot; }
  };

  private void Click(Item item)
  {
    Console.WriteLine($&quot;点击了 {item.Name}&quot;);
  }
}

结果将如下图所示:

使用MudBlazor在图像上方添加文本。

请注意关于“Hack”的注释:我的CSS知识相当有限,但我相信你的某位同事可以帮助解决这个问题。这只是一个让你入门的快速演示。


希望这对你有所帮助!

<details>
<summary>英文:</summary>

You could try something like this:

### CustomImage.razor

```cs
&lt;div style=&quot;position: relative; cursor: pointer&quot; @onclick=&quot;@this.Clicked&quot;&gt;

  @* the image *@
  &lt;MudImage Src=&quot;@this.Src&quot; Alt=&quot;@this.Alt&quot;/&gt;

  @* A transparent background for the text *@
  &lt;MudPaper
    Elevation=&quot;0&quot;
    Class=&quot;rounded-0&quot;
    Style=&quot;position: absolute; bottom: 0; left: 0; padding: 25px; width: 100vw; opacity: 0.6&quot;&gt;

    @* Hack: the text is needed to give the transparent part the proper height, but will also display the text in 0.6 transparency *@
    &lt;MudText Typo=&quot;Typo.h4&quot;&gt;@this.Name&lt;/MudText&gt;
    &lt;MudText Typo=&quot;Typo.h6&quot;&gt;@this.ShortName&lt;/MudText&gt;
  &lt;/MudPaper&gt;

  @* The text (in black) *@
  &lt;span style=&quot;position: absolute; bottom: 0; left: 0; padding: 25px; width: 100vw; color: black&quot;&gt;
    &lt;MudText Typo=&quot;Typo.h4&quot;&gt;@this.Name&lt;/MudText&gt;
    &lt;MudText Typo=&quot;Typo.h6&quot;&gt;@this.ShortName&lt;/MudText&gt;
  &lt;/span&gt;

&lt;/div&gt;

@code {

  [Parameter]
  public string Src { get; set; } = default!;

  [Parameter]
  public string Alt { get; set; } = string.Empty;

  [Parameter]
  public string Name { get; set; } = default!;

  [Parameter]
  public string ShortName { get; set; } = default!;

  [Parameter]
  public EventCallback OnClick { get; set; }

  private async Task Clicked()
  {
    await this.OnClick.InvokeAsync();
  }
}

If used like this:

&lt;MudGrid&gt;
  @foreach (var item in this.items)
  {
    &lt;MudItem md=&quot;6&quot;&gt;
      &lt;CustomImage
        Src=&quot;@item.Src&quot;
        Name=&quot;@item.Name&quot;
        ShortName=&quot;@item.ShortName&quot;
        OnClick=&quot;@(() =&gt; this.Click(item))&quot;/&gt;
    &lt;/MudItem&gt;
  }
&lt;/MudGrid&gt;

@code {
  private readonly List&lt;Item&gt; items = new()
  {
    new Item { Name = &quot;A tasty burger&quot;, ShortName = &quot;Some more text or a price...&quot;, Src = &quot;images/hamburger.jpg&quot; },
    new Item { Name = &quot;Another burger&quot;, ShortName = &quot;...&quot;, Src = &quot;images/hamburger.jpg&quot; }
  };

  private void Click(Item item)
  {
    Console.WriteLine($&quot;Clicked {item.Name}&quot;);
  }
}

The result will be:

使用MudBlazor在图像上方添加文本。

Note the comment about the hack: my CSS knowledge is rather limited, but I'm sure some colleague of yours can help with that. This is just a quick demo for you to get started.

huangapple
  • 本文由 发表于 2023年2月10日 14:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407551.html
匿名

发表评论

匿名网友

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

确定