顯示具有 VS2010 標籤的文章。 顯示所有文章
顯示具有 VS2010 標籤的文章。 顯示所有文章

2013年9月4日 星期三

AnyCPU, x86, x64, ARM

今天終於稍微了解了 AnyCPU的用意了。特地記錄下來,避免記憶體 reset 的老問題。

Visual Studio.NET 2003

在這個年代之前,並沒有64位元的 CPU(應該說不普及)。此時 Visual Studio 並不提供 CPU 相關的選項。回想一下,這是多麼美好的事。

Visual Studio 2005,2008

也才再過個兩年,64 位元的 CPU 開始在伺服器上普遍了起來。到了2007年前後,連桌上電腦也愈來愈普及。為了面對問題,微軟 Visual Studio 2005 開始有了 AnyCPU 的平台目標選項。

image

那到底使用 AnyCPU 的 assembly 會如何的被地行呢?此時的決策是非常簡單,而且看來也十分有效:

當 assembly 使用的是 AnyCPU 平台目標時,且

  • 執行在 32 位元Windows作業系統時,會以 32 位元的方式執行,IL 會被編譯成 x86 的 native code。
  • 執行在 64 位元Windows作業系統時,會以 64 位元的方式執行,IL 會被編譯成 x64 的 native code。

問題

由於目前的64位元電腦 (Windows) 都是32位元相容的,因此一個 assembly 在64位元的電腦只能以 64 位元的方式來執行,恐怕不妙。我常碰到的例子是 Oracle Client。

當我們的存取 Oracle 資料庫的程式以 AnyCPU 編譯後,執行在 64 位元的電腦,會以 64 位元的方式來執行。但如果安裝的 Oracle Client 是32位元的呢?32 位元的 Oracle Client 載入 64 位元的程式,就會拋出 System.BadImageFormatException 例外。

Visual Studio 2010

上面的問題持續了3~4年,到底 32 位元與 64位元應用程式好壞也漸漸地有了較明確的選擇。

除非明確地需要大量的記憶體,或者需要長整數,否則32位元就已經足夠。64位元的效能還會有一些些下降。

因此,在 Visual Studio 2010,建立新專案時,Target Platform(平台目標) 預設就是 x86。
但是,AnyCPU 的assembly 執行方式仍然沒有變。

Visual Studio 2012

到了 Visual Studio 2012又有變化。這個時間點,正巧 ARM CPU (也是32bits)也要進來,因此 32bit === x86 時代正式終結。

Visual Studio 2012 建立新專案後的預設選項為 AnyCPU32BitPreferred。
注意一下, Prefer 32-bit 被 disable 掉,是指不能修改,但在.NET Framework 4.5 就可以設定了!

image

當 assembly 使用的是 AnyCPU 平台目標時,且

  • 執行在 32 位元Windows作業系統時,會以 32 位元的方式執行,IL 會被編譯成 x86 的 native code。
  • 執行在 64 位元Windows作業系統時,會以 64 位元的方式執行,IL 會被編譯成 x64 的 native code。
  • 執行在 ARM Windows作業系統時,會以 32 位元的方式執行,IL 會被編譯成 ARM 的 native code。

因此,Perfer 32-bit 可以說是特地為 ARM 設計的。

結論

AnyCPU 從一開始,到目前其定義仍然沒有變動過。也就是「碰到ppp的作業系統,就執行成ppp的程式」,其中 ppp 可以是 32bit, 64bit, ARM。

改變的是「建立專案時的預設選項」。

也因此,上述 Oracle Client 的問題還是很可能會發生。建議儘可能地明確地指定平台目標(Target Platform),以減少不確定性。

2012年5月23日 星期三

Visual Studio 2010 中,MSTest 執行測試時,AppDomain.CurrentDomain.BaseDirectory 的值會隨測試設定不同而改變

今天在寫單元測試時,發現了長久來覺得怪界的現象。這次一定要解開這個謎團

問題:AppDomain.CurrentDomain.BaseDirectory 的值會不同

步驟1:

