2009年5月3日

Create a Path at Rutime

我需要利用 Silverlight 設計裁切照片的工具,
例如,讓 user 框選照片中的大頭,裁切下來,並加以應用。
因此,在 runtime 建立一組 path,
並隨著 user 的 click 新增 node,
是一項重要的流程。

以下是我利用 path 畫出一個正方形的範例,
並以 canvas 為其父物件。為了讓範例簡化,
click、clip、image 等部分的處理,並沒有加入範例中。

Path path = new Path();

PathGeometry pathGeometry = new PathGeometry();
path.Data = pathGeometry;
pathGeometry.Figures = new PathFigureCollection();

PathFigure figur = new PathFigure();
pathGeometry.Figures.Add(figur);

figur.StartPoint = new Point(0, 0);

LineSegment seg1 = new LineSegment();
seg1.Point = new Point(100, 0);
figur.Segments.Add(seg1);

LineSegment seg2 = new LineSegment();
seg2.Point = new Point(100, 100);
figur.Segments.Add(seg2);

LineSegment seg3 = new LineSegment();
seg3.Point = new Point(0, 100);
figur.Segments.Add(seg3);

path.Fill = new SolidColorBrush(Colors.Blue);
Canvas.SetTop(path, 200);
Canvas.SetLeft(path, 300);

Canvas canvas = new Canvas();
canvas.Background = new SolidColorBrush(Colors.DarkGray);
canvas.Width = 50;
canvas.Height = 50;
canvas.HorizontalAlignment = HorizontalAlignment.Left;
canvas.VerticalAlignment = VerticalAlignment.Top;
canvas.Children.Add(path);

this.LayoutRoot.Children.Add(canvas);
[refernece]
How to draw a path at runtime

2009年4月17日

Design Patterns

最近使用 silverlight 撰寫一些程式,在規劃模組時,覺得有點生疏,所以把 Head First Design Patterns 這本書拿來複習一下。 為了方便以後可以經常檢視自己的設計,乾脆把重點記錄一下。
  1. Strategy Pattern (策略模式): 把程式中需要更動的部分獨立出來,例如不同鴨子的飛行與叫聲,可以拆成兩個不同的介面。 不要用繼承方式,而要用 composition 方式來處理。
  2. Observer Pattern (觀察者模式): 提出一對多關係,以及 loose coupling 的意義。 出版者稱為Subject,訂閱者稱為 Observer。Subject 提供 register, remove, motify Observer 的 method, 而 observer 則是提供 update method 給 Subject。 這本書舉了一個氣象觀測的例子,提供目前資訊、統計以及預報看板。
  3. Decorator Pattern (觀察者模式): 每個元件都可以單獨使用,或被其他的裝飾者包起來使用。'裝飾者'可以在乎叫其'被裝飾者'之前或之後加上自己的行為。 作者舉了一個星巴茲咖啡的例子,不過我覺得這個例子,會讓不懂咖啡的人搞得頭暈。 在處理 IO Stream 時,經常會用到這樣的技術,例如把 File Stream 丟給 Text File Stream, 而增加了處理文字內容的功能,我想大概是這樣吧。 我曾經把這個 pattern 應用在通訊封包加解密的處理,但是覺得處理得不理想,因為當封包收得不夠完整時,必須將不完整封包的前半段存在 buffer 中,這會讓後續的處理變得不太直覺。
  4. Factory Pattern (工廠模式): 把建立一堆類似的物件,獨立成一個 class,然後以字串或代碼來對應,建立後傳回 interface。這樣可以把可能不斷新增的類似物件,加入這個工廠,而不會影響到其它的程式。 我經常利用這個 pattern 來設計 UI 或處理通訊封包。當我把寫好的工廠,放到 API 時,使用工廠的 AP 還可以利用 Decorator Pattern 來延伸新的工廠。
  5. Singleton Pattern (獨體模式): Singleton pattern 可以確保一個類別只有一個實體。把建構式宣告成 private,並利用用 public static GetInstance() 的第一次呼叫來建立唯一的實體。可以應用在 thread pool, catche, registry object 等。
  6. Command Pattern (命令模式): 這個模型是將「發出需求的物件」和「接受與執行這些需求的物件」分割開來。發出命令與執行命令的中間,必須要先宣告一個 ICommand 的物件,使發出命令物件只看到單純的 ICommand 介面,然後再由不同的 ICommand 來連接多樣性的執行物件。 書中使用了一個可以控制家中所有家電的遙控器為例子,簡單清楚。而我經常將 command pattern 應用在通訊封包的傳送處理。
...

2009年3月29日

App Store

自從我知道了 iPhone 的 App Store 平台服務後,才發現 Google 與 Microsoft 也有類似的服務。 Apple - App Store Google - Android Market Microsoft - Windows Mobile Marketplace BlackBerry - BlackBerry App World

2009年3月12日

SEO

