我尝试使用标准delphi序列化程序序列化/反序列化标准delphi容器.
procedure TForm7.TestButtonClick(Sender: TObject);
var
dict: TDictionary<Integer, Integer>;
jsonValue: TJSONValue;
begin
//serialization
dict := TDictionary<Integer, Integer>.Create;
dict.Add(1, 1);
jsonValue := TJsonConverter.ObjectToJSON(dict);
dict.Free;
//deserialization
dict := TJsonConverter.JSONToObject(jsonValue) as TDictionary<Integer, Integer>;
try
Assert(dict.ContainsKey(1), 'deserialization error - key not found');
except
Assert(false, 'deserialization error - dict object broken');
end;
end;
Run Code Online (Sandbox Code Playgroud)
有一种方法我将对象转换为JSON,反之亦然;
class function TJsonConverter.JSONToObject(AJSONValue: TJSONValue): TObject;
var
lUnMarshal: TJSONUnMarshal;
begin
lUnMarshal := TJSONUnMarshal.Create();
try
Result := lUnMarshal.Unmarshal(AJSONValue);
finally
lUnMarshal.Free;
end;
end;
class function TJsonConverter.ObjectToJSON(AData: TObject): TJSONValue;
var
lMarshal: TJSONMarshal;
begin
lMarshal := TJSONMarshal.Create(); …Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
TServer = class
private
fOnMsgFromServer: TProc<String, String, String>;
public
procedure RegisterOnMsgFromServer(aCallBack: TProc<String, String, String>);
procedure Execute;
end;
procedure TServer.RegisterOnMsgFromServer(aCallBack: TProc<String, String, String>);
begin
fOnMsgFromServer := aCallBack;
end;
procedure TServer.Execute;
begin
fOnMsgFromServer('userName', 'password', 'message');
end;
Run Code Online (Sandbox Code Playgroud)
问题在于程序执行当我想把参数放到fOnMsgFromServer时."helper"显示我(Arg1:string; Arg2:string; Arg3:string),我只是不知道哪个参数是哪个.
有没有解决方案来命名这个论点?
我想制作我自己的包含其他组件的简单组件.看起来像:
TTag = class(TLayout)
private
_line: TLine;
_label: TLabel;
_background: TRoundRect;
_button: TLabel;
public
constructor Create(AOwner: TComponent); override;
end;
Run Code Online (Sandbox Code Playgroud)
当我将这个组件放在表单上时,一切正常,我的表单结构如下所示:

但在Delphi IDE重新打开后,它看起来像:
如何添加子组件以避免这种奇怪的行为?
我刚刚学习Spring4D,我有一个问题.如果类只实现一个接口,则全部清除:
IWeapon = interface
['{E679EDA6-5D43-44AD-8F96-3B5BD43A147B}']
procedure Attack;
end;
TSword = class(TInterfacedObject, IWeapon)
public
procedure Attack;
end;
GlobalContainer.RegisterType<TSword>.Implements<IWeapon>('sword');
sword := ServiceLocator.GetService<IWeapon>('sword');
Run Code Online (Sandbox Code Playgroud)
我现在很高兴,我有剑,我不需要释放它.
但是如果类实现了两个或更多接口:
IWeapon = interface
['{E679EDA6-5D43-44AD-8F96-3B5BD43A147B}']
procedure Attack;
end;
IShield = interface
['{B2B2F443-85FE-489C-BAF4-538BB5B377B3}']
function Block: Integer;
end;
TSpikedShield = class(TInterfacedObject, IWeapon, IShield)
public
function Block: Integer;
procedure Attack;
end;
GlobalContainer.RegisterType<TSpikedShield>.Implements<IWeapon>.Implements<IShield>;
Run Code Online (Sandbox Code Playgroud)
我可以向ServiceLocator询问TSpikedShield的实例,但我需要选择一个IWeapon或IShield.但我想以两种方式使用它(或者我不应该想要?),如:
spikedShield.Attack;
spikedShield.Block;
Run Code Online (Sandbox Code Playgroud)
所以如果我很好看,我必须直接创建TSpikedShiled的实例(我的意思是没有接口).
function MakeSpikedShield: TSpickedShield;
begin
result := TSpickedShield.Create;
end;
Run Code Online (Sandbox Code Playgroud)
有没有办法使用这个类,但有自动免费?
(如果接口可以实现多次干扰但在delphi中不允许,则不会有问题)
编辑:也许是这样的想法?
ISpikedSield = interface
function AsWeapon: IWeapon;
function AsShield: IShield;
end;
TSpikedShield = class(TInterfacedObject, ISpikedShield)
Run Code Online (Sandbox Code Playgroud) 我很少使用线程,我对这个类有疑问:
unit ExpectingThread;
interface
uses
System.Classes;
type
TExpectingThread = class(TThread)
private
_timeoutMs: Integer;
_buff: string;
_patterns: TArray<string>;
_result: Integer;
function Timeouted(startTime: Cardinal): Boolean;
function ExpectedDetected: Boolean;
protected
procedure Execute; override;
public
constructor Create(patterns: TArray<string>; buff: string; timeoutMs: Integer);
//this method is called from other NOT MAIN thread
procedure BuffUpdate(text: string);
end;
implementation
uses
Winapi.Windows,
System.RegularExpressions;
{ TExpectingThread }
constructor TExpectingThread.Create(patterns: TArray<string>; buff: string; timeoutMs: Integer);
begin
_patterns := patterns;
_timeoutMs := timeoutMs;
_buff := buff;
end;
//this method is called …Run Code Online (Sandbox Code Playgroud) 我有简单的fmx形式(Delphi 10.2东京):
在代码中我将Button2显示为第二个:
procedure TForm6.FormCreate(Sender: TObject);
begin
Button2.Visible :=false;
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
Button2.Visible := true;
TTask.Create(procedure
begin
Sleep(1000);
TThread.Synchronize(nil, procedure
begin
Button2.Visible := false;
//tries
//Button2.Repaint;
//Layout1.Repaint;
//Self.InvalidateRect(Self.Bounds);
//Application.ProcessMessages;
end);
end).Start;
end;
Run Code Online (Sandbox Code Playgroud)
但是在按钮2隐藏后,会出现人工制品.它在手动形成调整大小后消失了.
如何强制刷新?