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

标签 c# wpf windows-10

我正在开发一个 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/

相关文章:

c# - 使用 Async/Await 时的 WPF ProgressBar 使用情况

powershell - Win10 : How to activate developer mode using powershell or cmd. exe?

c# - 当应用程序在 Windows 10 中以分配的访问模式运行时无法显示 MessageBox

c# - WPF如何获取当前屏幕的大小?

c# - 在代码隐藏中修改列表框的数据模板属性

.net - Visual Studio 不加载项目引用

c# - 使用 LINQ to SQL 查询中的数据填充 Excel

c# - Response.Redirect 属于哪个命名空间?

c# - 如何在 Web 应用程序中更改文本框输入语言

c# - 通过控制台应用程序连接到 SQL Server 2014 不起作用