建立一個測試專案,並在測試中加上

Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
結果是
image
當程式需要讀取同目錄的檔案,例如 xml 檔案時,這是合理的。但在 MSTest 中,檔案會複製到 TestResults\xxxxx\Out 目錄下,(xxxxx 是與時間相關的目錄名稱)
步驟2:
將測試設定的選項Deployment 勾選
image
image
再執行測試一次。結果與步驟1相同。

步驟3:

關掉 Visual Studio 2010後再執行 Visual Studio 2010,重新執行該測試。竟然結果不一樣了!!

image

結論

雖然想寫單元測試,但這個結果未免也太不單元了。與環境相關,算不算是整合測試呢?

2012年3月15日 星期四

Solution 無法再 Go Online 了

今天不知何時開始,我的 Solution 再也無法 Go Online 了。每次按Go Online 就會出現下面的畫面。

但 Team Explorer 仍然運作正常呢.

image

解決方法很簡單:將 Solution 所在目錄全刪除了,再從 TFS 抓一次原始碼就可以了。

還真是奇怪。

2011年7月26日 星期二

While trying to generate your tests, the following errors occurred

當我使用 Visual Studio 2010 建立一個 Unit Test 時,發生了下面的錯誤

image

While trying to generate your tests, the following errors occurred:
Value cannot be null.
Parameter name: key

找了一下,原因很簡單。我的 TestProject 已經有相同名稱的檔名了,但在不同的目錄。VS 2010 發現有相同的 class (Full name),就會發生這樣的錯誤。

解決方法呢?就是改檔名或改 namespace 即可。

2011年6月2日 星期四

TFS 的 Web 自動部署

繼上次的 TFS 建置:Coded UI 自動測試 問題後,好久好久我都沒再碰 TFS 的 Build 設定了。

這次,目的在建立 Web 的自動部署。

環境

這次的環境較為複雜。有開發機,TFS Server,Build Server,Test WebServer.

希望可以達成的需求:在開發機上要求一個 Build後,TFS Server 向 Build Server 要求啟動一個 Build。Build Server 完成 Compile 後,將 Web Application 自動部署到 Test WebServer。

過程還有些漫長,最大的原因還是「太久遠的記憶」是不可靠的。下面是我試出來的結果。

Build Definition

在 MSBuild Arguments 我設定如下

/p:DeployOnBuild=True
/p:DeployTarget=MsDeployPublish
/p:MSDeployPublishMethod=RemoteAgent
/p:CreatePackageOnPublish=True
/p:DeployIisAppPath="Default Web Site/YourApp"
/p:MsDeployServiceUrl=http://172.199.199.199/MsDeployAgentService
/p:username=administrator
/p:password=adminpassword

image

 

Build Server

Build Server 上,必須安裝了 Visual Studio 2010。我試的結果,不裝,就不會自動部署。我不知道到底我錯過了什麼環結,雖然設定了上述的 MSBuild Arguments,但就是不跑 Deploy。

安裝時,我選了 c# 與Visual Web Developer。理論上只需安裝 Visual Web Developer即可,但我不想再浪費時間試了。

image

另外,在 Visual Studio 2010 Web Application 的專案屬性上,也有一些玄機。如果有勾選「Include IIS settings as configured in IIS」或「Include application pool settings used by this Web project」的話,在 Build Server 就要安裝 IIS 並且有相同的設定。因為當 Build Server 要打包 package 時,需要讀IIS的設定。讀不到設定就會出現下面的錯誤訊息。

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets (1657): "MapUriToIisWebServer" 工作發生未預期的失敗。 System.Runtime.InteropServices.COMException (0x80005000): 未知的錯誤 (0x80005000)  

image

此時的我,顧不得完美了,先不要勾選這兩個選項了。

Test Web Server

1 在待部署的 Web Server 上,必須安裝 Web Deployment Tool。安裝時,必須注意要選「完整」安裝。

2 安裝完畢後,需要在服務中啟動 Web Development Agent Service

image

3  Build 執行後出現下面的錯誤

Web deployment task failed.((2011/6/2 下午 05:14:42) An error occurred when the request was processed on the remote computer.)  (2011/6/2 下午 05:14:42) An error occurred when the request was processed on the remote computer. The application pool that you are trying to use has the 'managedRuntimeVersion' property set to 'v2.0'. This application requires 'v4.0'.

由於我沒有選擇要打包 IIS 的設定,部署後的應用程式是下 Default Web Site 下。Default Web Site 預設是.net framework v2.0, MSDeploy 在建立 application 時會預設同上一層的 application pool。

我的解法是直接將上一層的 Default Web Site 改用 v4.0 的 application pool。

結論

上述設定,一定還有調整優化的空間。之後有空再寫 blog好了。

雖然很麻煩,但想到之後不必再手動部署測試環境時,心裡也就舒坦了許多。

參考

2011年6月1日 星期三

Enterprise Library on NuGet

NuGet 也可以直接安裝 Enterprise Library 了。

問題

舉例來說,之前在使用 Enterprise Library Exception Handling Application Block 時,需要在專案上加入下列參考

Microsoft.Practices.EnterpriseLibrary.Common
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling

沒有經驗的人,很難一次引用正確。

解決方式:NuGet

Add Library Package Reference

image

安裝指定的 package

image

完成!還比想像中多了一些 dll,這些 unity 在用的 dll,什麼時候會用到我也不知道。

image

可惜的是,引用 package 並不會直接產生 .config 的設定。原因很簡單,它不知道我們要設定些什麼。

Enterprise Library 的設定太靈活了,要設定的話,還是乖乖地使用 Enterprise Library Configuration Editor 來設定吧。

2011年5月16日 星期一

讓 VS2010 連接 Team Foundation Server 加快

根據小朱大的 [TFS] Visual Studio 2010 Team Foundation Server 首次部署心得,找到了我長久以來的困擾解決方式。

這是一定要記下來的啦!下文是 copy 小朱大的文章部份內容

另外一個問題是,在連接 Team Foundation Server 時真的是超級無敵的慢 …,且不論是哪個 Visual Studio 的版本都會這樣,後來在網路上找到一個解法,在 Visual Studio 的 devenv.exe 的設定檔 devenv.exe.config 中,找到 <system.net>,然後加入下列的設定:

<system.net>
<defaultProxy enabled="false" />
<settings>
<ipv6 enabled="true"/>
</settings>
</system.net>

再重新打開 Visual Studio IDE,此時連接 TFS 的速度就會加快很多。

2011年5月2日 星期一

Visual Studio 2010 愈來愈慢

歷史

不知為何,Visual Studio 2010 愈來愈慢了。

現在的 Extension Manager 實在好用,我所使用的 Extensions 也應有20 多個之譜。在經過 Service Pack 1 的更新後,不知哪一天,Visual Studio 2010 慢到比 Virtul Machine 中的 還慢。

今天是勞動節補假一天(感謝公司啊!),特地到公司加班。一打開 Visual Studio 2010,就頭痛了。慢!

現象

到底怎麼慢呢?有三大現象。

  1. Intellisense 不會自動顯示。例如:File.  正常來說,當我按了句號 . 後,會出現  System.IO.File 的 static methods,但我的環境卻必須再按 ctrl + j
  2. 在 solution 中,在 project 上的快顯功能表 「Properties」或直接按 alt + Enter時,超慢。有些專案甚至沒有回應,直到受不了強迫以 Task Manager kill process
  3. WPF 中,在xaml desinger 中為 control 命名時,x:Name=”  一開始打字就像慢動作一樣,一個一個字母慢慢顯示出來。

還有其他的怪問題,無法一一記錄下來。

解決

本來想找一天重灌電腦的。一想到如此的大工程還是不要隨意開工才好。上網用 Bing 搜尋了一下。下了兩個指令。

  1. devenv /ResetSettings
  2. devenv /resetaddin *

第一個指令下完後,還是不成功。第二個指令下完後,VS2010 彷彿減肥成功般飛快了起來。

應該是 addin 灌太多了吧!

記錄一下這個事件作為記念。

2011年3月21日 星期一

Visual Studio 如何共享程式碼

有人問到,在 Visual Studio 中如何在不同的專案/方案中,分享同一組程式碼/檔案。

其實很簡單,加入一個已存在的項目(Existing Item

image

),選到檔案後,選擇加入的方式為「Add as Link」。

image

這樣一來,原始位置的檔案更新後,其他 link 的內容也同步更新。

2011年1月16日 星期日

