2011年4月26日

Styling Separators in WPF

最近利用 WPF 設計 UI 時,想把 Menu Separator 的 Template 改掉,以為是個很簡單的工作,卻花了不少時間。還好找到了下面的一篇文章,這跟常規的處理方式略為不同,真不知道 WPF 還有多少控制項,也有相同的問題。

[Reference]
Styling Separators in WPF

2011年4月19日

Dual Screen Application and WPF

使用 WPF 建立雙螢幕或多螢幕畫面,作法如下:
  1. 首先刪除 App.xaml 中的 StartupUri="MainWindows.xaml"的設定。
  2. 加入 System.Windows.Forms (for Screen) 與 System.Drawing (for Rectangle) 等參考。
  3. 在 App.xaml.cs 中加入下列的程式碼 (以雙螢幕為例)。

App.xaml.cs:
 
    public partial class App : System.Windows.Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            MainWindow w1 = new MainWindow();
            MainWindow w2 = new MainWindow();

            Screen s1 = Screen.AllScreens[0];
            Screen s2 = Screen.AllScreens[1];

            Rectangle r1 = s1.WorkingArea;
            Rectangle r2 = s2.WorkingArea;

            w1.Top = r1.Top;
            w1.Left = r1.Left;

            w2.Top = r2.Top;
            w2.Left = r2.Left;

            w1.Show();
            w2.Show();

            w2.Owner = w1;
        }
    }

如果希望畫面在開啟時,都以全螢幕方式顯示,在屬性表設定是無效的,
必須等到 load 之後,才能指定。
MainWindow.xaml.cs:
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Maximized;
        }
    }

參考資料:
Dual Screen Application and WPF
Maximizing a WPF Window to second monitor

2011年1月26日

A Distributable (.exe) Silverlight OOB Application

Silverlight 的 Out of Browser 可以很方便讓你由網站,直接將程式安裝到桌面上;但是,若想把 Silverlight 包裝成可安裝的套裝軟體,反而變得比較麻煩。

下面這篇文章可以告訴你如何處理:

A Distributable (.exe) Silverlight OOB Application
How to distribute a Silverlight OOB Application?

2010年11月26日

ASP.NET Dynamic Image by WPF

[ Download Source Code ]

WPF 是微軟目前最主要的繪圖系統,應用在 ASP.NET Dynamic Image,可以產生許多特效與變化,例如文字特效、相片合成與特效等。



以下範例,我分別建立了 Web Site 與 WPF User Control 兩個專案,並在 Web Site 上,加入 WPF User Control 的參考。建立 Web Site 後,記得先加入下列四組 .dll 參考

  • PresentationCore
  • PresentationFramework
  • System.Xaml
  • WindowsBase

在 Web 上,新增 ImageBuilderHandler.ashx 來處理 Dynamic Image,並在網頁上加入 Image 元件,並設定 ImageUrl 連結 ImageBuilderHandler.ashx



而 ImageBuilderHander.ashx 的程式碼如下:
    public class ImageBuilderHandler : IHttpHandler
    {
        HttpContext m_context;
        byte[] m_imageBuffer = null;

        public void ProcessRequest(HttpContext context)
        {
            m_context = context;
            CreateImage();

            if (m_imageBuffer != null)
            {
                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(m_imageBuffer);
            }
        }

        void CreateImage()
        {
            Thread worker = new Thread(new ThreadStart(this.CreateImageWorker));
            worker.SetApartmentState(ApartmentState.STA);
            worker.Name = "CreateImageWorker";
            worker.Start();
            worker.Join();
        }

        void CreateImageWorker()
        {
            UserControl control = new UserControl1() { TimeText = DateTime.Now.ToString("HH:mm:ss.f") };

            control.Arrange(new System.Windows.Rect(new System.Windows.Size(control.Width, control.Height)));
            control.UpdateLayout();

            RenderTargetBitmap bitmapRenderer = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Default);
            bitmapRenderer.Render(control);

            JpegBitmapEncoder jpeg = new JpegBitmapEncoder();
            jpeg.Frames.Add(BitmapFrame.Create(bitmapRenderer));
            MemoryStream memoryStream = new MemoryStream();
            jpeg.Save(memoryStream);
            m_imageBuffer = memoryStream.ToArray();

            if (bitmapRenderer.Dispatcher.Thread.IsAlive)
                bitmapRenderer.Dispatcher.InvokeShutdown();
        }    

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

程式碼中,UserControl1 即是由 WPF 所產出的,可以利用 XAML 來整合美術設計與程式。
雖然 WPF 的優勢在於互動的介面上,但是用來取代 GDI+ 也是個不錯的選擇。

2010年11月10日

Using WPF to Create Images Dynamically (instead of GDI+)

