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>

2011年9月7日

WPF Binary Resource


WPF Resource 的學習筆記。

Package

  1. Embedded in assembly,適合 toolbar icon, logo 等圖案
    使用方法,是將檔案加入專案,將 Build Action 設定為 Resource
  2. 分離的外部檔,但是登記在程式中
    使用方法,也是將檔案加入專案,將 Build Action 設定為 Content
    另外,記得將 Copy to Output Directory 設為 Copy Always 或 Copy if newer
    可以在編譯時,自動依照專案中的目錄,存放到編譯好的環境中
  3. 執行階段才能知道的 resource,例如由使用者指定載入的圖片

Localize

  1. 和語系相關的 localizable resource
  2. 和語系無關的 language-neutral resource

Accessing Binary Resource

可以透過 uniform resource identifier (URI) 來指定 resource 位置
利入 <Image Width="32" Height = "32" Source="logo.png"/>
下列是各種使用狀況:
  1. Source=" Images/logo.png"
    說明:圖案位於 Images 目錄下的 logo.png
  2. Source="c:\Users\John\Documents\logo.png" or
    Source=file://c:/Users/John/Documents/logo.png
    說明:直接指定檔案在本機的目錄位置
  3. Source="http://myWebsite/image/logo.png"
    說明:圖檔位於網站上
  4. Source=" /MyDll;component/Images/Money.png"
    說明:圖檔位於 MyDll assembly 下的 images (僅適用 embedded)
  5. Source=" pack://siteOfOrigin:,,,/Images/logo.jpg"
    說明:圖檔位於所在位置的 images 子目錄下

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...