Pratiques conseillées : création de compléments configurables

Rendre les outils et comportements configurables

L'API d'extensibilité permet de rendre vos outils et comportements configurables. Si un outil ou un comportement est configurable, vous pouvez configurer le composant lors de la création d'une visionneuse. Pour présenter la configuration sur un outil ou un comportement, vous devez implémenter l'interface ESRI.ArcGIS.Client.Extensibility.ISupportsConfiguration. Cette interface nécessite l'implémentation des méthodes suivantes :

Pour donner un exemple d'outil configurable, supposez que vous ayez implémenté un simple contrôle utilisateur qui contient le code sous-jacent par défaut et une zone de texte. Le langage XAML (Extensible Application Markup Language) de ce contrôle peut apparaître sous cette forme :

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

Ce contrôle peut être utilisé dans un outil configurable simple comme suit :

[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
    }

Le fait de cliquer sur le bouton Configurer de la boîte de dialogue Ajouter un outil appelle la méthode de configuration de l'outil. Avec l'implémentation présentée précédemment, la boîte de dialogue suivante apparaît :

Boîte de dialogue Configuration

Lorsque la visionneuse est déployée ou enregistrée, le texte présent dans la zone de texte est conservé sous forme de chaîne. Au chargement de la visionneuse, cette chaîne est transmise à la méthode LoadConfiguration et utilisée pour initialiser la variable de chaîne de configuration. L'exécution de la commande (en cliquant sur le bouton de la commande) affiche une fenêtre qui présente la chaîne de configuration enregistrée :

Boîte de dialogue présentant la chaîne de configuration enregistrée

1/23/2014