ServiceContainer 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供 IServiceContainer 介面的簡單實作。 無法繼承這個類別。
public ref class ServiceContainer : IDisposable, IServiceProvider, System::ComponentModel::Design::IServiceContainer
public ref class ServiceContainer sealed : IServiceProvider, System::ComponentModel::Design::IServiceContainer
public ref class ServiceContainer : IDisposable, System::ComponentModel::Design::IServiceContainer
public class ServiceContainer : IDisposable, IServiceProvider, System.ComponentModel.Design.IServiceContainer
public sealed class ServiceContainer : IServiceProvider, System.ComponentModel.Design.IServiceContainer
public class ServiceContainer : IDisposable, System.ComponentModel.Design.IServiceContainer
type ServiceContainer = class
interface IServiceContainer
interface IServiceProvider
interface IDisposable
type ServiceContainer = class
interface IServiceContainer
interface IServiceProvider
Public Class ServiceContainer
Implements IDisposable, IServiceContainer, IServiceProvider
Public NotInheritable Class ServiceContainer
Implements IServiceContainer, IServiceProvider
Public Class ServiceContainer
Implements IDisposable, IServiceContainer
- 繼承
-
ServiceContainer
- 實作
範例
下列範例程式示範服務鏈結和連結 ServiceContainer 對象鏈結的結果服務可用性。 此程式提供使用者介面,可讓您查看連結服務鏈結內服務的可用性,並使用 AddService、GetService和 RemoveService 方法,以及鏈接的服務容器。
#using <System.Windows.Forms.dll>
#using <System.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;
ref class ServiceObjectControl;
// Example form provides UI for demonstrating service sharing behavior
// of a network of IServiceContainer/IServiceProvider controls.
public ref class ServiceForm: public System::Windows::Forms::Form
{
private:
// Root service container control for tree.
ServiceObjectControl^ root;
// Button for clearing any provided services and resetting tree states.
System::Windows::Forms::Button^ reset_button;
// Color list used to color code controls.
array<Color>^colorkeys;
// Strings used to reflect text service.
array<String^>^keystrings;
public:
ServiceForm()
{
InitializeComponent();
CreateServiceControlTree();
colorkeys = gcnew array<Color>(6);
colorkeys[ 0 ] = Color::Beige;
colorkeys[ 1 ] = Color::SeaShell;
colorkeys[ 2 ] = Color::LightGreen;
colorkeys[ 3 ] = Color::LightBlue;
colorkeys[ 4 ] = Color::Khaki;
colorkeys[ 5 ] = Color::CadetBlue;
array<String^>^stringstemp = {"No service use","Service not accessible","Service provided",
"Service obtained","Service accessible","No further access"};
keystrings = stringstemp;
}
private:
void CreateServiceControlTree();
void InitializeComponent();
internal:
void ResetServiceTree( Object^ sender, EventArgs^ e );
public:
void UpdateServiceCoverage();
protected:
virtual void OnPaint( System::Windows::Forms::PaintEventArgs^ e ) override;
};
// Example service type contains a text string, sufficient
// to demonstrate service sharing.
public ref class TextService
{
public:
String^ text;
TextService()
: text( String::Empty )
{}
TextService( String^ text )
{
this->text = text;
}
};
public enum class TextServiceState
{
ServiceNotObtained, ServiceObtained, ServiceProvided, ServiceNotFound
};
// Example Form for entering a string.
private ref class StringInputDialog: public System::Windows::Forms::Form
{
private:
System::Windows::Forms::Button^ ok_button;
System::Windows::Forms::Button^ cancel_button;
public:
System::Windows::Forms::TextBox^ inputTextBox;
StringInputDialog( String^ text )
{
InitializeComponent();
inputTextBox->Text = text;
}
private:
void InitializeComponent()
{
this->ok_button = gcnew System::Windows::Forms::Button;
this->cancel_button = gcnew System::Windows::Forms::Button;
this->inputTextBox = gcnew System::Windows::Forms::TextBox;
this->SuspendLayout();
this->ok_button->Anchor = static_cast<AnchorStyles>(System::Windows::Forms::AnchorStyles::Bottom |
System::Windows::Forms::AnchorStyles::Right);
this->ok_button->Location = System::Drawing::Point( 180, 43 );
this->ok_button->Name = "ok_button";
this->ok_button->TabIndex = 1;
this->ok_button->Text = "OK";
this->ok_button->DialogResult = System::Windows::Forms::DialogResult::OK;
this->cancel_button->Anchor = static_cast<AnchorStyles>(System::Windows::Forms::AnchorStyles::Bottom |
System::Windows::Forms::AnchorStyles::Right);
this->cancel_button->Location = System::Drawing::Point( 260, 43 );
this->cancel_button->Name = "cancel_button";
this->cancel_button->TabIndex = 2;
this->cancel_button->Text = "Cancel";
this->cancel_button->DialogResult = System::Windows::Forms::DialogResult::Cancel;
this->inputTextBox->Location = System::Drawing::Point( 6, 9 );
this->inputTextBox->Name = "inputTextBox";
this->inputTextBox->Size = System::Drawing::Size( 327, 20 );
this->inputTextBox->TabIndex = 0;
this->inputTextBox->Text = "";
this->inputTextBox->Anchor = static_cast<AnchorStyles>((System::Windows::Forms::AnchorStyles::Top |
System::Windows::Forms::AnchorStyles::Left) | System::Windows::Forms::AnchorStyles::Right);
this->ClientSize = System::Drawing::Size( 342, 73 );
array<System::Windows::Forms::Control^>^temp0 = {this->inputTextBox,this->cancel_button,this->ok_button};
this->Controls->AddRange( temp0 );
this->MinimumSize = System::Drawing::Size( 350, 100 );
this->Name = "StringInputDialog";
this->Text = "Text Service Provide String Dialog";
this->ResumeLayout( false );
}
};
// An example user control that uses ServiceContainer to add, remove,
// and access services through a linkable service container network.
public ref class ServiceObjectControl: public System::Windows::Forms::UserControl
{
public:
// This example user control implementation provides a wrapper for
// ServiceContainer, supporting a linked service container network.
ServiceContainer^ serviceContainer;
private:
// Parent form reference for main program function access.
ServiceForm^ parent;
public:
// String for label displayed on the control to indicate the
// control's current service-related configuration state.
String^ label;
private:
// The current state of the control reflecting whether it has
// obtained or provided a text service.
TextServiceState state_;
public:
property TextServiceState state
{
TextServiceState get()
{
return state_;
}
void set( TextServiceState value )
{
if ( (TextServiceState)value == TextServiceState::ServiceProvided )
this->BackColor = Color::LightGreen;
else
if ( (TextServiceState)value == TextServiceState::ServiceNotObtained )
this->BackColor = Color::White;
else
if ( (TextServiceState)value == TextServiceState::ServiceObtained )
this->BackColor = Color::LightBlue;
else
if ( (TextServiceState)value == TextServiceState::ServiceNotFound )
this->BackColor = Color::SeaShell;
state_ = value;
}
}
ServiceObjectControl( ServiceObjectControl^ serviceContainerParent, System::Drawing::Size size,
Point location, ServiceForm^ parent )
{
this->state_ = TextServiceState::ServiceNotObtained;
this->BackColor = Color::Beige;
this->label = String::Empty;
this->Size = size;
this->Location = location;
this->parent = parent;
if ( serviceContainerParent == nullptr )
serviceContainer = gcnew ServiceContainer;
else
serviceContainer = gcnew ServiceContainer( serviceContainerParent->serviceContainer );
}
protected:
// Paint method override draws the label string on the control.
virtual void OnPaint( System::Windows::Forms::PaintEventArgs^ e ) override
{
e->Graphics->DrawString( label, gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ), 5, 5 );
}
// Process mouse-down behavior for click.
virtual void OnMouseDown( System::Windows::Forms::MouseEventArgs^ e ) override
{
if ( e->Button == ::MouseButtons::Left )
{
if ( state_ != TextServiceState::ServiceProvided )
{
// Attempt to update text from service, and set color
// state accordingly.
TextService^ ts = dynamic_cast<TextService^>(serviceContainer->GetService( TextService::typeid ));
if ( ts != nullptr )
{
this->label = ts->text;
state = TextServiceState::ServiceObtained;
}
else
{
this->label = "Service Not Found";
state = TextServiceState::ServiceNotFound;
}
}
}
if ( e->Button == ::MouseButtons::Right )
{
if ( state_ == TextServiceState::ServiceProvided )
{
// Remove service if the container provided it.
if ( serviceContainer->GetService( TextService::typeid ) != nullptr )
{
serviceContainer->RemoveService( TextService::typeid, true );
state = TextServiceState::ServiceNotObtained;
this->label = "Service Removed";
}
}
else
{
// Obtain string and provide text service.
StringInputDialog^ form = gcnew StringInputDialog( "Test String" );
form->StartPosition = FormStartPosition::CenterParent;
if ( form->ShowDialog() == DialogResult::OK )
{
if ( serviceContainer->GetService( TextService::typeid ) != nullptr )
serviceContainer->RemoveService( TextService::typeid, true );
parent->ResetServiceTree( this, gcnew EventArgs );
serviceContainer->AddService( TextService::typeid, gcnew TextService( form->inputTextBox->Text ), true );
state = TextServiceState::ServiceProvided;
this->label = String::Format( "Provided Text: {0}", form->inputTextBox->Text );
}
}
}
parent->UpdateServiceCoverage();
}
public:
// Method accesses the TextService to test the visibility of the service
// from the control, and sets the UI state accordingly.
void ReflectServiceVisibility()
{
if ( state_ == TextServiceState::ServiceObtained )
{
if ( serviceContainer->GetService( TextService::typeid ) == nullptr )
this->BackColor = Color::CadetBlue;
}
else
if ( state_ != TextServiceState::ServiceProvided )
{
if ( serviceContainer->GetService( TextService::typeid ) == nullptr )
{
this->BackColor = Color::White;
return;
}
// Service available.
if ( state_ == TextServiceState::ServiceNotFound )
this->BackColor = Color::Khaki;
else
if ( state_ == TextServiceState::ServiceNotObtained && !label->Equals( "Service Removed" ) )
this->BackColor = Color::Khaki;
}
}
};
void ServiceForm::CreateServiceControlTree()
{
// Create root service control.
ServiceObjectControl^ control1 = gcnew ServiceObjectControl( nullptr,System::Drawing::Size( 300, 40 ),Point(10,80),this );
root = control1;
// Create first tier - pass parent with service object control 1
ServiceObjectControl^ control2 = gcnew ServiceObjectControl( control1,System::Drawing::Size( 200, 30 ),Point(50,160),this );
ServiceObjectControl^ control3 = gcnew ServiceObjectControl( control1,System::Drawing::Size( 200, 30 ),Point(50,240),this );
// Create second tier A - pass parent with service object control 2
ServiceObjectControl^ control4 = gcnew ServiceObjectControl( control2,System::Drawing::Size( 180, 20 ),Point(300,145),this );
ServiceObjectControl^ control5 = gcnew ServiceObjectControl( control2,System::Drawing::Size( 180, 20 ),Point(300,185),this );
// Create second tier B - pass parent with service object control 3
ServiceObjectControl^ control6 = gcnew ServiceObjectControl( control3,System::Drawing::Size( 180, 20 ),Point(300,225),this );
ServiceObjectControl^ control7 = gcnew ServiceObjectControl( control3,System::Drawing::Size( 180, 20 ),Point(300,265),this );
// Add controls.
array<Control^>^temp1 = {control1,control2,control3,control4,control5,control6,control7};
this->Controls->AddRange( temp1 );
}
void ServiceForm::ResetServiceTree( Object^ /*sender*/, EventArgs^ /*e*/ )
{
// Remove the service from the service tree.
if ( root->serviceContainer->GetService( TextService::typeid ) != nullptr )
root->serviceContainer->RemoveService( TextService::typeid, true );
// Set all controls to "not obtained" and clear their labels.
for ( int i = 0; i < Controls->Count; i++ )
if ( !Controls[ i ]->Equals( reset_button ) )
{
(dynamic_cast<ServiceObjectControl^>(Controls[ i ]))->state = TextServiceState::ServiceNotObtained;
(dynamic_cast<ServiceObjectControl^>(Controls[ i ]))->label = String::Empty;
(dynamic_cast<ServiceObjectControl^>(Controls[ i ]))->BackColor = Color::Beige;
}
}
void ServiceForm::UpdateServiceCoverage()
{
// Have each control set state to reflect service availability.
for ( int i = 0; i < Controls->Count; i++ )
if ( !Controls[ i ]->Equals( reset_button ) )
(dynamic_cast<ServiceObjectControl^>(Controls[ i ]))->ReflectServiceVisibility();
}
void ServiceForm::InitializeComponent()
{
this->reset_button = gcnew System::Windows::Forms::Button;
this->SuspendLayout();
//
// reset_button
//
this->reset_button->Location = System::Drawing::Point( 392, 88 );
this->reset_button->Name = "reset_button";
this->reset_button->TabIndex = 0;
this->reset_button->TabStop = false;
this->reset_button->Text = "Reset";
this->reset_button->Click += gcnew System::EventHandler( this, &ServiceForm::ResetServiceTree );
//
// ServiceForm
//
this->ClientSize = System::Drawing::Size( 512, 373 );
array<System::Windows::Forms::Control^>^temp2 = {this->reset_button};
this->Controls->AddRange( temp2 );
this->MinimumSize = System::Drawing::Size( 520, 400 );
this->Name = "ServiceForm";
this->Text = "Service Container Architecture Example";
this->ResumeLayout( false );
}
void ServiceForm::OnPaint( System::Windows::Forms::PaintEventArgs^ e )
{
e->Graphics->DrawString( "The following tree diagram represents a hierarchy of linked service containers in controls.",
gcnew System::Drawing::Font( "Arial",9 ), gcnew SolidBrush( Color::Black ), 4, 4 );
e->Graphics->DrawString( "This example demonstrates the propagation behavior of services through a linked service object tree.",
gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ), 4, 26 );
e->Graphics->DrawString( "Right-click a component to add or replace a text service, or to remove it if the component provided it.",
gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ), 4, 38 );
e->Graphics->DrawString( "Left-click a component to update text from the text service if available.",
gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ), 4, 50 );
// Draw lines to represent tree branches.
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 20, 125, 20, 258 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 21, 175, 45, 175 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 21, 258, 45, 258 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 255, 175, 285, 175 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 255, 258, 285, 258 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 155, 285, 195 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 238, 285, 278 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 155, 290, 155 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 195, 290, 195 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 238, 290, 238 );
e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 278, 290, 278 );
// Draw color key.
e->Graphics->DrawRectangle( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 20, 305, 410, 60 );
int y = 0;
for ( int i = 0; i < 3; i++ )
{
e->Graphics->FillRectangle( gcnew SolidBrush( colorkeys[ y ] ), 25 + (i * 140), 310, 20, 20 );
e->Graphics->DrawRectangle( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 25 + (i * 140), 310, 20, 20 );
e->Graphics->DrawString( keystrings[ y ], gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ),
(float)50 + (i * 140), 315 );
y++;
e->Graphics->FillRectangle( gcnew SolidBrush( colorkeys[ y ] ), 25 + (i * 140), 340, 20, 20 );
e->Graphics->DrawRectangle( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 25 + (i * 140), 340, 20, 20 );
e->Graphics->DrawString( keystrings[ y ], gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ),
(float)50 + (i * 140), 345 );
y++;
}
}
[STAThread]
int main()
{
Application::Run( gcnew ServiceForm );
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ServiceArchitectureExample
{
// Example form provides UI for demonstrating service sharing behavior
// of a network of IServiceContainer/IServiceProvider controls.
public class ServiceForm : System.Windows.Forms.Form
{
// Root service container control for tree.
private ServiceObjectControl root;
// Button for clearing any provided services and resetting tree states.
private System.Windows.Forms.Button reset_button;
// Color list used to color code controls.
private Color[] colorkeys;
// Strings used to reflect text service.
private string[] keystrings;
public ServiceForm()
{
InitializeComponent();
CreateServiceControlTree();
colorkeys = new Color[] { Color.Beige, Color.SeaShell, Color.LightGreen, Color.LightBlue, Color.Khaki, Color.CadetBlue };
keystrings = new string[] { "No service use", "Service not accessible", "Service provided", "Service obtained", "Service accessible", "No further access" };
}
private void CreateServiceControlTree()
{
// Create root service control.
ServiceObjectControl control1 = new ServiceObjectControl(null, new Size(300, 40), new Point(10, 80), this);
root = control1;
// Create first tier - pass parent with service object control 1
ServiceObjectControl control2 = new ServiceObjectControl(control1, new Size(200, 30), new Point(50, 160), this);
ServiceObjectControl control3 = new ServiceObjectControl(control1, new Size(200, 30), new Point(50, 240), this);
// Create second tier A - pass parent with service object control 2
ServiceObjectControl control4 = new ServiceObjectControl(control2, new Size(180, 20), new Point(300, 145), this);
ServiceObjectControl control5 = new ServiceObjectControl(control2, new Size(180, 20), new Point(300, 185), this);
// Create second tier B - pass parent with service object control 3
ServiceObjectControl control6 = new ServiceObjectControl(control3, new Size(180, 20), new Point(300, 225), this);
ServiceObjectControl control7 = new ServiceObjectControl(control3, new Size(180, 20), new Point(300, 265), this);
// Add controls.
this.Controls.AddRange( new Control[] { control1, control2, control3, control4, control5, control6, control7 } );
}
internal void ResetServiceTree(object sender, EventArgs e)
{
// Remove the service from the service tree.
if( root.serviceContainer.GetService(typeof(TextService)) != null )
root.serviceContainer.RemoveService(typeof(TextService), true);
// Set all controls to "not obtained" and clear their labels.
for( int i=0; i<Controls.Count; i++ )
if( !Controls[i].Equals(reset_button) )
{
((ServiceObjectControl)Controls[i]).state = TextServiceState.ServiceNotObtained;
((ServiceObjectControl)Controls[i]).label = string.Empty;
((ServiceObjectControl)Controls[i]).BackColor = Color.Beige;
}
}
public void UpdateServiceCoverage()
{
// Have each control set state to reflect service availability.
for( int i=0; i<Controls.Count; i++ )
if( !Controls[i].Equals(reset_button) )
((ServiceObjectControl)Controls[i]).ReflectServiceVisibility();
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.reset_button = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// reset_button
//
this.reset_button.Location = new System.Drawing.Point(392, 88);
this.reset_button.Name = "reset_button";
this.reset_button.TabIndex = 0;
this.reset_button.TabStop = false;
this.reset_button.Text = "Reset";
this.reset_button.Click += new System.EventHandler(this.ResetServiceTree);
//
// ServiceForm
//
this.ClientSize = new System.Drawing.Size(512, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.reset_button});
this.MinimumSize = new System.Drawing.Size(520, 400);
this.Name = "ServiceForm";
this.Text = "Service Container Architecture Example";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new ServiceForm());
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawString("The following tree diagram represents a hierarchy of linked service containers in controls.", new Font("Arial", 9), new SolidBrush(Color.Black), 4, 4);
e.Graphics.DrawString("This example demonstrates the propagation behavior of services through a linked service object tree.", new Font("Arial", 8), new SolidBrush(Color.Black), 4, 26);
e.Graphics.DrawString("Right-click a component to add or replace a text service, or to remove it if the component provided it.", new Font("Arial", 8), new SolidBrush(Color.Black), 4, 38);
e.Graphics.DrawString("Left-click a component to update text from the text service if available.", new Font("Arial", 8), new SolidBrush(Color.Black), 4, 50);
// Draw lines to represent tree branches.
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 20, 125, 20, 258);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 21, 175, 45, 175);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 21, 258, 45, 258);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 255, 175, 285, 175);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 255, 258, 285, 258);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 285, 155, 285, 195);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 285, 238, 285, 278);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 285, 155, 290, 155);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 285, 195, 290, 195);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 285, 238, 290, 238);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 285, 278, 290, 278);
// Draw color key.
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1), 20, 305, 410, 60);
int y=0;
for( int i=0; i<3; i++ )
{
e.Graphics.FillRectangle(new SolidBrush(colorkeys[y]), 25+(i*140), 310, 20, 20);
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1), 25+(i*140), 310, 20, 20);
e.Graphics.DrawString(keystrings[y], new Font("Arial", 8), new SolidBrush(Color.Black), 50+(i*140), 315);
y++;
e.Graphics.FillRectangle(new SolidBrush(colorkeys[y]), 25+(i*140), 340, 20, 20);
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1), 25+(i*140), 340, 20, 20);
e.Graphics.DrawString(keystrings[y], new Font("Arial", 8), new SolidBrush(Color.Black), 50+(i*140), 345);
y++;
}
}
}
// An example user control that uses ServiceContainer to add, remove,
// and access services through a linkable service container network.
public class ServiceObjectControl : System.Windows.Forms.UserControl
{
// This example user control implementation provides a wrapper for
// ServiceContainer, supporting a linked service container network.
public ServiceContainer serviceContainer;
// Parent form reference for main program function access.
private ServiceForm parent;
// String for label displayed on the control to indicate the
// control's current service-related configuration state.
public string label;
// The current state of the control reflecting whether it has
// obtained or provided a text service.
private TextServiceState state_;
public TextServiceState state
{
get
{
return state_;
}
set
{
if( (TextServiceState)value == TextServiceState.ServiceProvided )
this.BackColor = Color.LightGreen;
else if( (TextServiceState)value == TextServiceState.ServiceNotObtained )
this.BackColor = Color.White;
else if( (TextServiceState)value == TextServiceState.ServiceObtained )
this.BackColor = Color.LightBlue;
else if( (TextServiceState)value == TextServiceState.ServiceNotFound )
this.BackColor = Color.SeaShell;
state_ = value;
}
}
public ServiceObjectControl(ServiceObjectControl serviceContainerParent, Size size, Point location, ServiceForm parent)
{
this.state_ = TextServiceState.ServiceNotObtained;
this.BackColor = Color.Beige;
this.label = string.Empty;
this.Size = size;
this.Location = location;
this.parent = parent;
if( serviceContainerParent == null )
serviceContainer = new ServiceContainer();
else
serviceContainer = new ServiceContainer(serviceContainerParent.serviceContainer);
}
// Paint method override draws the label string on the control.
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawString(label, new Font("Arial", 8), new SolidBrush(Color.Black), 5, 5);
}
// Process mouse-down behavior for click.
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Left )
{
if( state_ != TextServiceState.ServiceProvided )
{
// Attempt to update text from service, and set color
// state accordingly.
TextService ts = (TextService)serviceContainer.GetService(typeof(TextService));
if( ts != null )
{
this.label = ts.text;
state = TextServiceState.ServiceObtained;
}
else
{
this.label = "Service Not Found";
state = TextServiceState.ServiceNotFound;
}
}
}
if( e.Button == MouseButtons.Right )
{
if( state_ == TextServiceState.ServiceProvided )
{
// Remove service if the container provided it.
if( serviceContainer.GetService(typeof(TextService)) != null )
{
serviceContainer.RemoveService(typeof(TextService), true);
state = TextServiceState.ServiceNotObtained;
this.label = "Service Removed";
}
}
else
{
// Obtain string and provide text service.
using (StringInputDialog form = new StringInputDialog("Test String"))
{
form.StartPosition = FormStartPosition.CenterParent;
if (form.ShowDialog() == DialogResult.OK)
{
if (serviceContainer.GetService(typeof(TextService)) != null)
serviceContainer.RemoveService(typeof(TextService), true);
parent.ResetServiceTree(this, new EventArgs());
serviceContainer.AddService(typeof(TextService), new TextService(form.inputTextBox.Text), true);
state = TextServiceState.ServiceProvided;
this.label = "Provided Text: " + form.inputTextBox.Text;
}
}
}
}
parent.UpdateServiceCoverage();
}
// Method accesses the TextService to test the visibility of the service
// from the control, and sets the UI state accordingly.
public void ReflectServiceVisibility()
{
if( state_ == TextServiceState.ServiceObtained )
{
if( serviceContainer.GetService(typeof(TextService)) == null )
this.BackColor = Color.CadetBlue;
}
else if( state_ != TextServiceState.ServiceProvided )
{
if( serviceContainer.GetService(typeof(TextService)) == null )
{
this.BackColor = Color.White;
return;
}
// Service available.
if( state_ == TextServiceState.ServiceNotFound )
this.BackColor = Color.Khaki;
else if( state_ == TextServiceState.ServiceNotObtained && label != "Service Removed" )
this.BackColor = Color.Khaki;
}
}
}
// Example service type contains a text string, sufficient
// to demonstrate service sharing.
public class TextService
{
public string text;
public TextService() : this(string.Empty)
{
}
public TextService(string text)
{
this.text = text;
}
}
public enum TextServiceState
{
ServiceNotObtained,
ServiceObtained,
ServiceProvided,
ServiceNotFound
}
// Example Form for entering a string.
internal class StringInputDialog : System.Windows.Forms.Form
{
private System.Windows.Forms.Button ok_button;
private System.Windows.Forms.Button cancel_button;
public System.Windows.Forms.TextBox inputTextBox;
public StringInputDialog(string text)
{
InitializeComponent();
inputTextBox.Text = text;
}
private void InitializeComponent()
{
this.ok_button = new System.Windows.Forms.Button();
this.cancel_button = new System.Windows.Forms.Button();
this.inputTextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.ok_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.ok_button.Location = new System.Drawing.Point(180, 43);
this.ok_button.Name = "ok_button";
this.ok_button.TabIndex = 1;
this.ok_button.Text = "OK";
this.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK;
this.cancel_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.cancel_button.Location = new System.Drawing.Point(260, 43);
this.cancel_button.Name = "cancel_button";
this.cancel_button.TabIndex = 2;
this.cancel_button.Text = "Cancel";
this.cancel_button.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.inputTextBox.Location = new System.Drawing.Point(6, 9);
this.inputTextBox.Name = "inputTextBox";
this.inputTextBox.Size = new System.Drawing.Size(327, 20);
this.inputTextBox.TabIndex = 0;
this.inputTextBox.Text = "";
this.inputTextBox.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.ClientSize = new System.Drawing.Size(342, 73);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.inputTextBox,
this.cancel_button,
this.ok_button});
this.MinimumSize = new System.Drawing.Size(350, 100);
this.Name = "StringInputDialog";
this.Text = "Text Service Provide String Dialog";
this.ResumeLayout(false);
}
}
}
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
' Example form provides UI for demonstrating service sharing behavior
' of a network of IServiceContainer/IServiceProvider controls.
Public Class ServiceForm
Inherits System.Windows.Forms.Form
' Root service container control for tree.
Private root As ServiceObjectControl
' Button for clearing any provided services and resetting tree states.
Private WithEvents reset_button As System.Windows.Forms.Button
' Color list used to color code controls.
Private colorkeys() As Color
' Strings used to reflect text service.
Private keystrings() As String
Public Sub New()
InitializeComponent()
CreateServiceControlTree()
colorkeys = New Color() {Color.Beige, Color.SeaShell, Color.LightGreen, Color.LightBlue, Color.Khaki, Color.CadetBlue}
keystrings = New String() {"No service use", "Service not accessible", "Service provided", "Service obtained", "Service accessible", "No further access"}
End Sub
Private Sub CreateServiceControlTree()
' Create root service control.
Dim control1 As New ServiceObjectControl(Nothing, New Size(300, 40), New Point(10, 80), Me)
root = control1
' Create first tier - pass parent with service object control 1
Dim control2 As New ServiceObjectControl(control1, New Size(200, 30), New Point(50, 160), Me)
Dim control3 As New ServiceObjectControl(control1, New Size(200, 30), New Point(50, 240), Me)
' Create second tier A - pass parent with service object control 2
Dim control4 As New ServiceObjectControl(control2, New Size(180, 20), New Point(300, 145), Me)
Dim control5 As New ServiceObjectControl(control2, New Size(180, 20), New Point(300, 185), Me)
' Create second tier B - pass parent with service object control 3
Dim control6 As New ServiceObjectControl(control3, New Size(180, 20), New Point(300, 225), Me)
Dim control7 As New ServiceObjectControl(control3, New Size(180, 20), New Point(300, 265), Me)
' Add controls.
Me.Controls.AddRange(New Control() {control1, control2, control3, control4, control5, control6, control7})
End Sub
Friend Sub ResetServiceTree(ByVal sender As Object, ByVal e As EventArgs) Handles reset_button.Click
' Remove the service from the service tree.
If (root.serviceContainer.GetService(GetType(TextService)) IsNot Nothing) Then
root.serviceContainer.RemoveService(GetType(TextService), True)
End If
' Set all controls to "not obtained" and clear their labels.
Dim i As Integer
For i = 0 To Controls.Count - 1
If Not Controls(i).Equals(reset_button) Then
CType(Controls(i), ServiceObjectControl).state = TextServiceState.ServiceNotObtained
CType(Controls(i), ServiceObjectControl).label = String.Empty
CType(Controls(i), ServiceObjectControl).BackColor = Color.Beige
End If
Next i
End Sub
Public Sub UpdateServiceCoverage()
' Have each control set state to reflect service availability.
Dim i As Integer
For i = 0 To Controls.Count - 1
If Not Controls(i).Equals(reset_button) Then
CType(Controls(i), ServiceObjectControl).ReflectServiceVisibility()
End If
Next i
End Sub
Private Sub InitializeComponent()
Me.reset_button = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'reset_button
'
Me.reset_button.Location = New System.Drawing.Point(392, 88)
Me.reset_button.Name = "reset_button"
Me.reset_button.TabIndex = 0
Me.reset_button.TabStop = False
Me.reset_button.Text = "Reset"
'
'ServiceForm
'
Me.ClientSize = New System.Drawing.Size(512, 373)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.reset_button})
Me.MinimumSize = New System.Drawing.Size(520, 400)
Me.Name = "ServiceForm"
Me.Text = "Service Container Architecture Example"
Me.ResumeLayout(False)
End Sub
<STAThread()> _
Shared Sub Main()
Application.Run(New ServiceForm())
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawString("The following tree diagram represents a hierarchy of linked service containers in controls.", New Font("Arial", 9), New SolidBrush(Color.Black), 4, 4)
e.Graphics.DrawString("This example demonstrates the propagation behavior of services through a linked service object tree.", New Font("Arial", 8), New SolidBrush(Color.Black), 4, 26)
e.Graphics.DrawString("Right-click a component to add or replace a text service, or to remove it if the component provided it.", New Font("Arial", 8), New SolidBrush(Color.Black), 4, 38)
e.Graphics.DrawString("Left-click a component to update text from the text service if available.", New Font("Arial", 8), New SolidBrush(Color.Black), 4, 50)
' Draw lines to represent tree branches.
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 20, 125, 20, 258)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 21, 175, 45, 175)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 21, 258, 45, 258)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 255, 175, 285, 175)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 255, 258, 285, 258)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 285, 155, 285, 195)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 285, 238, 285, 278)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 285, 155, 290, 155)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 285, 195, 290, 195)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 285, 238, 290, 238)
e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 285, 278, 290, 278)
' Draw color key.
e.Graphics.DrawRectangle(New Pen(New SolidBrush(Color.Black), 1), 20, 305, 410, 60)
Dim y As Integer = 0
Dim i As Integer
For i = 0 To 2
e.Graphics.FillRectangle(New SolidBrush(colorkeys(y)), 25 + i * 140, 310, 20, 20)
e.Graphics.DrawRectangle(New Pen(New SolidBrush(Color.Black), 1), 25 + i * 140, 310, 20, 20)
e.Graphics.DrawString(keystrings(y), New Font("Arial", 8), New SolidBrush(Color.Black), 50 + i * 140, 315)
y += 1
e.Graphics.FillRectangle(New SolidBrush(colorkeys(y)), 25 + i * 140, 340, 20, 20)
e.Graphics.DrawRectangle(New Pen(New SolidBrush(Color.Black), 1), 25 + i * 140, 340, 20, 20)
e.Graphics.DrawString(keystrings(y), New Font("Arial", 8), New SolidBrush(Color.Black), 50 + i * 140, 345)
y += 1
Next i
End Sub
End Class
' An example user control that uses ServiceContainer to add, remove,
' and access services through a linkable service container network.
Public Class ServiceObjectControl
Inherits System.Windows.Forms.UserControl
' This example user control implementation provides a wrapper for
' ServiceContainer, supporting a linked service container network.
Public serviceContainer As serviceContainer
' Parent form reference for main program function access.
Private parentserviceForm As ServiceForm
' String for label displayed on the control to indicate the
' control's current service-related configuration state.
Public label As String
' The current state of the control reflecting whether it has
' obtained or provided a text service.
Private state_ As TextServiceState
Public Property state() As TextServiceState
Get
Return state_
End Get
Set(ByVal Value As TextServiceState)
If CType(Value, TextServiceState) = TextServiceState.ServiceProvided Then
Me.BackColor = Color.LightGreen
ElseIf CType(Value, TextServiceState) = TextServiceState.ServiceNotObtained Then
Me.BackColor = Color.White
ElseIf CType(Value, TextServiceState) = TextServiceState.ServiceObtained Then
Me.BackColor = Color.LightBlue
ElseIf CType(Value, TextServiceState) = TextServiceState.ServiceNotFound Then
Me.BackColor = Color.SeaShell
End If
state_ = Value
End Set
End Property
Public Sub New(ByVal serviceContainerParent As ServiceObjectControl, ByVal size As Size, ByVal location As Point, ByVal parent As ServiceForm)
Me.state_ = TextServiceState.ServiceNotObtained
Me.BackColor = Color.Beige
Me.label = String.Empty
Me.Size = size
Me.Location = location
Me.parentserviceForm = parent
If serviceContainerParent Is Nothing Then
serviceContainer = New ServiceContainer()
Else
serviceContainer = New ServiceContainer(serviceContainerParent.serviceContainer)
End If
End Sub
' Paint method override draws the label string on the control.
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawString(label, New Font("Arial", 8), New SolidBrush(Color.Black), 5, 5)
End Sub
' Process mouse-down behavior for click.
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = System.Windows.Forms.MouseButtons.Left Then
If state_ <> TextServiceState.ServiceProvided Then
' Attempt to update text from service, and set color
' state accordingly.
Dim ts As TextService = CType(serviceContainer.GetService(GetType(TextService)), TextService)
If (ts IsNot Nothing) Then
Me.label = ts.text
state = TextServiceState.ServiceObtained
Else
Me.label = "Service Not Found"
state = TextServiceState.ServiceNotFound
End If
End If
End If
If e.Button = System.Windows.Forms.MouseButtons.Right Then
If state_ = TextServiceState.ServiceProvided Then
' Remove service if the container provided it.
If (serviceContainer.GetService(GetType(TextService)) IsNot Nothing) Then
serviceContainer.RemoveService(GetType(TextService), True)
state = TextServiceState.ServiceNotObtained
Me.label = "Service Removed"
End If
Else
' Obtain string and provide text service.
Using form As New StringInputDialog("Test String")
form.StartPosition = FormStartPosition.CenterParent
If form.ShowDialog() = DialogResult.OK Then
If (serviceContainer.GetService(GetType(TextService)) IsNot Nothing) Then
serviceContainer.RemoveService(GetType(TextService), True)
End If
parentserviceForm.ResetServiceTree(Me, New EventArgs())
serviceContainer.AddService(GetType(TextService), New TextService(form.inputTextBox.Text), True)
state = TextServiceState.ServiceProvided
Me.label = "Provided Text: " + form.inputTextBox.Text
End If
End Using
End If
End If
parentserviceForm.UpdateServiceCoverage()
End Sub
' Method accesses the TextService to test the visibility of the service
' from the control, and sets the UI state accordingly.
Public Sub ReflectServiceVisibility()
If state_ = TextServiceState.ServiceObtained Then
If serviceContainer.GetService(GetType(TextService)) Is Nothing Then
Me.BackColor = Color.CadetBlue
End If
ElseIf state_ <> TextServiceState.ServiceProvided Then
If serviceContainer.GetService(GetType(TextService)) Is Nothing Then
Me.BackColor = Color.White
Return
End If
' Service available.
If state_ = TextServiceState.ServiceNotFound Then
Me.BackColor = Color.Khaki
ElseIf state_ = TextServiceState.ServiceNotObtained AndAlso label <> "Service Removed" Then
Me.BackColor = Color.Khaki
End If
End If
End Sub
End Class
' Example service type contains a text string, sufficient
' for use to demonstrate service sharing.
Public Class TextService
Public [text] As String
Public Sub New()
MyClass.New(String.Empty)
End Sub
Public Sub New(ByVal [text] As String)
Me.text = [text]
End Sub
End Class
Public Enum TextServiceState
ServiceNotObtained
ServiceObtained
ServiceProvided
ServiceNotFound
End Enum
' Example Form for entering a string.
Friend Class StringInputDialog
Inherits System.Windows.Forms.Form
Private ok_button As System.Windows.Forms.Button
Private cancel_button As System.Windows.Forms.Button
Public inputTextBox As System.Windows.Forms.TextBox
Public Sub New(ByVal [text] As String)
InitializeComponent()
inputTextBox.Text = [text]
End Sub
Private Sub InitializeComponent()
Me.ok_button = New System.Windows.Forms.Button()
Me.cancel_button = New System.Windows.Forms.Button()
Me.inputTextBox = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
Me.ok_button.Anchor = System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right
Me.ok_button.Location = New System.Drawing.Point(180, 43)
Me.ok_button.Name = "ok_button"
Me.ok_button.TabIndex = 1
Me.ok_button.Text = "OK"
Me.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK
Me.cancel_button.Anchor = System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right
Me.cancel_button.Location = New System.Drawing.Point(260, 43)
Me.cancel_button.Name = "cancel_button"
Me.cancel_button.TabIndex = 2
Me.cancel_button.Text = "Cancel"
Me.cancel_button.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.inputTextBox.Location = New System.Drawing.Point(6, 9)
Me.inputTextBox.Name = "inputTextBox"
Me.inputTextBox.Size = New System.Drawing.Size(327, 20)
Me.inputTextBox.TabIndex = 0
Me.inputTextBox.Text = ""
Me.inputTextBox.Anchor = System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right
Me.ClientSize = New System.Drawing.Size(342, 73)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.inputTextBox, Me.cancel_button, Me.ok_button})
Me.MinimumSize = New System.Drawing.Size(350, 100)
Me.Name = "StringInputDialog"
Me.Text = "Text Service Provide String Dialog"
Me.ResumeLayout(False)
End Sub
End Class
備註
ServiceContainer 物件可用來儲存和提供服務。 ServiceContainer 實作 IServiceContainer 介面。
您可以使用建構函式來建立 ServiceContainer 物件,這個建構函式會新增父 IServiceContainer,以便選擇性地從所有父系 IServiceContainer 物件加入或移除服務,包括直接父 IServiceContainer。 若要透過父系從連結至此 IServiceContainer 的所有 IServiceContainer 實作新增或移除服務,請呼叫接受布爾值,指出是否要升級服務要求的布爾值 AddService 或 RemoveService 方法多載。
注意
套用至此類別的 HostProtectionAttribute 屬性具有下列 Resources 屬性值:SharedState。 HostProtectionAttribute 不會影響傳統型應用程式(通常是透過按兩下圖示、輸入命令或在瀏覽器中輸入URL來啟動)。 如需詳細資訊,請參閱 HostProtectionAttribute 類別或 SQL Server 程式設計與主機保護屬性。
建構函式
ServiceContainer() |
初始化 ServiceContainer 類別的新實例。 |
ServiceContainer(IServiceProvider) |
使用指定的父服務提供者,初始化 ServiceContainer 類別的新實例。 |
屬性
DefaultServices |
取得直接透過 ServiceContainer實作的預設服務。 |
方法
AddService(Type, Object, Boolean) |
將指定的服務加入至服務容器。 |
AddService(Type, Object) |
將指定的服務加入至服務容器。 |
AddService(Type, ServiceCreatorCallback, Boolean) |
將指定的服務加入至服務容器。 |
AddService(Type, ServiceCreatorCallback) |
將指定的服務加入至服務容器。 |
Dispose() |
處置此服務容器。 |
Dispose(Boolean) |
處置此服務容器。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
GetService(Type) |
取得要求的服務。 |
GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
RemoveService(Type, Boolean) |
從服務容器中移除指定的服務類型。 |
RemoveService(Type) |
從服務容器中移除指定的服務類型。 |
ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |