gpt4 book ai didi

c# - Windows 版本在设计中显示出真正的值(value),但在我运行程序时显示出不同的值(value)

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:23 24 4
gpt4 key购买 nike

我正在开发一个 WPF MVVM 应用程序来显示一些 Windows 系统信息 并练习一些 MVVM

不知何故,在设计中 Environment.OSVersion.ToString() 可以正常工作,但在调试后却无法正常工作。我尝试了调试和 Release模式,但没有任何改变。

“Microsoft Windows NT 10.0.15063.0”在设计时显示:

运行软件时显示“Microsoft Windows NT 6.2.9200.0”:

我的用户控件

<UserControl.DataContext>
<viewModels:WindowsVersionViewModel />
</UserControl.DataContext>
<Grid>
<TextBox IsReadOnly="True"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="{Binding Path=Version, Mode=OneWay}" />
</Grid>

我的 View 模型

public class WindowsVersionViewModel : ObservableObject {
public string Version { get; } = Environment.OSVersion.ToString();
}

运行后如何获得正确的版本以及为什么会出现这种情况?

最佳答案

发生这种情况是因为您的应用程序未针对 Windows 8.1 或更高版本显示。

当未显示的应用程序调用 GetVersionEx() 时,如果该函数在更高版本的操作系统上运行,它将返回 Windows 8.0 的版本号。

要解决此问题,您需要为您想要支持的 Windows 版本显示您的应用程序。

为此,您需要 add something to you application's app.manifest in the "compatibility" section .

例如:

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on and is
is designed to work with. Uncomment the appropriate elements and Windows will
automatically selected the most compatible environment. -->

<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />

<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />

<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>

这在设计器中起作用的原因是因为 Visual Studio 是针对 Windows 10 显示的,并且获取版本号的调用是在 Visual Studio 的上下文中进行的。

关于c# - Windows 版本在设计中显示出真正的值(value),但在我运行程序时显示出不同的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44240825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com