在我们的 C# clickonce 应用程序中,我们想要获取用户的屏幕尺寸(宽度、高度)。起初一切都很好
int width = Screen.FromControl(this).Bounds.Width;
int height= Screen.FromControl(this).Bounds.Height;
后来我注意到,在我们的 Windows 8.1 笔记本电脑上,返回的值比屏幕的实际尺寸小 1.25 倍。那时我注意到某些 Windows 版本在高分辨率屏幕上存在 DPI 问题。
我知道继续使用 WPF 是一个不错的选择,但我们需要一个快速而肮脏的修复。通过谷歌搜索后,我发现了几种描述的方法 here我决定采用这个解决方案。毕竟,即使是 MSDN 博客条目也建议将以下代码片段添加到 app.manifest:
<asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
不幸的是,我不明白如何以及在何处添加此代码。我的 list (由 Visual Studio 自动生成)如下所示:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
</application>
</compatibility>
</asmv1:assembly>
请注意,我已删除注释部分以使应用程序 list 在这里看起来更小。我尝试在 </asm1:assembly>
之前添加上面的代码片段在最后一行。每当我尝试这样做时,我都会在应用程序启动前遇到异常:
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll.
Additional information: Value cannot be null.
奇怪的是,stackoverflow.com 上的其他人建议取消选中 Enable clickonce security settings
在应用程序设置中。这使该应用程序再次启动,并按预期在我的笔记本电脑上运行:该应用程序已升级且文本清晰。此外,我能够通过我的代码获得正确的屏幕尺寸(以像素为单位)。
当然,如果您想部署应用程序,禁用安全设置不是一个选项。那么我该如何解决这个问题才能得到我想要的呢?我错过了什么?在这一点上我完全迷路了。
尝试重新开始:
- 打开 Visual Studio(2013 专业版)
- 名为“Test”的新 Windows 窗体应用程序
- 启用“ClickOnce 安全设置”
- 按照其他帖子的建议再次编辑 list 文件,例如 here
- 点击开始(或构建)
出现以下错误。我不知道如何获取有关该错误的更多信息,因为没有...
最佳答案
也许现在回答来不及了,但根据the same problem的回答
Project Options > Debug > Enable the Visual Studio hosting process(disable it run app later enable it again)
关于c# - 使用 WinForms 创建 dpi 感知 C# clickonce 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933868/