使用 IIS Express

上一篇介紹 IIS Express 後,這一次談如何使用。

安裝

Internet Information Services (IIS) 7.5 Express 下載後執行。安裝過程完全是「下一步」「是」「完成」的過程,其間沒有任何輸入選項。或者到 Web Platform Installer 來安裝 這個連結

自行啟動 IIS Express

預設的狀況下,是要自行修改 IIS Express 設定及啟動 IIS Express 的。以下就介紹如何在 Visual Studio 2010 上使用 IIS Express。

  • 首先,建立一個 Web Application,並且命名為 WebApplication1。接著,修改 WebApplication1 的 Property,在 “Use Custom Web Server” 中設定為 http://localhost:12345/

image

  • 再來,以notepad 打開 %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config,修改configuration\system.applicationHost\sites 如下
<sites>
    <site name="WebSite1" id="1" > <!-- serverAutoStart="true" -->
        <application path="/">
            <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation=":8080:localhost" />
        </bindings>
    </site>
    <site name="WebApplication1" id="2">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
            <virtualDirectory path="/" physicalPath="C:\Users\bc\Documents\visual studio 2010\Projects\WebApplication1\WebApplication1" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation=":12345:localhost" />
        </bindings>
    </site>
    ...

我主要增加了一個新的 site,name 為 WebApplication1。id=2,請不要重複即可。在 physicalPath 中,請填入上一步 Visual Studio 所建立的 web Application 的應用程式目錄。bindngInformation 則彷校範例 WebSite1的樣子,修改為 :12345:localhost,意思是使用  localhost 的12345 port

而我將 第一個 Site name=”WebSite1” 的 serverAutoStart 移除掉了。因為我不想一啟動 IISExpress 就會啟動這個WebSite1範例。

  • 最後,按Win+R鍵或在 cmd 中執行下面指令以啟動 IISExpress
"C:\Program Files\IIS Express\IISExpress.exe" site:WebApplication1

執行結果如下圖

image

在Windows 右下方也會出現圖示。在圖示上按右鍵可以查詢執行。

image

當然,我們就可以執行 http://localhost:12345/

image

 

整合Visual Studio 2010 開發環境

上述的設定過程是不是很麻煩呢?是的,可是沒辦法。我們必須等到 Visual Studio 2010 SP1之後,才能享到直接在開發環境中整合設定的好處。使用 Visual Studio 2010 SP1 Beta 時,會多出一個「use IIS Express」的選項,如下畫面。

image

在未使用 IIS Express 的 Web 專案,也會多出一個 Use IIS Express 在快顯選單上。

image

結論

我還是等到 Visual Studio 2010 SP1 好了。這樣比較簡單。但對於其他如  VS 2005, VS2008 等使用者,只好說聲 Sorry,乖乖地自行啟動 IIS Express 吧!

2011年1月15日 星期六

介紹 IIS Express

開發 ASP.NET 的應用程式是我的主要工作。當然我會選擇最適合的開發環境。客戶多屬於企業用戶,我的開發的選擇,多半是 ASP.NET Web Application + SQL Server + IIS。

在內部 Demo 開發技術時,則會偶而使用 Web Development Server 來示範。然而在許多專案上,仍然看到選用 Web Development Server 來開發企業用的程式,雖然知道不妥,也常常宣導,但在 Visual Studio 預設使用 Web Development Server 的狀況,實在無法防止誤用的情形。

以下就探討一下 IIS 7,Web Development Server,及2010/01/13 推出的 IIS Express

使用 IIS

所有的 ASP.NET 應用程式,無論是 WebSite,WebForm,還是最新的 MVC,最終需要部署到 IIS 上。我認為開發環境最好直接使用 IIS,避免開發環境與正式環境差異太大,開發人員與維運人員的認知才會趨於一致。

優點

  • 與正式環境趨於一致。尤其在權限設定差異最為常見。

