Blazor Maui无法显示包含空格或特殊字符的图像。

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

Blazor Maui not displaying images with spaces or special characters

问题

我可以显示文件名中没有空格或特殊字符的图像,但如何显示具有这些字符的图像呢?我尝试了以下方法,但在使用最新版本和 .Net 7 时都不起作用:

Razor:

<img class="actor-image" src="@EncodedActorImagePath" />

以下代码中的任何一种都不起作用:

// ActorImagePath = "images/actors/John Test.png"; // 是的,该文件存在。

private string EncodedActorImagePath => Uri.EscapeDataString(ActorImagePath);

private string EncodedActorImagePath => ActorImagePath.Replace(" ", "%20");

private string EncodedActorImagePath => WebUtility.HtmlEncode(ActorImagePath);

private string EncodedActorImagePath => WebUtility.UrlEncode(ActorImagePath);
英文:

I can display images that have no spaces nor special characters in their filenames but how am I supposed to display images that do have those? I tried the following and none worked for me using the latest versions and .Net 7:

Razor:

&lt;img class=&quot;actor-image&quot; src=&quot;@EncodedActorImagePath&quot; /&gt;

None of the following works in the code behind:

// ActorImagePath = &quot;images/actors/John Test.png&quot;; // Yes, this file exists.

private string EncodedActorImagePath =&gt; Uri.EscapeDataString(ActorImagePath);

private string EncodedActorImagePath =&gt; ActorImagePath.Replace(&quot; &quot;, &quot;%20&quot;);

private string EncodedActorImagePath =&gt; WebUtility.HtmlEncode(ActorImagePath);

private string EncodedActorImagePath =&gt; WebUtility.UrlEncode(ActorImagePath);

答案1

得分: 1

关于在 MAUI Blazor 中静态文件(CSS、JS、图像等)中的空格不起作用的类似问题,请参考 GitHub 上的问题。根据其中的评论,这个问题完全特定于 Blazor Hybrid。

我已经创建了一个新的 Blazor WebAssembly App 进行测试,即使图像名称中有空格,图像仍然可以在浏览器中显示。但是在 MAUI Blazor 中,Razor 页面显示在 WebView2 中。

此外,这个问题已经添加到 .Net 8 的计划中。您可能需要更改图像文件的名称,以使它们可以在 MAUI Blazor 中显示。

英文:

There is a similar issue about spaces in static files (CSS, JS, images, etc.) not working in MAUI Blazor on the github. And according to the comment in it, this problem is entirely specific to Blazor Hybrid.

I have create a new Blazor WebAssembly App to test, the image can be displayed in the browser even though there is a space in the image name. But the maui blazor display the razor page in the webview2.

In addition, this problem has been added into the .Net 8 planning. And you may need to change the image files' name to make them can be displayed in the maui blazor.

huangapple
  • 本文由 发表于 2023年1月9日 04:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050898.html
匿名

发表评论

匿名网友

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

确定