Assembly : System (dans system.dll)
SyntaxePublic MustInherit Class ApplicationSettingsBase Inherits SettingsBase Implements INotifyPropertyChanged
Dim instance As ApplicationSettingsBase
public abstract class ApplicationSettingsBase : SettingsBase, INotifyPropertyChanged
public ref class ApplicationSettingsBase abstract : public SettingsBase, INotifyPropertyChanged
public abstract class ApplicationSettingsBase extends SettingsBase implements INotifyPropertyChanged
public abstract class ApplicationSettingsBase extends SettingsBase implements INotifyPropertyChanged
NotesApplicationSettingsBase ajoute la fonctionnalité suivante à la classe SettingsBase, qui est utilisée par les applications Web :
-
La capacité à détecter des attributs sur une classe wrapper de paramètres dérivée. ApplicationSettingsBase prend en charge le modèle déclaratif utilisé pour les propriétés de classe wrapper, comme décrit ultérieurement.
-
Événements de validation supplémentaires que vous pouvez gérer pour garantir la convenance des paramètres.
Dans l'architecture de paramètres d'application, pour accéder à un groupe de propriétés de paramètres, vous devez dériver une classe wrapper concrète de ApplicationSettingsBase. La classe wrapper personnalise ApplicationSettingsBase des manières suivantes :
-
Pour chaque propriété de paramètres devant être accessible, une propriété publique fortement typée correspondante est ajoutée à la classe wrapper. Cette propriété a des accesseurs get et set pour les paramètres d'application en lecture/écriture, mais uniquement un accesseur get pour les paramètres en lecture seule.
-
Des attributs appropriés doivent être appliqués aux propriétés publiques de la classe wrapper afin d'indiquer les caractéristiques de la propriété des paramètres, telles que la portée du paramètre (application ou utilisateur), la prise en charge, ou non, de l'itinérance par le paramètre, la valeur par défaut du paramètre, le fournisseur de paramètres à utiliser, etc. Chaque propriété doit spécifier sa portée à l'aide de ApplicationScopedSettingAttribute ou de UserScopedSettingAttribute. Les paramètres de portée application sont en lecture seule si le LocalFileSettingsProvider par défaut est utilisé.
La classe ApplicationSettingsBase utilise la réflexion pour détecter ces attributs au moment de l'exécution. La plupart de ces informations sont passées à la couche du fournisseur de paramètres, qui est responsable du stockage, du format de persistance, et ainsi de suite.
Lorsqu'une application a plusieurs classes wrapper de paramètres, chaque classe définit un groupe de paramètres. Chaque groupe a les caractéristiques suivantes :
-
Un groupe peut contenir tout nombre ou type de paramètres de propriété.
-
Si le nom de groupe n'est pas défini explicitement par la décoration de la classe wrapper avec un SettingsGroupNameAttribute, un nom est généré automatiquement.
Par défaut, toutes les applications basées sur le client utilisent LocalFileSettingsProvider pour fournir le stockage. Si un autre fournisseur de paramètres est souhaité, la classe wrapper ou la propriété doit être décorée avec un SettingsProviderAttribute correspondant.
Pour plus d'informations sur l'utilisation des paramètres d'application, consultez Paramètres d'application pour les Windows Forms.
ExempleL'exemple de code suivant illustre l'utilisation de paramètres d'application pour rendre persistants les attributs suivants du formulaire principal : emplacement, taille, couleur d'arrière-plan et texte de barre de titre. Tous ces attributs sont persistants comme propriétés de paramètres d'application uniques dans la classe FormSettings, nommées respectivement FormLocation, FormSize, FormBackColor et FormText. Tous, à l'exception de FormText, sont liés aux données à leurs propriétés de formulaire associées et ont une valeur de paramètre par défaut appliquée, DefaultSettingValueAttribute.
Le formulaire contient quatre contrôles enfants qui ont les noms et fonctions suivants :
-
Un bouton nommé btnBackColor utilisé pour afficher la boîte de dialogue commune Couleur.
-
Un bouton nommé btnReload utilisé pour Reload les paramètres d'application.
-
Un bouton nommé btnReset utilisé pour Reset les paramètres d'application.
-
Une zone de texte nommée tbStatus utilisée pour afficher des informations d'état sur le programme.
Notez qu'après chaque exécution de l'application, un point supplémentaire est ajouté au texte du titre du formulaire.
partial class Form1 : Form { private FormSettings frmSettings1 = new FormSettings(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Associate settings property event handlers. frmSettings1.SettingChanging += new SettingChangingEventHandler( frmSettings1_SettingChanging); frmSettings1.SettingsSaving += new SettingsSavingEventHandler( frmSettings1_SettingsSaving); //Data bind settings properties with straightforward associations. Binding bndBackColor = new Binding("BackColor", frmSettings1, "FormBackColor", true, DataSourceUpdateMode.OnPropertyChanged); this.DataBindings.Add(bndBackColor); Binding bndSize = new Binding("Size", frmSettings1, "FormSize", true, DataSourceUpdateMode.OnPropertyChanged); this.DataBindings.Add(bndSize); Binding bndLocation = new Binding("Location", frmSettings1, "FormLocation", true, DataSourceUpdateMode.OnPropertyChanged); this.DataBindings.Add(bndLocation); //For more complex associations, manually assign associations. String savedText = frmSettings1.FormText; //Since there is no default value for FormText. if (savedText != null) this.Text = savedText; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //Synchronize manual associations first. frmSettings1.FormText = this.Text + '.'; frmSettings1.Save(); } private void btnBackColor_Click(object sender, EventArgs e) { if (DialogResult.OK == colorDialog1.ShowDialog()) { Color c = colorDialog1.Color; this.BackColor = c; } } private void btnReset_Click(object sender, EventArgs e) { frmSettings1.Reset(); this.BackColor = SystemColors.Control; } private void btnReload_Click(object sender, EventArgs e) { frmSettings1.Reload(); } void frmSettings1_SettingChanging(object sender, SettingChangingEventArgs e) { tbStatus.Text = e.SettingName + ": " + e.NewValue; } void frmSettings1_SettingsSaving(object sender, CancelEventArgs e) { //Should check for settings changes first. DialogResult dr = MessageBox.Show( "Save current values for application settings?", "Save Settings", MessageBoxButtons.YesNo); if (DialogResult.No == dr) { e.Cancel = true; } } } //Application settings wrapper class sealed class FormSettings : ApplicationSettingsBase { [UserScopedSettingAttribute()] public String FormText { get { return (String)this["FormText"]; } set { this["FormText"] = value; } } [UserScopedSetting()] [DefaultSettingValueAttribute("0, 0")] public Point FormLocation { get { return (Point)(this["FormLocation"]); } set { this["FormLocation"] = value; } } [UserScopedSetting()] [DefaultSettingValueAttribute("225, 200")] public Size FormSize { get { return (Size)this["FormSize"]; } set { this["FormSize"] = value; } } [UserScopedSetting()] [DefaultSettingValueAttribute("LightGray")] public Color FormBackColor { get { return (Color)this["FormBackColor"]; } set { this["FormBackColor"] = value; } } }
FormSettings ^ formSettings;
public:
AppSettingsForm()
{
formSettings = gcnew FormSettings;
InitializeComponent();
}
private:
void AppSettingsForm_Load(Object^ sender, EventArgs^ e)
{
//Associate settings property event handlers.
formSettings->SettingChanging += gcnew SettingChangingEventHandler(
this, &AppSettingsForm::FormSettings_SettingChanging);
formSettings->SettingsSaving += gcnew SettingsSavingEventHandler(
this,&AppSettingsForm::FormSettings_SettingsSaving);
//Data bind settings properties with straightforward associations.
Binding^ backColorBinding = gcnew Binding("BackColor",
formSettings, "FormBackColor", true,
DataSourceUpdateMode::OnPropertyChanged);
this->DataBindings->Add(backColorBinding);
Binding^ sizeBinding = gcnew Binding("Size", formSettings,
"FormSize", true, DataSourceUpdateMode::OnPropertyChanged);
this->DataBindings->Add(sizeBinding);
Binding^ locationBinding = gcnew Binding("Location", formSettings,
"FormLocation", true, DataSourceUpdateMode::OnPropertyChanged);
this->DataBindings->Add(locationBinding);
//For more complex associations, manually assign associations.
String^ savedText = formSettings->FormText;
//Since there is no default value for FormText.
if (savedText != nullptr)
{
this->Text = savedText;
}
}
private:
void AppSettingsForm_FormClosing(Object^ sender,
FormClosingEventArgs^ e)
{
//Synchronize manual associations first.
formSettings->FormText = this->Text + '.';
formSettings->Save();
}
private:
void BackColorButton_Click(Object^ sender, EventArgs^ e)
{
if (::DialogResult::OK == colorDialog->ShowDialog())
{
Color color = colorDialog->Color;
this->BackColor = color;
}
}
private:
void ResetButton_Click(Object^ sender, EventArgs^ e)
{
formSettings->Reset();
this->BackColor = SystemColors::Control;
}
private:
void ReloadButton_Click(Object^ sender, EventArgs^ e)
{
formSettings->Reload();
}
private:
void FormSettings_SettingChanging(Object^ sender,
SettingChangingEventArgs^ e)
{
statusDisplay->Text = e->SettingName + ": " + e->NewValue;
}
private:
void FormSettings_SettingsSaving(Object^ sender,
CancelEventArgs^ e)
{
//Should check for settings changes first.
::DialogResult^ dialogResult = MessageBox::Show(
"Save current values for application settings?",
"Save Settings", MessageBoxButtons::YesNo);
if (::DialogResult::No == dialogResult)
{
e->Cancel = true;
}
}
};
Sécurité des threads
Plates-formesWindows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Voir aussiRéférence
Membres ApplicationSettingsBaseSystem.Configuration, espace de noms
LocalFileSettingsProvider
ApplicationScopedSettingAttribute, classe
UserScopedSettingAttribute
SettingsGroupNameAttribute
LocalFileSettingsProvider
SettingsProviderAttribute
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