缺點

  • 設定繁複,且不同版本的 IIS會有不同的設定方式。例如 IIS5, 5.1, 6, 7, 7.5。隨著IIS不同版本,設定的位置,介面,帳號都有極大的不同。
  • 在 Vista 或 Windows 7 上,由於UAC 的限制,需要以管理員權限執行 Visual Studio 才能開發。
  • Windows Home 版本無法安裝 IIS。尤其學生或 SOHO 人員常常經費不多,無法學習或開發簡易的程式。
  • 在 Windows XP 上,只能使用 IIS 5.1。此時如果想要在 XP 上開發 IIS 7 的 ASP.NET 程式就很難了。

想要讓開發人員對不同 IIS 版本都有一定程度的了解,幾乎是不可能的事了。開發人員只能儘快地開發出需求對應的功能,對於如何部署一事,大部份都不太在意。

ASP.NET Web Development Server 的缺點

為了克服 Visual Studio 使用 IIS 的困難,微軟開發了 ASP.NET Web Development Server 這個工具。

優點

  • 簡化開發環境。開發人員不必安裝 IIS。這一點,也克服了 Windows Home 版本無法安裝 IIS 的問題。

缺點

  • 一個應用程式一個 Port。無法使用虛擬目錄。也不適用 Single Sign On 的開發。
  • 不支援 HTTPS
  • 無法使用自訂的驗證方式 ,IPrincipalIIdentity
  • 只能給開發機使用
  • 與 IIS 相比,啟動時間長
  • 直接使用與開發人員所使用的帳號作為執行 security context,而開發人員所登入的帳號常常又是管理員,造成與部署 到IIS的真實狀況 (IUser, network Service, w3wp 等)差異甚大。
  • 無法模擬asp。如果您的程式有 asp 的網頁,就不適用 Web Development Server。
  • 與 IIS 7 上的 http handler, http module 執行方式不同。見 http://charlesbc.blogspot.com/2009/11/aspnet-file-does-not-exist.html 
  • etc.

由於 Web Development Sever 的設計先天上就與真實 IIS 的執行大異其趣,許多怪異的行為層出不窮。我非常不建議使用 Web Development Server 來執行軟體開發專案。

IIS Express

由於 UAC, IIS 7 等後來的技術演變,原先設計的 Web Development Server 早已不符合需求了。微軟也體認到了這一點,於是在2010 年中開始著手開發下一個繼任的版本,也就是 IIS Express 7.5。

優點

  1. 使用 IIS 7 同一個 Kernel。因此行為與 IIS 大致相同。例如可以模定 https,http modules 與 http handlers 也與 IIS 7 的執行結果一致。
  2. 使用與開發人員相同的 security context,因此不受 UAC 的限制。

IIS Express 同時包含了 IIS 與 Web Development Server 的優點。這也是我推薦的原因。

缺點

  1. 還是使用了開發人員的 security context。開發人員容易誤以為程式的執行結果都是對的,卻不知道其實是有許多前題(即安全設定)。

由於 IIS Express 才 release 沒多久,還無法知道有哪些實際的缺點。這個缺點的清單或許會愈列愈多吧。

參考

2010年11月23日 星期二

Visual Studio 2010 做壓力測試 (load test),出現錯誤訊息

使用 Visual Studio 2010 做壓力測試 (load test)好一陣子。有一天做完後,,突然發生了錯誤訊息

Error occured running test. The loadTest completed but an error occurred while collecting the final results. Could not access the load test results repository: Incorrect syntax near 's'.  The label 'http' has already been declared. Label names must be unique within a uqery batch or stored procdure

檢視報表切到 Detail時顯示了下面的訊息

No detail exists at the selected point

使用 「Open and manage results」也找不到剛剛執行的測試結果。

原本想說將 load test 所使用的資料庫清空,重新執行loadtestresultsrepository.sql 就可以解決了。這招也不成功。

看來這是 Visual Studio 的 bug 了。

接著,我想了一下,之前為什麼可以?我修改了什麼後變成這樣了。於是我找到了原因。

我的 load test 中其中一個Web Performance Test 有問題。將該 Web Performance Test 移除,或者在 Test Matrix 中設該test 的比例為0,就可以正常儲存測試結果到資料庫。

為什麼這個 Web Performance Test 會造成 load test 的問題呢?這看起來是 Visual Studio 的 bug

2010年10月7日 星期四

No Code Coverage Results

問題

在 TFS 伺服器上,老是出現「No Code Coverage Results」的訊息。我就是要知道程式碼涵蓋率啊!

