【发布时间】:2013-05-22 10:17:36
【问题描述】:
每隔几周,我就会遇到这种情况:在 Delphi 项目中对使用单元执行 IDE 操作时,它会破坏 .dpr 文件。
发生的情况是它重建了uses 列表,但位置错误。
我想知道要避免什么使用模式,所以我不会再遇到这个错误。
我在许多 Delphi 版本中都出现过这个错误。我知道它至少存在于 Delphi XE2(今天又出现了)、XE、2007、2006 和 7。
损坏的片段通常是这样的结构:
ususes
Forms,
..
LastUnitInUses in 'LastUnitInUses.pas';
R *.RES}
并且应该通过删除一个us并添加一个{$来纠正:
uses
Forms,
..
LastUnitInUses in 'LastUnitInUses.pas';
{R *.RES}
出错的示例文件:
program SysUtilsFormatTests;
{
Delphi DUnit Test Project
-------------------------
This project contains the DUnit test framework and the GUI/Console test runners.
Add "CONSOLE_TESTRUNNER" to the conditional defines entry in the project options
to use the console test runner. Otherwise the GUI test runner will be used by
default.
}
{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}
ususes
Forms,
TestFramework,
GUITestRunner,
TextTestRunner,
SysUtilsFormatUnit in 'SysUtilsFormatUnit.pas';
R *.RES}
begin
Application.Initialize;
if IsConsole then
with TextTestRunner.RunRegisteredTests do
Free
else
GUITestRunner.RunRegisteredTests;
end.
更正的.dpr 文件示例:
program SysUtilsFormatTests;
{
Delphi DUnit Test Project
-------------------------
This project contains the DUnit test framework and the GUI/Console test runners.
Add "CONSOLE_TESTRUNNER" to the conditional defines entry in the project options
to use the console test runner. Otherwise the GUI test runner will be used by
default.
}
{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
Forms,
TestFramework,
GUITestRunner,
TextTestRunner,
SysUtilsFormatUnit in 'SysUtilsFormatUnit.pas';
{$R *.RES}
begin
Application.Initialize;
if IsConsole then
with TextTestRunner.RunRegisteredTests do
Free
else
GUITestRunner.RunRegisteredTests;
end.
【问题讨论】:
-
我也遇到过类似的问题,因为 Delphi 源文件包含 Unix 风格和 Windows 风格的行尾混合。您能否检查您的
.dpr是否仅包含 Windows 样式的行尾?确保文件的一种简单方法是在记事本(Windows 自己的版本)中打开文件:它只识别 Windows 样式的行尾,所以如果它看起来与在 Delphi 中的不同,那就去吧。 -
另外,不要在 active 源代码编辑器选项卡中打开 dpr。您可以在另一个选项卡上打开它,但如果它在活动选项卡中打开,我也遇到过类似的问题。
-
这很好。我不确定它是否在活动标签中打开。
-
我刚刚检查了我的项目的历史选项卡,所有 .DPR 版本都没有“错误”行结尾(所有行都以 CRLF 结尾)
-
我认为.dpr文件是否显示在IDE中并不重要
标签: delphi delphi-xe2