共用方式為


使用 ModelItem 編輯環境

ModelItem 編輯上下文是主應用程式用來與設計師通訊的物件。 EditingContext 提供兩種方法:ItemsServices,這兩種方法可以使用。

項目集合

集合 Items 可用來存取主機與設計工具之間共享的數據,或可供所有設計工具使用的數據。 此集合具有下列功能,可透過 ContextItemManager 類別存取:

  1. GetValue

  2. Subscribe

  3. Unsubscribe

  4. SetValue

服務集合

集合 Services 可用來存取設計工具用來與主機互動的服務,或是所有設計工具所使用的服務。 此集合具有下列值得注意的方法:

  1. Publish

  2. Subscribe

  3. Unsubscribe

  4. GetService

指派設計師一項活動

若要指定活動所使用的設計工具,則會使用 Designer 屬性。

[Designer(typeof(MyClassDesigner))]  
public sealed class MyClass : CodeActivity  
{
}

建立服務

若要建立一個服務,作為設計師和主機之間的資訊橋樑,必須建立介面和實作。 方法會使用 Publish 介面來定義服務的成員,而實作包含服務的邏輯。 在下列程式代碼範例中,會建立服務介面和實作。

public interface IMyService  
    {  
        IEnumerable<string> GetValues(string DisplayName);  
    }  
  
    public class MyServiceImpl : IMyService  
    {  
        public IEnumerable<string> GetValues(string DisplayName)  
        {  
            return new string[]  {
                DisplayName + " One",
                DisplayName + " Two",  
                "Three " + DisplayName  
            } ;  
        }  
    }  

發佈服務

若要讓設計師取用服務,首先必須由主機使用 Publish 方法發佈。

this.Context.Services.Publish<IMyService>(new MyServiceImpl);  

訂閱服務

設計師使用 Subscribe 方法中的 OnModelItemChanged 方法來取得服務的存取權。 下列代碼段示範如何訂閱服務。

protected override void OnModelItemChanged(object newItem)  
{  
    if (!subscribed)  
    {  
        this.Context.Services.Subscribe<IMyService>(  
            servInstance =>  
            {  
                listBox1.ItemsSource = servInstance.GetValues(this.ModelItem.Properties["DisplayName"].ComputedValue.ToString());  
            }  
            );  
        subscribed = true;
    }  
}  

使用 Items 集合共享資料

使用 Items 集合與使用 Services 集合類似,不同之處在於使用 SetValue 取代 Publish。 此集合更適合在設計工具與主機之間共用簡單數據,而不是複雜的功能。

EditingContext 主機項目和服務

.NET Framework 提供一些透過編輯內容存取的內建項目和服務。

項目

服務業: