使用 IntraWeb (45) - 活用 IntraWeb

來源:互聯網
上載者:User

標籤:style   color   io   os   使用   java   ar   for   檔案   


asp.net 剛開始時, 也是拖拉控制項, 但後來有了 MVC、xNext.

換個思路使用 IntraWeb 吧:
介面全部用 html+js+css 實現(除了動態產生的), 然後用 js 通過 Ajax 調用 Delphi 的方法.
測試程式要使用的模板 IWForm1.html:
<!doctype html><html><head><meta charset="utf-8" /><link rel="stylesheet" href="IWForm1.css" type="text/css"/></head><body>添加 刪除{%IWLabel1%}</body></html>

1、在程式所在目錄建立 Templates 檔案夾, 把 IWForm1.html 放其中.
2、在程式所在目錄建立 wwwroot 檔案夾, 把模板中用到的 IWForm1.js 和 IWForm1.css 放其中.
3、其中的 AjaxFun1() 方法是在 IWForm1.js 中實現的; 這裡兩次調用參數分別是 1、2
4、{%IWLabel1%} 中的 IWLabel1 是在 Delphi 中添加的控制項, 它用來返回資料結果.
IWForm1.js:
function AjaxFun1(n) {executeAjaxEvent("&x="+n, null, "IWCallBack1", false, null, false);}

1、executeAjaxEvent 或 processAjaxEven 方法是 IntraWeb 內建的 IWLib.js 提供的; 它將調用 Delphi 中的 IWCallBack1 方法.
2、Delphi 收到的參數將是一個 TStringList, 上面的 n 是將要傳遞的參數, x 是隨意命名的參數標示.

IWForm1.css:
div { padding: 16px; }#div2 { background-color: green; color: #fff; }#IWLABEL1 { font-size: large; }

這是隨意定義的; IWLABEL1 對應的是在 Delphi 添加的 IWLabel1.
//在建立 IW 主表單上放置 IWTemplateProcessorHTML1、IWLabel1 兩個控制項.unit Unit1;interfaceuses  Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, IWVCLComponent, IWBaseLayoutComponent, IWBaseContainerLayout, IWContainerLayout,  IWTemplateProcessorHTML, IWCompLabel, Vcl.Controls, IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl, IWCompButton, IWCompEdit;type  TIWForm1 = class(TIWAppForm)    IWLabel1: TIWLabel;    IWTemplateProcessorHTML1: TIWTemplateProcessorHTML;    procedure IWAppFormCreate(Sender: TObject);  public    procedure DoCallBack1(EventParams: TStringList); //這是 IWForm1.js 將要調用的方法; 下面還需要通過 WebApplication.RegisterCallBack 註冊一下  end;implementation{$R *.dfm}uses IW.Common.AppInfo; //擷取路徑需要var gPath: string;procedure TIWForm1.IWAppFormCreate(Sender: TObject);begin  LayoutMgr := IWTemplateProcessorHTML1;          //關聯模板(IWForm1.html)   IWTemplateProcessorHTML1.RenderStyles := False; //禁用 IW 的樣式設定  WebApplication.RegisterCallBack(‘IWCallBack1‘, DoCallBack1); //註冊回調  gPath := TIWAppInfo.GetAppPath + ‘Data.txt‘; //用於測試檔案的路徑  if not FileExists(gPath) then //初始化測試檔案  begin    with TStringList.Create do begin      Add(DateTimeToStr(Now));      SaveToFile(gPath, TEncoding.UTF8);      Free;    end;  end;  IWLabel1.RawText := True; //指定以 Html 的方式呈現其內容; 具有 RawText 屬性的幾個控制項中, 發現 IWLabel1 最靈活.  IWLabel1.StyleRenderOptions.RenderSize := False;     //既然前面已經指定了 IWTemplateProcessorHTML1.RenderStyles := False; 下面這些就應該不需要了, 但在 IE 下不行  IWLabel1.StyleRenderOptions.RenderPosition := False;  IWLabel1.StyleRenderOptions.RenderFont := False;  IWLabel1.StyleRenderOptions.RenderZIndex := False;  IWLabel1.StyleRenderOptions.RenderVisibility := False;  IWLabel1.StyleRenderOptions.RenderStatus := False;  IWLabel1.StyleRenderOptions.RenderAbsolute := False;  IWLabel1.StyleRenderOptions.RenderPadding := False;  IWLabel1.StyleRenderOptions.RenderBorder := False;end;procedure TIWForm1.DoCallBack1(EventParams: TStringList);var  List: TStringList;  x: Integer;begin  x := EventParams.Values[‘x‘].ToInteger; //擷取 js 傳來的參數  List := TStringList.Create;  List.LoadFromFile(gPath, TEncoding.UTF8);  case x of    1: List.Add(DateTimeToStr(Now));          //參數是 1 表示添加    2: if List.Count > 0 then List.Delete(0); //參數是 2 表示刪除  end;  IWLabel1.Text := List.Text.Replace(sLineBreak, ‘
‘); //呈現; List.SaveToFile(gPath, TEncoding.UTF8); List.Free;end;initialization TIWForm1.SetAsMainForm;end.

思路很簡單, 在實踐中可能會碰到一些小問題, 譬如:
1、如果真傳到伺服器, 那個 Data.txt 修改其屬性為可寫;
2、通過 executeAjaxEvent 傳遞中文參數時, 我在本機沒發現問題, 但傳到伺服器不行了, 最後自己寫了一個編碼(js)、解碼(Delphi)函數; 如果你需要可以告訴我.
不過都是小問題.

測試源檔案(IW_MVC_Test.rar), 傳到 "intraweb交流群 319037363" 了; 歡迎指正!

使用 IntraWeb (45) - 活用 IntraWeb

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.