【问题标题】:Creating dpi aware C# clickonce application with WinForms使用 WinForms 创建 dpi 感知 C# clickonce 应用程序
【发布时间】:2015-01-14 00:15:31
【问题描述】:

在我们的 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 博客条目也建议将以下 sn-p 添加到 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>

很遗憾,我不明白如何以及在何处添加此代码。我的清单(由 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>

请注意,我已删除注释部分以使应用清单在此处看起来更小。我尝试在最后一行的&lt;/asm1:assembly&gt; 之前从上面添加sn-p。每当我尝试这样做时,我都会在我的应用程序启动之前得到一个异常:

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 Pro)
  • 名为“Test”的新 Windows 窗体应用程序
  • 启用“ClickOnce 安全设置”
  • 按照here 等其他帖子中的建议再次编辑清单文件
  • 点击开始(或构建)

出现以下错误。我不知道如何获取有关错误的更多信息,因为没有...

【问题讨论】:

  • 您似乎正确地声明了清单。所以你是例外。但是您没有提供完整的代码示例,也没有提供有关异常的完整信息。请显示异常的完整堆栈跟踪,并提供a good, complete code example。
  • @PeterDuniho 没有完整的堆栈跟踪,或者至少我找不到它。但是我添加了一些信息,我什至可以通过启动一个新的示例项目来重现该问题而无需任何编辑。

标签: c# winforms clickonce dpi


【解决方案1】:

也许回答太晚了,不过根据the same problem的回答

项目选项 > 调试 > 启用 Visual Studio 托管进程(禁用它运行应用程序稍后再启用它)

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多