字符显示不正确

问题描述 投票:0回答:1

我正在使用powershell制作表单应用程序。我在使字符“æ”,“ø”,“å”正确显示在标签中时遇到问题。有关它们的显示方式,请参见下面的屏幕抓图。似乎存在某种类型的编码问题,由于Powershell将字符作为utf-16保留在内存中,所以我猜问题出在我正在使用的System.Windows.Forms.Label上。

Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Width = 800
$Form.Height = 500
$Form.BackColor = "#dff3ee" #"#00554b"
$Form.AutoSize = $false
$Form.StartPosition = "CenterScreen"
$Info= New-Object System.Windows.Forms.Label
$Info.Width = 250
$Info.Height = 70
$Info.AutoSize = $false
$Info.Location = New-Object System.Drawing.Point(20,20)
$Info.Font = "Arial Unicode MS, 9"
$Info.Text = "Why is this æ ø å displayed wrong"
$Form.Controls.AddRange(@($Info))
$Form.ShowDialog()

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.18362.145
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.145
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Screengrab wrong formatting

我无法解决此问题。但是我用PS2EXE将其打包在.exe中,然后正确显示了字符。由于它在生产中是正确的,所以我不再为此烦恼。

winforms powershell
1个回答
0
投票

这是保存文件的工具的问题。如果使用Windows PowerShell ISE,它将使用UTF-8 With BOM保存文件(这意味着带有签名),但是当您使用VS CODE时,默认设置是使用UTF-8保存文件(这意味着没有BOM或没有签名) 。

要解决VS CODE中的问题:

  • 在VS CODE的状态栏中,在状态栏的右侧,您会看到UTF-8,单击它
  • 在顶部打开的菜单中,选择“编码保存”>
  • 从下一个菜单中,选择带有BOM的UTF-8
© www.soinside.com 2019 - 2024. All rights reserved.