1 test run(s) completed - 100% average pass rate (100% total pass rate)
No Code Coverage Results

設定

目前方案的測試設定,是使用哪一個設定呢?我都是使用 Local (local.testsettings)這一個。

image

在Solution Items中,可以找到這一個項目。雙擊該項目,打開如下圖的設定。記得將 Code Coverage 勾選。

SNAGHTML44ee3d

按「Configure」,或雙擊「Code Coverage」,必須再設定對哪些  assembly 進行code coverage 的分析。

SNAGHTML47521e

這樣的設定下就成功了。先在本機跑一下,確定可以跑出 code coverage後,再 check in 到 TFS 上吧。

SNAGHTML4aa744

原因

在vs2010中,又多了一個TraceAndTestImpact.testsettings的設定。開發時,如果要了目前程式的修改衝擊到哪些測試,就必須使用這個一測試設定。而一旦設定了這個測試設定,很容易就忘記了,就這樣 check in 到 TFS中。這就是原因所在。?

2010年9月21日 星期二

This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed

緣由

開發 Oracle 專案時,發生了下面的錯誤訊息

Attempt to load Oracle client libraries threw BadImageFormatException.  This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed. Inner exception message: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

環境

以下是我的開發環境

解決方法

    由於 ODAC 目前只有32 bits的程式,而VS2010方案(solution)指定Build Target 是 AnyCPU,在 IIS 7 上就會執行 64 bits。在 64 bits 的 Process 上載入 32 bits 的 Oracle client library 就會發生這樣的錯誤訊息。

   解決方式也很簡單,就是在 IIS 7 上指定 Application Pool 可以跑 32 bits 的程式。

  1. 找到執行該程式的 Application Pool. 我的例子是使用預設的 ASP.NET v4.0。
  2. 在右方的 Action Panel 上,點擊「Advanced Settings」
  3. 將 Enable 32-Bit Applications 設為 True

   image

結論

    此方法只適用在開發機器上,目的是為了使用 Oracle Developer Tools For Visual Studio。在正式機上,就應該安裝 64 bits 的 Oracle Client library.

2010年9月7日 星期二

Visual Studio 2010 無法連線到 Visual Source over internet

正常來說,Source Control 改使用 Visual SourceSafe (Internet)後,在打開專案的視窗中,應該可以看到 Add SourceSafe Database 的項目。

然而,我的同直卻空空如也,一片空白。為什麼呢?原來又有更新未上。見 http://support.microsoft.com/kb/943847/en-us?p=1

 

SNAGHTML1b6bb2f

2010年9月3日 星期五

WPF 應用程式,App.xaml 不使用 StartupUri 時無法套用 ResourceDictionary

前言

WPF 實在是很棒的一個 Framework,而且在開發設計時,Visual Studio IDE 的輔助,讓我們的開發更具生產力。但也因為如此,當我們不朝預設的開發方式時,就容易遇到一些「地雷」。今天我就踩到一個。

問題

問題的由來是有演變過程的。以下就一步步的演進吧。

步驟1: 標準的 Style

我按標準步驟產生了一個 WPF Application後,在 MainWindow.xaml  上放了一個 Button。

image

Xaml 如下, 非常簡單

<Window x:Class="WpfApplication2.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                Title="MainWindow" Height="350" Width="525">
        <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="40,36,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

 

 

步驟2: 套用標準的 Style 到每一個 Button

為了讓整個程式的按鍵看來有一致的外觀,我在 App.xaml 上增加了 Button Style。App.xaml 如下,也非常的 easy

<Application x:Class="WpfApplication2.App"
                         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                         StartupUri="MainWindow.xaml">
        <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Button">
                <Setter Property="Background" Value="LightBlue" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

而無論在開發階段或執行階段,Button 的樣式果然成我預期的背景顏色LightBlue 淺藍色。

image image

步驟3:移除 StartupUri

因故,我必須移除 App.xaml 中的 StartupUri,並自行建立 MainWindow 的 instance。修改後的 App.xaml如下

<Application x:Class="WpfApplication2.App"
                         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                         > <!--注意到我移除了 StartupUri-->
        <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Button">
                <Setter Property="Background" Value="LightBlue" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

