星期二, 2月 19, 2008

[Programming Concept] Coroutine

 

最近在研究C#與Script的結合,想到之前買的書「遊戲之旅 我的程式設計感悟」裡有一章在在講腳本(Script) 大概就是如何寫C++與Lua之間的整合.... 那時候才了解一個新的名詞叫Coroutine

 

本來以為這是我想要的東西,但是survey的結果似乎不是 不過也蠻好玩的,主要翻成「協同式多工」。

概念很簡單,就是如何讓兩個要run很久的function能交錯地run...好像是兩個thread在一起run,但事實是是兩個func之間作了些手腳,他們互讓的結果。

 

有興趣,可以參考以下的link..都有sample code可以試

http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

http://blog.linux.org.tw/~jserv/archives/001848.html

http://www.wretch.cc/blog/LungZeno&article_id=19936457

星期一, 2月 18, 2008

[.net] How to ConfigureVisual Studio to Debug .net framework source code


找到這個網頁 十分詳盡介紹怎麼設定要debug .net framework source 的方法

不過還沒有時間試... 就先記吧

許多.net dll已經有 full source/symbols,可以直接一步步trace...

以後會比較少用reflector.net 一點 :-D


Shawn Burke's Blog : Configuring Visual Studio to Debug .NET Framework Source Code



Powered by ScribeFire.

星期二, 1月 22, 2008

[WPF]How can I get a ListBoxItem from a data bound ListBox?

天呀~~ 我長久以來的問題 居然在2005年就有人Blog回答這個問題

我卻渾然未知 一直在navigate visual tree來解決

Data會藉由Data Template展開始Visual Tree

這兩邊的Binding要如何互相串連呢? 這是很重要的問題

從Visual Tree串連到Data 我已經找到方法 就是直接看任何一個UIElement裡的DataContext

就會指到Data

但是如果從Data 想要得到展開Visual Tree的item怎麼辦呢?

原來就是下面這一行

GreekGod greekGod = (GreekGod)(listBox.Items[0]);

ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));

from http://www.beacosta.com/blog/?p=7

[WPF] ItemsPanel, ItemsTemplate, DataTemplate, ControlTemplate? How to find ItemsPanel from code?

開始初學WPF對於Template其實是很容易一知半解 尤其是一大堆Template的名詞

與許多功能重複的關係 造成搞不清楚狀況

今天我跟同事們又解釋了一次 覺得其實我之前也花了許多時間搞懂 不如就寫下來

順便剛又找到一個問題的workaround解法 也寫下來記錄一下 .....

WPF主要是希望用XAML與Code 的方式 把User Interface與Logics分開

所以可以想到我們要寫一個Control的話......

所有的behavior, property是寫在Code裡

ControlTemplate (also can be defined in Style) 就是幫你把整個Custom Control的程式展開成Visual Tree,就是一大堆小元件,主要是Control 的Presentation....

ItemsTemplate 對於ItemsControl而言 就是將他的Items如何展開成Visual Tree....所以可想而知...ItemsTemplate是一種DataTemplates

DataTemplate 就是描述如何將Data (.net class)展開成Visual Tree (所以也就是我們assign source可以輕鬆地給.net data class,data template會為我們展開)

ItemsPanel就是對於一個ItemsControl而言 就是要去描述要如何layout所有的items,例如可以用StackPanel or WrapPanel.....


哈哈 這樣有比較清楚了嗎?..... 還有一個叫ContentTemplate

ItemsTemplate 之於ItemsControl 就等同於 ContentTemplate 之於 ContentControl
也就是描述如何將Content展開....


ok~~ 接下來是另一個問題 是我個人的經驗.....

何時要用Data Template,何時要用Custom Control,何時要用User Control

彈性而言 DataTemplate >> CustomControl >> UserControl

但是大家也知道 寫UI就是最容易東改西改 而且Control常混雜的很嚴重 也有可能很複雜

有時候很特別的一個UI 並不會需要有reuse的功能 我們可以直接用UserControl解決

但是如果是十分Common的Control 我們可以用CustomControl解決

DataTemplate則是如果能用現有的Control Composite成一個的話 就不需要花時間寫Control

大概是這樣 :D


最後一個問題 當ItemsPanel展開之後 我們沒辦法用Template.FindName找個ItemsPanel裡的Panel怎麼嗎?

我從google找到一個討論串 得enumerate Visual Tree解決 請享用

from http://www.msdner.net/dev-archive/210/153-119-2109609.shtm

private T FindItemsPanel(Visual visual)

{

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)

{

Visual child = VisualTreeHelper.GetChild(visual, i) as Visual;

if (child != null)

{

if (child is T && VisualTreeHelper.GetParent(child) is ItemsPresenter)

{

object temp = child;

return (T)temp;

}

T panel = FindItemsPanel(child);

if (panel != null)

{

object temp = panel;

return (T)temp; // return the panel up the call stack

}

}

}

return default(T);

}

星期五, 1月 18, 2008

UI與Logics可[WPF]以完全分離嗎? TemplatePart可以解決不能分離的部分

一直以來 寫WPF Custom Control會往UI與Style (ControlTemplate)作分離的假設實作

但是發現這太難了~~ 如果想要加一下比較特別的功能 或是 Usability

UI跟Logics其實是有隱含的共識與假設的........ 這部分TemplatePart就可以用來解決這方面的問題

可以參考以下這二個link

http://www.cnblogs.com/zhouyinhui/archive/2007/12/01/979715.html
http://pavanpodila.spaces.live.com/blog/cns!9C9E888164859398!634.entry

怎麼寫呢? 例如 ( from 以上Link的Copy & Paste)

[TemplatePartAttribute(Name = "PART_EditableTextBox", Type = typeof(TextBox))]
[TemplatePartAttribute(Name
= "PART_Popup", Type = typeof(Popup))]
[LocalizabilityAttribute(LocalizationCategory.ComboBox)]
[StyleTypedPropertyAttribute(Property
= "ItemContainerStyle", StyleTargetType = typeof(ComboBoxItem))]
public class ComboBox : Selector


public override void OnApplyTemplate()
{
base.OnApplyTemplate();

Button mybtn
= base.GetTemplateChild("PART_BTN");

if (mybtn != null)
{
mybtn.Click
+= new RoutedEventHandler(mybtn_Click);
}

}

星期二, 1月 01, 2008

WPF Asynchronous DataBinding


一直以來 我想要寫一個Async DataBinding... 因為大量使用Data Binding 可能會影響整個UI的Performance

結果我找到這篇.... 唔... 是我誤會WPF... 其實是有Asynchronous Mode的...


Beatriz Costa » How can I control whether my Binding is synchronous or asynchronous?


另外還有一篇也很有意思 有關cross threading的操作.... 蠻值得參考的

Beatriz Costa » How can I propagate changes across threads?

[WPF] .netfx 3.5 IDataErrorInfo


.net 2.0的Databinding似乎有這個用法 但是.net 3.0沒有 所以特別在.net 3.5加入進來....

WPF 3.5 的IDataErrorInfo的用法 -- &gt;

WPF IDataErrorInfo and Databinding

Better WPF binding in .NET 3.5 « WPF Wonderland

Windows Presentation Foundation SDK : Data Validation in 3.5



Powered by ScribeFire.

星期六, 12月 29, 2007

Another 3rd Party WPF Controls : Infragistics


我又藉由一些文章 找到另一家3rd party的WPF controls

而且好像這家也十分有名 叫Infragistic

我也找到他們有做Ribbon的Solution

http://www.infragistics.com/uploadedImages/WhatsHot/xamRibbonCtpScreenshot.jpg

有興趣參考以下link吧..

http://www.infragistics.com/hot/wpf-beta.aspx#xamRibbon


Powered by ScribeFire.

Smart Routed Commands in WPF


這次從Minneapolis回來~ 我學到了許多有關Command System的一些設計與概念

覺得十分好... 也一直在反覆比較我們之前程式的架構.... 他們設計的方法與架構 真的是比較好的方法

然後我再回到WPF Command System....我又卡住了... 是二個截然不同的概念....


Design Pattern裡講的Command十分淺顯.. 但也還不夠複雜到應付中大型的Application

我看到的Command System比較偏向是Design Pattern裡寫的 將所有的operation寫在Command裡...

而每一個Command 會有自己所對應到的Execution的Context

WPF就不太一樣... 每一個Command 都是一個RoutedEvent 會藉由Bubbling的方法(由下而上) 讓Visual Tree自己決定是否執行

執行的operation也都散布在各位Visual Element裡

從比較high level的面向來看~~ 一個Visual Element也可以是所謂的Execution Context 是十分General的看法....


我在codeproject找到一篇 作者寫了一個Smart Routed Command 這樣的方法 其實就有種mix兩種方法的味道

看完文章之後 我對Command的疑惑又進了一步.... 不過還有一大段還是需要再多想多努力