有幾個正正當當的 SEO 規則:
  1. 讓 Google 與 Yahoo 知道這個網站或網頁的存在:Google 可以透過網站管理工具,Yahoo 則透過 siteexplorer。
  2. 找出與網站相關的熱門關鍵字:既然想透過 Google 或 Yahoo 的關鍵字搜尋,當然不要選擇冷門的關鍵字。列出想到的關鍵字,到 Google 打一下,便可以看到數字的大小;至於 Yahoo,則可以透過關鍵字建議工具測試看看。
  3. 提供 Search Engine 明確的訊息:根據適當的關鍵字,設定好對應的 Title, Description 以及 Keyword;注意字串不要太長,否則搜尋機器人,會認為你想騙它。
  4. 讓更多的人連結你的網頁:趕快鼓勵你的親朋好友,連結你的網頁吧;當搜尋引擎到他的網頁而發現你的網址時,也會順便幫你加分。

meta tag sample:

<head>
  <title>JQuery Dialog Practice</title>
  <meta name="description" content="Try jQuery Popup Dialog" />
  <meta name="keywords" content="jQuery,Dialog" />
  <meta name="author" content="Dyson Liu" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

2009年3月8日

Objective-C Primer

第一次探究 Objective-C,所以做了一些簡單的筆記,原本我已為跟 C 差不多,沒想到看得 "霧煞煞"。

Extension Name
.h - header files, .m - source files of C, .mm - source files of C++

與 C 不同的 keyword
==============================
NULL => nil
#include => #inport
super =>base
self => this
------------------------------------

Classes
==============================
@interface MyClass : NSObject
{
NSString * Name;
int Age;
}
- (id)initWithString: (NSString*)aName;
+ (MyClass)Create: (NSString*)aName;
@end
------------------------------------
class 的宣告必須放在 @interface 與 @end 之間,繼承的宣告方式與 C++ 相同。
method 的宣告比較特別,最前面的 "+" 代表 static method,"-" 則是一般 instance object method;method 冒號後面是參數。
------------------------------------
@implementation MyClass
- (id) initWithString : (NSString*) aName
{
if (self = [super init])
{ Name = [aName copy];
Age = 0;
return self;
}
}
@end
------------------------------------
Class 的實體,寫在 @implementation 與 @end 之間

呼叫 mathod 的方式
==============================
MyClass *obj = [[MyClass alloc] init]; // memory allocation, init is constructor
[obj initWithString: "Peter"]; // similar to obj->initWithString("Peter");
[obj release]; // release obj
------------------------------------

String
==============================
NSLog (@"Hello World!");
NSLog (@"I like Objective-C.\\n");
NSLog (@"The value is %d", nValue);
------------------------------------
Objective-C 的字串,前面固定要多加一個@,含意與 C# 不同,所以要讓字串換行,還是要再 n 之前寫兩個反斜線。

[refernece]
Objective-C Beginner's Guide: http://www.otierney.net/objective-c.html

2009年3月3日

Silverlight Download Image

我在設計網路相簿時,必須由 server 下載照片,花了不少時間找相關資料,發現下列三種方式可以使用。 1. 利用 Web Service 將 image 以 byte[] 下傳。 2. 利用 BitmapImage.UriSource 方式下載。 3. 利用 WebClient 方式下載。(ps.僅能讀取同一站台內的資料) 範例分別如下: --- Web Service --- private void LoadAllPhoto() { PhotoServiceReference.PhotoServiceClient proxy = new PhotoManager.PhotoServiceReference.PhotoServiceClient(); proxy.GetPhotoCompleted += new EventHandler<PhotoManager.PhotoServiceReference.GetPhotoCompletedEventArgs>(proxy_GetPhotoCompleted); proxy.GetPhotoAsync(); } private Image imageShow; void proxy_GetPhotoCompleted(object sender, PhotoManager.PhotoServiceReference.GetPhotoCompletedEventArgs e) { MemoryStream stream = new MemoryStream(); stream.Write(e.Result, 0, e.Result.Length); stream.Position = 0; BitmapImage image = new BitmapImage(); image.SetSource(stream); Image imageShow = new Image(); imageShow.Source = image; this.LayoutRoot.Children.Add(imageShow); } --- BitmapImage --- BitmapImage image = new BitmapImage(); image.UriSource = new Uri("...", UriKind.Absolute); image.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(image_DownloadProgress); Image image.SetSource(stream); imageShow = new Image(); imageShow.Source = image; this.LayoutRoot.Children.Add(imageShow); --- WebClient --- WebClient imageLoader = new WebClient(); imageLoader.OpenReadCompleted += new OpenReadCompletedEventHandler(imageLoader_OpenReadCompleted); imageLoader.OpenReadAsync(new Uri("...", UriKind.Absolute)); void imageLoader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { BitmapImage image = new BitmapImage(); image.SetSource(e.Result); Image imageShow = new Image(); imageShow.Source = image; this.LayoutRoot.Children.Add(imageShow); }

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