如果想在 Asp.net 上,利用 WPF 來產生圖片,下列連結是個很好的參考資料。
雖然詢問的標題是如何利用 dll 來處理,但答覆提出了一個直接產出的方式。
Using WPF on a DLL to create Images Dynamically (instead of GDI+)

2010年10月20日

Design-Time Attributes for Components and Behaviors

使用 Expression Blend 設計 Silverlight 介面時,若希望設計人員能更清楚 Property 的含意,可以透過 Design-Time Attributes 來協助。

以下列出可能會使用到的項目:


可以在 class 上加入 Description, 來說明物件的功用 (上圖)
    [Description("可點選物件 - 可以設定為正確或錯誤點選")]
    public class ClickItemProcess : QuestionGroupBase<panel>
    {
       //..
    }


Description 也可以使用在 property 上, 並能以 Category 來分類 (上圖)
        [Category("遊戲功能"), Description("若此物件為錯誤答案,點選後所顯示的動畫(Storyboard)")]
        public string WrongAnswerAnimation
        {
            get { return (string)GetValue(WrongAnswerAnimationProperty); }
            set { SetValue(WrongAnswerAnimationProperty, value); }
        }

如果 property 不希望出現在 Expression Blend 的面板上, 可以將 Browsable 設為 false
        Result m_Result = Result.NoAnswer;
         [Browsable(false)]
        public Result Result
        {
            get { return m_Result; }
            set { m_Result = value; }
        }


如果 properties 屬於 state, element 或 storyboard, 並希望直接出現下拉選單,則可以用 CustomPropertyValueEditorAttribute 來設定。CustomPropertyValueEditor.StateName, CustomPropertyValueEditor.Element, CustomPropertyValueEditor.Storyboard 分別對應這三種不同類型的物件。

        [CustomPropertyValueEditorAttribute(CustomPropertyValueEditor.Storyboard)] 
        public Storyboard RightAnswerAnimation
        {
            get { return (Storyboard)GetValue(RightAnswerAnimationProperty); }
            set { SetValue(RightAnswerAnimationProperty, value); }
        }

另外,對於 TriggerAction, 可以利用 DefaultTriggerAttribute 預設 event。以及利用 TypeConstraintAttribute 來限制 TargetedTriggerAction 與 EventTriggerBase 的 AssociatedObject 型態。

    [DefaultTrigger(typeof(UIElement), typeof(EventTrigger), "MouseLeftButtonDown")]
    [DefaultTrigger(typeof(ButtonBase), typeof(EventTrigger), "Click")]
    public class WaveAction : TriggerAction<UIElement> { }

[reference]
MSDN: Design-Time Attributes for Components
State, Element, and Storyboard Pickers
Silverlight Hover Behavior

2010年10月12日

Free Silverlight Hosting

在部落格上探討 Silverlight 的技術與應用,如果不能顯示範例執行的樣貌,是一件令人苦惱的事情。當然,可以花錢用虛擬主機解決,只是還沒有收到 Google AdSense 的支票,卻要先支出,也太吃虧了。

先前微軟提供了 Microsoft Silverlight Streaming by Windows Live 免費的 hosting 服務,2010 已由 Azure 取代,也有 Introductory Special 免費方案 (Limited Amount at No Charge),但是台灣地區卻遲遲未上線。想亂編一個美國的地址去註冊,因為要輸入信用卡號,覺得不妥而作罷。

好在發現可以利用 Google App Engine application 來解決,雖然要多繞一圈,但還算值得,而且第二個 ap 之後,也會方便許多。步驟如下:

  1. 建立 Python 開發環境,並安裝 Google App Engine 上傳檔案的工具
  2. 利用 Google App Engine 建立帳號並新增 Application
  3. 撰寫 app.yaml 來設定上傳的檔案,而 .py (以 Python為例) 可以是一個空檔案
  4. 利用 appcfg.py 上傳 app.yaml, silverlight.js, .xap 至 Server
  5. 在部落格的網頁中,加入呼叫 .xap 的語法

app.yaml 範例:

application: dysonliu
version: 1
runtime: python
api_version: 1

handlers:

- url: /Silverlight3D.xap
static_files: Silverlight3D.xap
upload: Silverlight3D.xap
mime_type: application/x-silverlight-app

- url: /Silverlight.js
static_files: Silverlight.js
upload: Silverlight.js

- url: .*
script: Silverlight3D.py


插入部落格 HTML 的語法,可以由 Silverlight 的專案抄過來,然後把 Silverlight.js 與 .xap 的位置改掉,並加入寬度與高度的設定。

[reference]
Google 應用服務引擎 - 入門指南
How to embed a silverlight control on a Google Sites webpage?

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