首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

變更自訂活動設計器上的圖示

     編輯:關於.NET

環境基礎:.NET Framework 4.0 Beta 2

建立自訂活動設計器時,更換圖示通常是第一件想做的事。在 WF4 中這並不困難。以簡單的 WriteLine 活動為例:

[Designer(typeof(MyWriteLineDesigner))]
public sealed class MyWriteLine : CodeActivity
{
    // 定義字串型輸入參數
    public InArgument<string> Text { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        string text = context.GetValue(this.Text);
        Console.WriteLine(text);
    }
}

透過 Designer 屬性關聯自訂設計器,標準設計器 XAML 如下:

<sap:ActivityDesigner x:Class="WorkflowConsoleApplication10.MyWriteLineDesigner1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">
    <Grid></Grid>
</sap:ActivityDesigner>

將活動拖入工作流設計器,預設顯示如下:

預設活動設計器

問題:屬性視窗無法直接設定 Icon

設計器雖有 Icon 屬性,但目前屬性視窗無法直接編輯:

屬性視窗無法設定Icon

解決方案:在 XAML 中定義 DrawingBrush 圖示

需在 XAML 中加入 sap:ActivityDesigner.Icon 區段,使用 DrawingBrush + BitmapImage 指向圖片資源:

<sap:ActivityDesigner x:Class="WorkflowConsoleApplication10.MyWriteLineDesigner1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">
    <sap:ActivityDesigner.Icon>
        <DrawingBrush>
            <DrawingBrush.Drawing>
                <ImageDrawing>
                    <ImageDrawing.Rect>
                        <Rect Location="0,0" Size="16,16" />
                    </ImageDrawing.Rect>
                    <ImageDrawing.ImageSource>
                        <BitmapImage UriSource="Clipboard_edit.png" />
                    </ImageDrawing.ImageSource>
                </ImageDrawing>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </sap:ActivityDesigner.Icon>
    <Grid></Grid>
</sap:ActivityDesigner>

關鍵設定:圖片建置動作設為 Resource

圖片檔 Clipboard_edit.png 的「建置動作」必須設為 Resource:

建置動作設為Resource 屬性視窗確認

重新建置後效果

專案重新建置後,工作流設計器中的活動圖示即更新為新圖示:

更新後的活動圖示

來源參考

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved