2011年9月8日

WPF Application

WPF Application 會使用到的一些基本功能

Command-line argument

應用程式的參數可以透過 string[] args = System.Environment.GetCommandLineArgs(); 來取得

 

Persisting and Restoring Application State

過去 Windows 的應用程式多半使用 Registry 或執行目錄中的檔案來存取,
WPF 則建議採用 isolated storage,除了更容易使用外,
也與 Silverlight Application, XAML Browser Application 能有一致性。
isolated storage 以隱藏檔,存放在 current user's Document 的目錄中。

儲存:
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myFile", System.IO.FileMode.Create, file))
{
    using (StreamWriter writer = new StreamWriter(stream))
    {… }
}

讀取:
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myFile", System.IO.FileMode.OpenOrCreate, file))
{
    using (StreamReader reader = new StreamReader(stream))
    {… }
}

Splash Screen

Splash Screen 是指程式載入前,所顯示的形象圖案,
在 WPF 中的設定十分簡單,只要將圖檔加入專案中,
然後將 Build Action 屬性設定為 SplashScreen 即可。

 

Deployment

有 Windows Installer 與 ClickOnce 兩種方式可以選擇,
Windows Installer 是以專案的方式加入方案,與 ClickOnce 比較,有下列好處:

  1. 可以設計安裝介面與安裝流程
  2. 可以掌控要安裝的所有檔案
  3. 可以將分享的組件,放置在 Global Assembly Cache
  4. 可以註冊 COM 元件
  5. 可以提供 user 選項,決定安裝的內容
  6. 可以透過 DVD 離線安裝

ClickOnce 則是直接在執行專案的 Publish 屬性表中,設定資料,然後直接佈署,
其優點如下:

  1. 採連線方式安裝,有自動更新版本與退版的功能
  2. 可以在每次網頁關閉時即清除,或安裝在本機中
  3. 檔案存放在獨立的區域,不會影響到其他軟體與檔案
  4. Uninstall 可以完整清除所有檔案 (Full-trust application 則不一定)
  5. 與 .NET code access security 整合,程式可以在權限受限下執行

 

WPF Browser Applications

在新增專案時,除了 WPF Application 外,還有另一種 WPF Browser Application,
或稱為 XAML Browser Application (XBAP)
使用 ClickOnce 的技術,可以直接在 browser 的畫面上執行。
ClickOnce 型態的 Application 為了有較好的效能,會以 cache 方式處理,
更新軟體時,必須增加 version number 編號,否則仍會執行 cache 上舊版本。
WPF Browser Application 僅有 Internet Zone 等級的權限,因此有許多限制;
例如無法存取 local file, Registry, 或開啟新的 window,並在 Firefox 中,
無法使用 WebBrowser。

如果要將 Application 改為 full-trust 等級,可以在專案的 Security 屬性頁中修改,
或依照下列步驟更改:

  1. 在 app.manifest 的 PermissionSet 加入 Unrestricted="true"
    <PermissionSet class=… ID="Custom" SameSite="Site" Unrestricted="true">
  2. 更改專案檔案 .csproj (or .vbproj),將 <TargetZone>Internet</TargetZone>
    改為<TargetZone>Custom</TargetZone>

沒有留言:

Deploying Vue & .NET with Google OAuth on GCP Cloud Run

Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...