ベスト プラクティス: 構成可能なアドインの作成

ツールとビヘイビアーを構成可能にする

拡張 API には、ツールとビヘイビアーを構成可能にするための機能が備わっています。ツールやビヘイビアーが構成可能であれば、目的のコンポーネントをビューアの作成時に構成できます。ツールまたはビヘイビアーを構成可能にするには、ESRI.ArcGIS.Client.Extensibility.ISupportsConfiguration インタフェースを実装する必要があります。さらに、このインタフェースを使用するには次のメソッドを実装する必要があります。

たとえば、構成可能ツールとして、デフォルトのコードビハインドとテキストボックスを含む単純な UserControl を実装する場合、このコントロールの XAML(Extensible Application Markup Language)は次のようになります。

<UserControl x:Class="MyExtension.ConfigurationDialog"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d"
     d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel Margin="10" Background="Transparent">
            <TextBlock Text="Configuration Input:" Margin="0,0,0,5" />
            <TextBox Name="InputTextBox" Width="200" />
        </StackPanel>
    </Grid>
</UserControl>

このコントロールは、単純な構成可能ツールで次のように使用できます。

[Export(typeof(ICommand))]
[DisplayName("Configurable Command")]
[Category("My Tools")]
[Description("Example of a configurable command")]
[DefaultIcon(Path to icon, ex: "/Viewer.Addins;component/Images/Identify.png")]
public class ConfigurableCommand: ICommand, ISupportsConfiguration
    {
        private ConfigurationDialog configDialog = new ConfigurationDialog();

        #region ISupportsConfiguration Members

        public void Configure()
        {
            // When the dialog box opens, it shows the information saved from the last 
												//time the command was configured.
            MapApplication.Current.ShowWindow("Configuration", configDialog);
        }

        public void LoadConfiguration(string configData)
        {
            // If the saved configuration is not null, apply it to the configuration dialog box.
            configDialog.InputTextBox.Text = configData ?? "";
        }

        public string SaveConfiguration()
        {
            // Save the information from the dialog box, and 
            return configDialog.InputTextBox.Text;
     
        }

        #endregion

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            // Return true so that the command can always be executed.
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            // Show the configuration data. 
            MapApplication.Current.ShowWindow("Configuration", new TextBlock()
            {
                Text = configDialog.InputTextBox.Text,
                TextWrapping = TextWrapping.Wrap,
                Margin = new Thickness(30),
                MaxWidth = 480
            });
        }

        #endregion
    }

[ツールの追加] ダイアログ ボックスで [構成] をクリックすると、該当するツールの Configure メソッドが呼び出されます。上記の例では、次のようなダイアログ ボックスが表示されます。

構成ダイアログ ボックス

ビューアを配置または保存すると、テキストボックス内のテキストが文字列として保持されます。ビューアを読み込むと、この文字列が LoadConfiguration メソッドに渡され、それを使用して構成の String 変数が初期化されます。(コマンドのボタンをクリックして)コマンドを実行するとメッセージ ボックスが開き、保存した構成文字列が表示されます。

保存した構成文字列がダイアログ ボックスに表示される

11/30/2012