而 App.xaml.cs 如下

using System.Windows;

namespace WpfApplication2
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var mainWindow = new MainWindow();
            mainWindow.Show();
        }
    }
}

程式可以執行,但問題來了:執行時期,Button 的樣式跑掉了。

image

這是怎麼一回事?開發的Designer 仍然是淺藍色背景--我要的樣式啊!

原因

原因是:開發工具太強了,幫我們做了太多的事。將原來的 StartupUri 加回去,並在 Solution Explorer 下找到 obj\Debug\ 可以找到一堆由 Visual Studio 幫我們產生的程式碼。

image

打開 App.g.i.cs ,可以找到 InitializeComponent 這個方法。可以看到它是如何幫我們自動載入 Resource 的.

image

一旦我們將 StartupUri 移除掉,這段程式就會變化。整個 InitializeComponent 方法就不見了!!

這就是原因所在。

解決

既然找到了原因,當然就知道要怎麼解了。方法有二。

第一個方法是自己補上消失的程式,並且自己將消失的 style 補上。這個方法難度較高,而且不好維護。我採用第二個方法:自訂一個 ResourceDictionary。

新增一個 ResourceDictionary 並命名為 AppResource.xaml, 並將原來在 App.xaml 的 style 移到這裡。

SNAGHTML8e2c6c

AppResource.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
        <Setter Property="Background" Value="LightBlue" />
    </Style>
</ResourceDictionary>

改用 。修改App.xaml 如下

<Application x:Class="WpfApplication2.App"
                         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                         > <!--注意到我移除了 StartupUri-->
        <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="AppResource.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

然後,執行時期的 Button 樣式就回來了。

image

結論

WPF 實在設計的很棒,而 IDE 的開發輔助也讓我們快速的完成常見的需求。只是在 IDE 快速開發的背後,我們常常忽略了原來應該是我們要做的事。一旦不符合原 IDE 的快速開發條件時,就摸不著頭緒了。

2010年8月20日 星期五

Visual Studio Scrum 1.0 安裝

安裝過程,可見

http://visualstudiomagazine.com/Articles/2010/07/27/Installing-Scrum-Process-Template.aspx?Page=1

安裝

這裡 下載並安裝程式

上載Process Template

執行 Visual Studio 2010的功能表「Team/Team Project Collection Settings/Process Template Manager」

image

點擊「Upload」鍵,選擇目錄「C:\Program Files (x86)\Microsoft\Microsoft Visual Studio Scrum 1.0\Process Template」完成上傳.

Web Portal

小組網站必須顯示統計報表,而每種 Process Template 都可能有自己的顯示方式。Scrum 就是一例。

請將C:\Program Files (x86)\Microsoft\Microsoft Visual Studio Scrum 1.0\Project Portal\Microsoft.TeamFoundation.SharePoint.Scrum.wsp 複製到 moss 網站伺服器下的一個子目錄中,例如 c:\temp\。

再來,以管理員身份執行 cmd

image

逐一執行下列指令

SET STSADM="%CommonProgramFiles%\Microsoft Shared\web server extensions\12\Bin\stsadm.exe"
%STSADM% -o addsolution -filename Microsoft.TeamFoundation.SharePoint.Scrum.wsp
%STSADM% -o deploysolution -name Microsoft.teamfoundation.sharepoint.scrum.wsp -immediate -allowGacDeployment
%STSADM% -o execadmsvcjobs
%STSADM% -o enumsolution

記得注意一下是否有錯誤的訊息哦!我在執行%STSADM% -o execadmsvcjobs花了相當長的時間(1小時多),原來是在進行掃毒,你的環境可能沒有這回事。

結果

天啊!Dashboard 竟然出現了下面的錯誤訊息。但其它地方到還是正常的。而 Dashboard 也大約在半小時後自動正常了,也不曉得是什麼原因。

image

 

20110119 註記

這次安裝了另一台 TFS,這是安裝的是中文版的 TFS。使用上述方法後 並建立一個新的 Team Project 後,發生了下面的錯誤

TF249033: 沒有可供此地區設定識別項 (LCID) 使用的網站範本。網站範本名稱是: TFS2010 Agile Dashboard。LCID 是: 1033

