Visual Studio 2010 RC關于WPF開發多個不同
本文是《辨析Visual Studio 2010 RC中WPF開發的改進》的續篇,將繼續講述Visual Studio 2010 RC在WPF方面的改進。
本不該在這個元宵佳節里寫隨筆。在這花好月圓時刻應該把酒當歌,莫使金樽空對月。特別是即汶川,海地之后的智利8.8級地震,更加讓我嗅到了2012的味道。
但是千不該,萬不該,還是讓我找到了上一篇的答案,于是沒有辦法寫下來與諸位分享。哥寫的真不是寂寞,是答案——有圖有真相的答案。
首先如果諸位不熟悉Helloj2ee的問題的,可以參見上一篇:Visual Studio 2010 RC關于WPF開發的X個不同之一——居然多了一個程序集引用
Helloj2ee總結出來的問題是 在VS2008里面 一個WPF程序至少需要四個程序集 system.dll,presentationframework.dll,windowsbase.dll和presentationcore.dll.
而Visual Studio 2010 RC里或者承蒙怕怕的韋恩卑鄙兄指點,說的準確些是WPF4。那么他需要的是五個程序集,除去剛才四個,還有就是System.xaml.dll。
現在Helloj2ee在這里告訴他的答案。如果在Visual Studio 2010 RC里編譯,會報一個什么錯誤呢?
error CS0012: The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
意思是已經定義了一個IQueryAmbient接口,而這個接口在System.xaml程序集當中。
如果您要去找IQueryAmbient接口,我打保票,您是尋訪整個工程都找不到的。如果您在過去的MSDN,我是指3.5SP1的MSDN去搜索IQueryAmbient接口,那也是找不到的。
好在.Net4.0里可以找到的,這里是該接口的官方文檔:http://msdn.microsoft.com/en-us/library/system.windows.markup.iqueryambient(VS.100).aspx
關鍵的修改之處在于FrameworkElement 實現了該接口。該接口是System.xaml.dll程序集當中的,而Window又派生自FrameworkElement,因此需要該程序集的引用。
這個接口非常簡單只有一個稱之為IsAmbientPropertyAvailable的方法,傳遞進去的參數是string,返回的是bool型。如下所示
- // Summary:
- // Queries for whether a specified property should be treated as ambient in
- // the current scope.
- public interface IQueryAmbient
- {
- // Summary:
- // Queries for whether a specified named property can be considered ambient
- // in the current scope.
- //
- // Parameters:
- // propertyName:
- // The name of the property to check for ambience state.
- //
- // Returns:
- // true if the requested property can be considered ambient; otherwise, false.
- bool IsAmbientPropertyAvailable(string propertyName);
- }
這個接口的作用,我簡單看了一下還是為了提高效率的。具體也可以請各位高手指點,我在這里拋磚先,你們盡管砸玉。
如果繼續Reflector的話,不難發現原有的System.windows.markup命名空間當中的類和接口有相當一部分從過去的Windowsbase.dll移植到了System.xaml.dll當中。不妨看下面幾張圖。***張圖是3.0版本下面的WindowsBase.dll中的System.windows.markup當中的類和接口,我只能說挺長,挺多。
下一張就是4.0版本下面的windowsbase.dll當中的System.windows.markup當中的類和接口。
可以看到相比上一張圖,類和接口少了不少。不是憑空消失了相當一部分類和接口移到了System.xaml.dll程序集當中了。比如IComponetConnector接口等。下圖就是System.xaml程序集當中的關于System.windows.markup當中的類和接口。
在這花好月圓夜,Helloj2ee寫下了事實的真相。當然比較凌亂,概括一下就是三點:
(1)WPF4當中 FrameworkElement有所改動,它實現了IQueryAmbient接口,該接口正是傳說中的System.xaml程序集中的接口,因此需要引用該程序集,繞不開,躲不過。
(2)實現該接口的目的,由Helloj2ee初步判定是為了提高效率;
(3)原來在3.5SP1之前,WindowsBase.dll承擔了部分關于XAML的類和接口,在System.windows.markup明名空間當中,好了4.0來了,他把這當中的部分類和接口放在System.xaml這個程序集里。當然具體還請各位自己查證。
原文標題:“VS2010RC關于WPF開發的X個不同之一——居然多了一個程序集引用”姊妹篇——有圖有真相 鏈接:http://www.cnblogs.com/helloj2ee/archive/2010/02/28/1675288.html 【編輯推薦】