發表文章

目前顯示的是 2013的文章

Hadoop 的介紹

    最近開始接觸 Hadoop 及 MapReduce 的部份! 好吧~剛好看到不錯的 vedio 在做介紹,把它記錄一下, 以備不時之需. Introduction to Hadoop Destributed System Overview 來源 : http://www.wretch.cc/blog/trendnop09/21756796

WPF – InvokeCommandAction 的使用

    在使用 MVVM 的架構時, 都會使用 Command 來 Handle 所觸發的 Event, 但如果其控制項沒有提供 Command 時, 此時又該如何呢?在 WPF 中提供了 InvokeCommandAction 這個 Class, 它提供控制項在觸發其 Event 時所需的 Handle,讓 develper 人員可以任意的 hanlde 所需的 Event. 下面的例如使用 InvokeCommandAction handle ComboBox 控制項當 select item changed 所觸發 SelectionChanged 的 Event. <ComboBox Width="180" Margin="3" HorizontalAlignment="Left" VerticalAlignment="Top"> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding StepIdChangedCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </ComboBox>

C# – 統計 Percentile 函數

    最近一直聽到 P25, P50, P75, P95 這種統計數值, 那在 .NET 中並沒有相對映的函數可以使用, 故在網路上看到了有人提供的此函數的寫法, 故在此記錄一下. public double Percentile(double[] sequence, double excelPercentile) { Array.Sort(sequence); int N = sequence.Length; double n = (N - 1) * excelPercentile + 1; // Another method: double n = (N + 1) * excelPercentile; if (n == 1d) return sequence[0]; else if (n == N) return sequence[N - 1]; else { int k = (int)n; double d = n - k; return sequence[k - 1] + d * (sequence[k] - sequence[k - 1]); } }

WPF – Getting Started with MVVM

I've been working with MVVM and WPF for a couple of weeks now. I decided to log what I've learned here. Here goes a getting started tutorial with MVVM. Download the source code . The Model-View-ViewModel pattern was introduced by John Gossman to effectively utilize the functionality of WPF. Since then, MVVM has been used in a number of WPF applications with very impressive results. MVVM has three components: Model: It is your data or classes that represent entities in your application. It normally contains no WPF-specific code. View: This is the User Interface element visible to the user. Its DataContext is its ViewModel. ViewModel: It contains all the data that needs to be displayed and procedures to modify the model at will. The magic about MVVM is that the ViewModel knows nothing about the View. You see that this is very loosely coupled. The View knows the ViewModel but the ViewModel does not know the View. You can very easily replace the View without affecting t

WPF - 深入 Style

圖片
Style 用來在類型的不同實例之間共享屬性、資源和事件處理程序,您可以將 Style 看作是將一組屬性值應用到多個元素的捷徑。   這是MSDN上對Style的描述,翻譯的還算中規中矩。Style(樣式),簡單來說,就是一種對屬性值的批處理,類似于Html的CSS,可以快速的設置一系列屬性值到UI元素。   示例   一個最簡單的Style的例子: <Window> <Grid> <Grid.Resources> <Style TargetType="{x:Type Button}" x:Key="ButtonStyle"> <Setter Property="Height" Value="22"/> <Setter Property="Width" Value="60"/> </Style> </Grid.Resources> <Button Content="Button" Style="{StaticResource ButtonStyle}"/> <Button Content="Button" Style="{StaticResource ButtonStyle}" Margin="156,144,286,145" /> </Grid> </Window>   關于Resources的知識,請參見 MSDN ,這里創建了一個目標類型為Button的ButtonStyle,兩個Button使用靜態資源( StaticResource )的查找方式來找到這個Style。Style中定義了Button的高度(Height)和寬度(Width),當使用了這個Style后,兩個Button無需手動設置,即可自動設置

WPF – 使用 DrawingVisual 描繪圖形

圖片
在 WPF 中有很多種方式來描繪圖形, 其 DrawingVisual 是一個輕量級的 Class, 它需一個 container 來承接, 並不提供 Layout, Hit-Testing 及 Event-Handling. 那來實作一下如何使用 DrawingVisual 來描繪圖形. 建立一個 class, 它繼承 FrameworkElement Class, 並描繪一個 Rectangle . public class MyVisualHost : FrameworkElement { private VisualCollection childern; public MyVisualHost() { childern = new VisualCollection(this); childern.Add(CreateDrawingVisualRectangle()); } protected override int VisualChildrenCount { get { return childern.Count; } } protected override Visual GetVisualChild(int index) { return childern[index]; } private DrawingVisual CreateDrawingVisualRectangle() { DrawingVisual drawing = new DrawingVisual(); using (DrawingContext content = drawing.RenderOpen()) { content.DrawRectangle(Brushes.Red, new Pen(Brushes.

WPF - 性能優化

  WPF性能優化一 - Rendering Tier  根据硬件配置的不同,WPF采用不同的Rendering Tier做渲染。下列情況請特別注意,因為在這些情況下,即使是處于Rendering Tier 2的情況下也不會硬件加速。(不全,其余請查閱SDK)  WPF性能優化二 - 布局和設計  盡量多使用Canvas等簡單的布局元素,少使用Grid或者StackPanel等復雜的,越復雜性能開銷越大。  建立邏輯樹或者視覺樹的時候,遵循Top-Down的原則。  WPF性能優化三 - 圖像  對Image做動畫處理的時候(如調整大小等),可以使用這條語句RenderOptions.SetBitmapScalingMode(MyImage,BitmapScalingMode.LowQuality),以改善性能。 用TileBrush的時候,可以CachingHint。  WPF性能優化四 - 對象行為  訪問CLR對象和CLR屬性的效率會比訪問DependencyObject/DependencyProperty高。注意這里指的是訪問,不要和前面的綁定混淆了。但是,把屬性注冊為DependencyProperty會有很多的優點:比如繼承、數据綁定和Style。 WPF性能優化五 - 應用程序資源  在自定義控件,盡量不要在控件的ResourceDictionary定義資源,而應該放在Window或者Application級。因為放在控件中會使每個實例都保留一份資源的拷貝。 盡量使用Static Resources,但不能盲目使用。  WPF性能優化六 - 文本  文字少的時候用TextBlock或者label,長的時候用FlowDocument.  使用元素TextFlow和TextBlock時,如果不需要TextFlow的某些特性,就應該考慮使用TextBlock,因為它的效率更高。  在TextFlow中使用UIElement(比如TextBlock)所需的代价要比使用TextElement(比如Run)的代价高.在FlowDocument中盡量避免使用TextBlock,要用Run替代。 在TextBlock中顯式的使用Run命令比不使用Run命名的代碼要高。 把Label(標簽)元素的

C# - NHibernate Queries Examples (轉貼)

圖片
Today was the first day of my NHibernate course, and I think that it might be good to point out a few of the samples that we worked with. Those are pretty basic NHibernate queries, but they are probably going to be useful for beginners. Let us take my usual Blog model, and see what kind of queries (and results) we can come up with: Let us find a blog by its identifier: var blog = s.Get<Blog>(1); Which results in: We can also try: var blog = s.Load<Blog>(1); Which would result in… absolutely no SQL queries. You can look at a more deep discussion of that here . Now, let us try to search by a property: var blogs = s.CreateCriteria<Blog>() .Add(Restrictions.Eq("Title", "Ayende @ Rahien")) .List<Blog>(); Which results in: If we try to make the same with HQL, it would look: var blogs = s.CreateQuery("from Blog b where b.Title = :title") .SetParameter("title","Ayende @ Rahien") .List<Blog>(); Whic

Flex – Cairngorm 之初體驗

圖片
最近在看 Flex 的 Framework, 以 Adobe 官方建議的 Cairngorm 做為開始. 1. ModelLocator : 此部份提供了 Model 的功能, 使用Signleton design pattern 的設計把所有的資料都收集到這邊. 2. Command, Event, FrontController: 在 Front Controller 註冊 Command  與對應的 Event, Event Dispatcher 監聽 Event, 爾後操作過程中 dispatch event 觸發 Command 執行動作, 最後 Update Model 更新 View 顯示. 3. Interact with a server : ServiceLocator : 此 Class 提供了 application 將使用的所有 services, 它包含了 HTTPService, WebService, RemoteObject 呼叫. Business Delegate : 此 Class 有三個 Function     to locate the service that is needed     to call a method on the service     to route the result to the specified responder Value Object : 此 Class 一般定義 property 並不需要任何的 methods, 使用來跟 Server 做 transfer strongly-typed complex data object. 4. Responders, Commands, Delegates : Responders : Responders should deal only in strongly-typed application objects. Responders may set values on the model. Commands : Commands never deal directly with server interaction.Commands may set values on the mo

C# – 得知 Server 上 Protocol 是否已開啟

當程式要得知 Server 上的 Protocol 是否已開啟, 可以試著去連接來查看, 下面的程式碼即可得知 Server 上其 Protocol 是否已開啟.下面提供一個簡單的範例. TcpClient client = new TcpClient(); bool isPortExist = false; Random rnd = new Random(); int portId = rnd.Next(10000, 60000); try { client.Connect("localhost", portId); isPortExist = true; } catch (Exception ex) { isPortExist = false; } finally { Console.WriteLine(string.Format("Port Id : {0},protocol {1}", portId, isPortExist ? "opened." : "closed.")); } Console.Read();

WPF - 窺探 Flow Diagram Designer 的設計

最近收到一個很好玩的 Requirement,提供 Data Sources, Charts, Filters 等組件可讓 User 隨意組合這些元件為一個 Flow 並執行取得結果, 這讓我回想到三年前 WPF 出來時在網路上看到有好心人士提供了其作法和 Source Code, 想不到現在有這 Requirement 需要它了, 真的是太感謝這些好心人士了,那在此記錄一下這些相關的文章, 以防之後還有類似的需求時, 好方便瀏覽. Part 1 – Drag, resize and rotate items on a canvas Part 2 – Toolbox, drag & drop, rubberband selection Part 3 – Connecting items Part 4 – Open/Save, Cut/Copy/Past/Delete and print

Javascript – easy UI 的 datetimebox 自定日期時間格式

圖片
在做 Web 的 UI 時, 其 easyUI 這個免費的 Javascript Framework 提供了很多好用的組件,來幫助我們建立豐富及美觀的 Web 畫面, 在日期組件中提供了 datebox, datetimebox 這二種組件, 並提供相對映的 member 讓我們可以自定日期(日期時間)格式, 提供一個 yyyy-MM-dd HH:mm: ss 的時間格式 $("#startDate, #endDate").datetimebox({ formatter: function (date) { return date.getFullYear() + "-" + (date.getMonth() + 1).toString().leftPad("0", 2) + "-" + date.getDate().toString().leftPad("0", 2) + " " + date.getHours().toString().leftPad("0", 2) + ":" + date.getMinutes().toString().leftPad("0", 2) + ":" + date.getSeconds().toString().leftPad("0", 2); }, parser: function (date) { return new Date(Date.parse(date.replace(/-/g, "/"))); } }); 其顯示如下所示:

C# – M$ Chart Control 自定 ToolTip 的顯示

圖片
在使用 M$ Chart Control 來顯示 Chart 時, 一般的 Requirments 其 ToolTip 在顯示一般都為 X/Y Value,可是當有特別的資料需顯示時,那又該如何呢?在其 DataPoint Class 中提供一個SetCustomProperty Member 讓 User 可以設定其 Custom Property Value,那只需在 Series Class 中設定 ToolTipProperty 後,其 ToolTip 即可顯示 Custom Property. Series s = new Series(); s.Name = "SeriesName"; s.ChartType = SeriesChartType.Line; s.MarkerStyle = MarkerStyle.Circle; s.MarkerSize = 8; s.BorderWidth = 3; s.ToolTip = "Wafer Id: #AXISLABEL\n" + "Time: #CUSTOMPROPERTY(TIME) \n" + "Y Value: #VALY";

C# – M$ Chart Control 顯示 Stacked Bar Chart.

圖片
最近因 Requirement 的需要, 需 Drawing stacked bar chart,之前的文章已有寫過如何在 ASP.NET MVC 中使用 MS Chart Control 這個元件了, 那其 Stacked Bar Chart 需在 Series 中加入一個自訂的 Attributes ( StackedGroupName ) 來控制,故在此記錄一下其 Code 的部份,以防後續時間一久又忘了如何使用. Series s = new Series(); s.Name = "SeriesName"; s.ChartArea = "ChartArea1"; s.ChartType = SeriesChartType.StackedColumn; s["StackedGroupName"] = "Group1"; chart.Series.Add(s);

Javascript – String 增加 LeftPad 的 Function

在 C# 中其 String 當其 Length 不足時可在前方補上所需的字元, 那在 Javascript 中並沒有此 Function 可以使用, 那使用擴充的方式來增加所需的 Function. 那如下所示: String.prototype.leftPad = function (padString, length) { var str = this; while (str.length < length) str = padString + str; return str; }