查了保哥的 TFS2010 安裝 Microsoft Visual Studio Scrum 1.0 流程範本 ,才知道需要安裝 WSS 3.0 的 language pack

但最後,還是失敗了。用改 WSSTasks.xml 中的 language=1028 才能成功。

2010年7月18日 星期日

對 .NET Framweork 進行除錯

有時候,我們程式寫著寫著效能不佳,就可能怪起微軟的 .NET Framework了。微軟看來也怕了這一點,趕緊公佈 .NET Framework 的 Source Code,並且還可以在 Visual Studio 2010 進行除錯。

以下就介紹如何設定。

步驟1:下載

請到 http://referencesource.microsoft.com/netframework.aspx , 下載 .NET 4 的 Source Code 及 Symbol.

image

步驟2:安裝

下載回來的是 Net_4.msi。安裝時設定指定路徑為個人檔案的路徑,如 C:\Users\yourname\Documents\RefSrc,我就用了這一個路徑。

步驟3:設定

設定的步驟比較多。打開 Visual Studio 2010,執行功能表Tools/Options,選擇 General 頁。
不要勾選 "Enable Just My Code (Managed only)"。
勾選 "Enable .NET Framework source stepping"。
勾選 "Enable source server support"。
不要勾選"Require source files to exactly match the original version"


如下圖:

image

再選到 Symbols 路徑

按新增目錄,輸入 C:\Users\yourname\Documents\RefSrc\symbols
在 Cache symbols in this directory 中輸入 C:\Users\yourname\Documents\RefSrc\symbols\SymbolCache

完成後按確定。
image

大功告成。之後您就可以使用 F11來除錯了。

2010年7月7日 星期三

Data Driven Test

寫了一個類別

public class Class1
{
    public int Add(int a, int b)
    {
      return a + b;
    }
}
作了一個單元測試 (Unit Test)
[TestMethod]
public void TestMethod1()
{
  var o = new Class1();
  int a = 1;
  int b = 2;
  int c = o.Add(a, b);
  Assert.AreEqual<int>(3, c);
}

當然,這個測試通過了。但可惜的是,只能測一個 1 + 2 = 3 的測試案例。如果要測數個案例,要怎麼做呢?

此時,需要一些步驟。

步驟1:增加測試資料

增加一個文字檔 Data.csv, 並加入 TestProject, 內容如下

Val1, Val2, Result
1,2,3
4,6,10
5,7,12
10,-10,0
儲存時,使用 Chinese Trandional (Big5) 的編碼存檔
image
image

步驟2:設定部署

執行 Test/Edit Test Settings/Local(local.testsettings)

image

選取 Test Settings/Deployment 中的 Enable deploymnet 選項。此選項在 Visual Studio 2008 中預設為選取的,但在 Visual Studio 2010 中預設為不選取。

image

接下來,要在 Test Method 中加上 Deployment 及DataSource屬性,指示進行測試時,需要部署該 Data.csv 檔案。如下

[TestMethod, DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "data.csv", "data#csv", DataAccessMethod.Sequential)]
[DeploymentItem("Data.csv")]
public void TestMethod1()

接著,在 Solution Explorer 中選取 Data.csv,按 F4 打開 Properties Windows,將 Copy to Output Directory 設成 Copy always

image

步驟3:修改測試程式

這個動作將執行測試時,輸入 Class1.Add 的參數改由測試資料來讀取

[TestMethod, DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "data.csv", "data#csv", DataAccessMethod.Sequential)]
[DeploymentItem("Data.csv")]
public void TestMethod1()
{
  var o = new Class1();
  int a = Convert.ToInt32(TestContext.DataRow["Val1"]);
  int b = Convert.ToInt32(TestContext.DataRow["Val2"]);
  int c = o.Add(a, b);
  int checkValue = Convert.ToInt32(TestContext.DataRow["Result"]);
  Assert.AreEqual<int>(checkValue, c);
}

結果

測試結果如下。注意到雖然我們只寫了一個 TestMethod,但資料有4組,使用 Debug 模式追蹤,也的確跑了4次。但為什麼會顯示5個測試呢?我也不清楚。

image

image

 

Sample code download

Share with Facebook