Software Design 對我而言就好像練太極拳或是在修內功一樣....... 一分力一分功 永無止盡....Orz

CodeProject: Smart Routed Commands in WPF. Free source code and programming articles



Powered by ScribeFire.

WPFpedia actipro提供的一些WPF的資源


我剛玩完他們所有的WPF Control & Demo...

Impressed!!! 其實之前有嘗試要寫Ribbon這類的Control....

後來分析的結果覺得好像有點複雜 就沒繼續下去了 可是我卻發現他們寫的很好!!!!

然後我看他們的Transition Effect也Support好多種~~ 我之前寫的大概只support到五六種而已


也看到他們collect不到的WPF Resource放在WPFpedia.com裡~~ 所以也記一下 我會繼續挖寶

有不錯的再記上來囉...


WPFpedia.com (The WPF Resource Guide)

Powered by ScribeFire.

[WPF] Actipro WPF Studio


很久沒寫些WPF相關的東西了~~

最近又開始在看一下Blog~~ 終於看到一些我覺得早該有的東西了...

就是3rd Party的WPF Control.....

這次我看到的是有WPF Ribbon & Wizard....

有興趣到下面的link看看吧...:p

我一開始已經寫了一個Wizard的東西了~~ 也有Transition Effect

這個Library也有implement一些相關的概念.....




我下載了Free Evaluation來看~~ Ribbon讓我十分驚豔....




Actipro WPF Studio - The ultimate UI control suite for WPF



Powered by ScribeFire.

The New Iteration.... Designer如何跟Engineer合作


XAML在WPF扮演很關鍵的角色~~ 而XAML的引進 造成以往RD與Designer之間的合作更加緊密

這邊有一本書 (or whitepaper) 在敘述這樣的事情

工作一二年來也碰了這方面的事 也深深覺得真的還蠻不錯的...

可惜現在的WPF Performance實在是一大敗筆 .... 不過managed code與unamanged code或許一起比較performance有點不公平吧

The New Iteration: How XAML Transforms the Collaboration Between Developers and Designers in Windows Presentation Foundation (WPF)

Powered by ScribeFire.

星期三, 11月 21, 2007

Code Generation, Software Factories and Visual Studio Extensibility


我對automatically的Code Generation一直都很有興趣.....

在C# with Visual Studio .net 其實應該有能力可以做到了....

好像是叫CodeDom的東西吧..... 之前組讀書會的時候 也有同事報過一樣的主題

雖然我還沒有時間好好去試..... 不過先記錄下來 發現這一篇blog也有蠻有詳盡的references


------------------------------------------------------------------------------------------------------------------------------------



Last night I presented Code Generation, Software Factories and Visual Studio Extensibility at Direct Supply. Below is the project download with all of the source followed by many useful links related to the various topics.


[ Download the sample project: SmallSharpTools.VisualStudioPlugins.zip ]



CodeDom



Templated Code Generation



Software Factories



Visual Studio Extensibility



Video



If you have any other links that you feel are useful, please add them below in the comments.





Powered by ScribeFire.

星期三, 11月 14, 2007

Visual Studio Debugger Visualizer for WPF Visual Tree



Visual Studio 2005有一種Visualizer Plugin的功能 可以自己撰寫對於特種的Data Type的視覺化的表現

而這篇Codeproject的作者 做出了一個VS2005 Visualizer 可以Show出WPF Element的Visual Tree的元件

十分好用~~

而我也稍微改了一下code....做了另一個Logical Tree的版本..... 現在我可以輕鬆地在VS2005 IDE裡 很容易了解Visual/Logical Tree的樣子了


Woodstock for WPF - The Code Project - Windows Presentation Foundation



Powered by ScribeFire.

星期一, 11月 05, 2007

Over-coming the Interop Airspace Issue in WPF


我只能說.... 對於host的handle-based control到WPF裡

雖然不滿意 但可以接受~~

但事實上 我也從未奢想這件事是有辦法達成的


但跟上一篇同一個作者居然實作出來了... 我只能說 真的太厲害了~~~

不過目前除了有一些bugs之外 其實十分耗資源... 可能沒辦法當成是一個比較formal的解法


Over-coming the Interop Airspace Issue in WPF


看到了吧.... 下面的圖就是把一個Broswer Control貼到一個WPF 3D Model上.....

這是一個很厲害的技法....也宣告WPF基本上解除所有的限制封印了....





Powered by ScribeFire.

Build your own DirectShow Graph in WPF MediaElement



對於WPF的MediaElement 我已經苦思許久 不得其解

對於一家Playback or Video Editing的公司 MediaElement是個完全沒辦法使用的東西

而Microsoft也未提供任何更進階的控制項 讓我們來使用 甚至連我們想自行開發都不太容易

再過幾天剛好會跟Microsoft的人一起開會... 我想趁這個機會好好再問他們一下 有沒有什們suggestion or future plan


不過在上星期我已經找到一篇Blog能達到我想做的事了... 以下兩篇

WPF - Hackery! Part I

WPF - Hackery! Part II


第一篇主要是演示拉一個MediaElement 然後自己建一個Capture的Graph,於是可以看到MediaElement也可以秀出Capture的畫面了

真的很厲害 :-)..... (要run sample前記得 register那個DirectShow Filter哦.... regsvr32 xxxx.ax)

This webcam makes me look greasy!


This is a live webcam brought into WPF using my MediaBridge hack. I swear I don't look that greasy IRL.


WPF Modified Graph



Powered by ScribeFire.

星期五, 10月 12, 2007

WPF Book Control


之前我曾經有用過Flash or 一些作法 來做幾本電子書.........

之前的SilverLight也有一個demo叫Page Turn也蠻炫的


不過今天才發現這本Pure WPF的Book Control

這本才真的比較棒~~ 翻頁不是四四角角的 而是有點柔軟的感覺

裡面可以放一大堆Control, 3D Object, Video, ...etc 應有盡有

而且還有Full Source

還真的不錯 值得一看....







Powered by ScribeFire.

Routed Events In WPF


今天在解一個Bugs.... 就發現先前設計的一個Control有一些功能沒有想的很完善.......

於是又重新再思考了一下..... 發現自以為對WPF裡的Routed Event, 包括三種方式Tunnel, Bubble, 與Direct很熟悉

以為已經掌握到訣竅 但是因為之前的設計不完善 怎麼想就是想到一些漏洞

直到看到下面的兩種Add Handler的方法 我才恍然大悟.... 我未曾懂過....

也難怪WPF的Controls的Event機制並沒有照著普通的.Net的event or delegate方法 而是還有什麼AddHandler, ...etc之類的東西

就是為了要Support Routed Event


全文可以再參考一次msdn WPF Event

唔.... 沒有一直持續設計與思考 我還真的沒發現自己沒真正懂過.....

Design的路還很長.......


C# Method1-------------------------------------------------
void MakeButton()
{
Button b2 = new Button();
b2.AddHandler(Button.ClickEvent, new RoutedEventHandler(Onb2Click));
}
void Onb2Click(object sender, RoutedEventArgs e)
{
//logic to handle the Click event
}

The next example shows the C# operator syntax
(Microsoft Visual Basic .NET has slightly different operator syntax
because of its handling of indirection):

C# Method2-------------------------------------------------

void MakeButton2()
{
Button b2 = new Button();
b2.Click += new RoutedEventHandler(Onb2Click2);
}
void Onb2Click2(object sender, RoutedEventArgs e)
{
//logic to handle the Click event
}


Powered by ScribeFire.

星期一, 10月 08, 2007

GeekInterview


之前我有一個朋友很喜歡蒐集各類很Tricky的小問題 因為現在國外大型公司很喜歡問這些Tricky的問題

例如如何Swap一個Varaible不用記憶體啦~~ 或是...etc

比較偏程式的方面 或許我還有機會可以答的出來..... 但是如果是一些思考性的問題 我幾乎答對率都很低

像是三個燈泡在一間房子裡與外面有三個開關 你可以先在外面動三個開關 (開或關) 然後只能進房子一次 就要決定那一個開關是那一個燈泡

像這類的問題 我幾乎都答不出來 @@||

所以發現網上有一個site在整理這些東西~~ 所以覺得以後如果要面試一些會考這類問題的大公司 還是多多蒐集題庫可能比較保險

:-)


Link --&gt; IT Interview Questions Open Database - GeekInterview.com

Powered by ScribeFire.

Marshaling Nested Data Structures


C++ & C#之間的Interop有許多方式,或是workaround

一個是C++/CLi, 一直是把C++ Class轉成Regular C,然後用Platform Invoke的方式,另一個是把C++包成COM

不過不論如何 都有可能會遇到可以傳遞Structure的問題 上一篇就是基本的Sample

但是Structure可能複雜到Nested怎麼辦.........找到了一篇Blog (Marshaling Nested Data Structure from jaredpar's WebLog)在描述如何解決


看完之後~~ 只能說 真的很麻煩!!!!


Powered by ScribeFire.