ApplicationSettingsBase Klas
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Fungeert als basisklasse voor het afleiden van concrete wrapperklassen voor het implementeren van de functie toepassingsinstellingen in Windows Forms-toepassingen.
public ref class ApplicationSettingsBase abstract : System::Configuration::SettingsBase, System::ComponentModel::INotifyPropertyChanged
public abstract class ApplicationSettingsBase : System.Configuration.SettingsBase, System.ComponentModel.INotifyPropertyChanged
type ApplicationSettingsBase = class
inherit SettingsBase
interface INotifyPropertyChanged
Public MustInherit Class ApplicationSettingsBase
Inherits SettingsBase
Implements INotifyPropertyChanged
- Overname
- Implementeringen
Voorbeelden
In het volgende codevoorbeeld ziet u het gebruik van toepassingsinstellingen om de volgende kenmerken van het hoofdformulier te behouden: locatie, grootte, achtergrondkleur en titelbalktekst. Al deze kenmerken worden bewaard als eigenschappen van één toepassingsinstellingen in de klasse, met de FormSettings naam FormLocationen FormSizeFormBackColor respectievelijk , en FormText. Alle behalve FormText en Size zijn gegevens gebonden aan de bijbehorende formuliereigenschappen en hebben een standaardinstellingswaarde toegepast met behulp van DefaultSettingValueAttribute.
Het formulier bevat vier onderliggende besturingselementen met de volgende namen en functies:
Een knop die
btnBackColorwordt gebruikt om het dialoogvenster Kleur algemeen weer te geven.Een knop met de naam die
btnReloadwordt gebruikt voor Reload de toepassingsinstellingen.Een knop met de naam die
btnResetwordt gebruikt voor Reset de toepassingsinstellingen.Een tekstvak dat
tbStatuswordt gebruikt om statusinformatie over het programma weer te geven.
U ziet dat na elke uitvoering van de toepassing een extra puntteken wordt toegevoegd aan de titeltekst van het formulier.
Voor dit codevoorbeeld is een formulier met een klasse met de ColorDialog naam colorDialog1en een StatusStrip besturingselement met een ToolStripStatusLabel benoemde naam tbStatusvereist. Daarnaast zijn er drie Button objecten met de naam btnReload, btnReseten btnBackColor.
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Configuration;
using namespace System::Windows::Forms;
namespace AppSettingsSample
{
//Application settings wrapper class
ref class FormSettings sealed: public ApplicationSettingsBase
{
public:
[UserScopedSettingAttribute()]
property String^ FormText
{
String^ get()
{
return (String^)this["FormText"];
}
void set( String^ value )
{
this["FormText"] = value;
}
}
public:
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("0, 0")]
property Point FormLocation
{
Point get()
{
return (Point)(this["FormLocation"]);
}
void set( Point value )
{
this["FormLocation"] = value;
}
}
public:
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("225, 200")]
property Size FormSize
{
Size get()
{
return (Size)this["FormSize"];
}
void set( Size value )
{
this["FormSize"] = value;
}
}
public:
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("LightGray")]
property Color FormBackColor
{
Color get()
{
return (Color)this["FormBackColor"];
}
void set(Color value)
{
this["FormBackColor"] = value;
}
}
};
ref class AppSettingsForm : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private:
System::ComponentModel::IContainer^ components;
/// <summary>
/// Clean up any resources being used. The Dispose(true)
/// pattern for embedded objects is implemented with this
/// code that just contains a destructor
/// </summary>
public:
~AppSettingsForm()
{
if (components != nullptr)
{
delete components;
}
}
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private:
void InitializeComponent()
{
this->components = nullptr;
this->colorDialog = gcnew System::Windows::Forms::ColorDialog();
this->backColorButton = gcnew System::Windows::Forms::Button();
this->resetButton = gcnew System::Windows::Forms::Button();
this->statusDisplay = gcnew System::Windows::Forms::TextBox();
this->reloadButton = gcnew System::Windows::Forms::Button();
this->SuspendLayout();
//
// backColorButton
//
this->backColorButton->Location = System::Drawing::Point(26, 24);
this->backColorButton->Name = "backColorButton";
this->backColorButton->Size = System::Drawing::Size(159, 23);
this->backColorButton->TabIndex = 0;
this->backColorButton->Text = "Change Background Color";
this->backColorButton->Click += gcnew System::EventHandler
(this,&AppSettingsForm::BackColorButton_Click);
//
// resetButton
//
this->resetButton->Location = System::Drawing::Point(26, 90);
this->resetButton->Name = "resetButton";
this->resetButton->Size = System::Drawing::Size(159, 23);
this->resetButton->TabIndex = 1;
this->resetButton->Text = "Reset to Defaults";
this->resetButton->Click += gcnew System::EventHandler
(this,&AppSettingsForm::ResetButton_Click);
//
// statusDisplay
//
this->statusDisplay->Location = System::Drawing::Point(26, 123);
this->statusDisplay->Name = "statusDisplay";
this->statusDisplay->Size = System::Drawing::Size(159, 20);
this->statusDisplay->TabIndex = 2;
//
// reloadButton
//
this->reloadButton->Location = System::Drawing::Point(26, 57);
this->reloadButton->Name = "reloadButton";
this->reloadButton->Size = System::Drawing::Size(159, 23);
this->reloadButton->TabIndex = 3;
this->reloadButton->Text = "Reload from Storage";
this->reloadButton->Click += gcnew System::EventHandler
(this,&AppSettingsForm::ReloadButton_Click);
//
// AppSettingsForm
//
this->ClientSize = System::Drawing::Size(217, 166);
this->Controls->Add(this->reloadButton);
this->Controls->Add(this->statusDisplay);
this->Controls->Add(this->resetButton);
this->Controls->Add(this->backColorButton);
this->Name = "AppSettingsForm";
this->Text = "App Settings";
this->FormClosing += gcnew
System::Windows::Forms::FormClosingEventHandler
(this,&AppSettingsForm::AppSettingsForm_FormClosing);
this->Load += gcnew System::EventHandler(this,
&AppSettingsForm::AppSettingsForm_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private:
System::Windows::Forms::ColorDialog^ colorDialog;
System::Windows::Forms::Button^ backColorButton;
System::Windows::Forms::Button^ resetButton;
System::Windows::Forms::TextBox^ statusDisplay;
System::Windows::Forms::Button^ reloadButton;
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;
}
}
};
partial class Form1 : Form
{
private FormSettings frmSettings1 = new FormSettings();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
//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 bndLocation = new Binding("Location", frmSettings1,
"FormLocation", true, DataSourceUpdateMode.OnPropertyChanged);
this.DataBindings.Add(bndLocation);
// Assign Size property, since databinding to Size doesn't work well.
this.Size = frmSettings1.FormSize;
//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.FormSize = this.Size;
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; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("0, 0")]
public Point FormLocation
{
get { return (Point)(this["FormLocation"]); }
set { this["FormLocation"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("225, 200")]
public Size FormSize
{
get { return (Size)this["FormSize"]; }
set { this["FormSize"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("LightGray")]
public Color FormBackColor
{
get { return (Color)this["FormBackColor"]; }
set { this["FormBackColor"] = value; }
}
}
Imports System.Configuration
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
Public Shared Sub Main()
End Sub
Private WithEvents frmSettings1 As New FormSettings
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles MyBase.Load
'Settings property event handlers are associated through WithEvents
' and Handles combination.
'Data bind settings properties with straightforward associations.
Dim bndBackColor As New Binding("BackColor", frmSettings1, "FormBackColor",
True, DataSourceUpdateMode.OnPropertyChanged)
DataBindings.Add(bndBackColor)
Dim bndLocation As New Binding("Location", frmSettings1, "FormLocation",
True, DataSourceUpdateMode.OnPropertyChanged)
DataBindings.Add(bndLocation)
' Assign Size property, since databinding to Size doesn't work well.
Size = frmSettings1.FormSize
'For more complex associations, manually assign associations.
Dim savedText As String = frmSettings1.FormText
'Since there is no default value for FormText.
If (savedText IsNot Nothing) Then
Text = savedText
End If
End Sub
Private Sub Form1_FormClosing_1(ByVal sender As Object, ByVal e As _
FormClosingEventArgs) Handles MyBase.FormClosing
'Synchronize manual associations first.
frmSettings1.FormText = Text + "."c
' Save size settings manually.
frmSettings1.FormSize = Size
frmSettings1.Save()
End Sub
Private Sub btnBackColor_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles btnBackColor.Click
If DialogResult.OK = colorDialog1.ShowDialog() Then
Dim c As Color = colorDialog1.Color
BackColor = c
End If
End Sub
Private Sub btnReset_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles btnReset.Click
frmSettings1.Reset()
BackColor = SystemColors.Control
End Sub
Private Sub btnReload_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles btnReload.Click
frmSettings1.Reload()
End Sub
Private Sub frmSettings1_SettingChanging(ByVal sender As Object, ByVal e As _
SettingChangingEventArgs) Handles frmSettings1.SettingChanging
tbStatus.Text = e.SettingName & ": " & e.NewValue.ToString
End Sub
Private Sub frmSettings1_SettingsSaving(ByVal sender As Object, ByVal e As _
CancelEventArgs) Handles frmSettings1.SettingsSaving
'Should check for settings changes first.
Dim dr As DialogResult = MessageBox.Show(
"Save current values for application settings?", "Save Settings",
MessageBoxButtons.YesNo)
If (DialogResult.No = dr) Then
e.Cancel = True
End If
End Sub
End Class
'Application settings wrapper class. This class defines the settings we intend to use in our application.
NotInheritable Class FormSettings
Inherits ApplicationSettingsBase
<UserScopedSettingAttribute()>
Public Property FormText() As String
Get
Return CStr(Me("FormText"))
End Get
Set(ByVal value As String)
Me("FormText") = value
End Set
End Property
<UserScopedSettingAttribute(), DefaultSettingValueAttribute("0, 0")>
Public Property FormLocation() As Point
Get
Return CType(Me("FormLocation"), Point)
End Get
Set(ByVal value As Point)
Me("FormLocation") = value
End Set
End Property
<UserScopedSettingAttribute(), DefaultSettingValueAttribute("225, 200")>
Public Property FormSize() As Size
Get
Return CType(Me("FormSize"), Size)
End Get
Set(ByVal value As Size)
Me("FormSize") = value
End Set
End Property
<UserScopedSettingAttribute(), DefaultSettingValueAttribute("LightGray")>
Public Property FormBackColor() As Color
Get
Return CType(Me("FormBackColor"), Color)
End Get
Set(ByVal value As Color)
Me("FormBackColor") = value
End Set
End Property
End Class
Opmerkingen
ApplicationSettingsBase voegt de volgende functionaliteit toe aan de SettingsBase klasse, die wordt gebruikt door webtoepassingen:
De mogelijkheid om kenmerken te detecteren op een afgeleide wrapperklasse voor instellingen. ApplicationSettingsBase ondersteunt het declaratieve model dat wordt gebruikt voor wrapper-klasse-eigenschappen, zoals later wordt beschreven.
Aanvullende validatiegebeurtenissen die u kunt afhandelen om de juistheid van afzonderlijke instellingen te garanderen.
In de architectuur van de toepassingsinstellingen moet u voor toegang tot een groep instellingeneigenschappen een concrete wrapperklasse afleiden van ApplicationSettingsBase. De wrapper-klasse wordt op de volgende manieren aangepast ApplicationSettingsBase :
Voor elke instellingeneigenschap die moet worden geopend, wordt een bijbehorende sterk getypte openbare eigenschap toegevoegd aan de wrapper-klasse. Deze eigenschap heeft
getensetaccessors voor instellingen voor lees-/schrijftoepassingen, maar alleen eengettoegangsbeheerprogramma voor alleen-lezeninstellingen.De juiste kenmerken moeten worden toegepast op de openbare eigenschappen van de wrapper-klasse om kenmerken van de eigenschap Instellingen aan te geven, zoals het bereik van de instelling (toepassing of gebruiker), of de instelling roaming moet ondersteunen, de standaardwaarde voor de instelling, de instellingenprovider die moet worden gebruikt, enzovoort. Elke eigenschap is vereist om het bereik ervan op te geven, met behulp van ApplicationScopedSettingAttribute of UserScopedSettingAttribute. Instellingen voor toepassingsbereik zijn alleen-lezen als de standaardwaarde LocalFileSettingsProvider wordt gebruikt.
De ApplicationSettingsBase klasse gebruikt reflectie om deze kenmerken tijdens runtime te detecteren. De meeste van deze informatie wordt doorgegeven aan de instellingenproviderlaag, die verantwoordelijk is voor opslag, persistentie-indeling, enzovoort.
Wanneer een toepassing meerdere wrapperklassen voor instellingen heeft, definieert elke klasse een instellingengroep. Elke groep heeft de volgende kenmerken:
Een groep kan een willekeurig aantal of type eigenschapsinstellingen bevatten.
Als de groepsnaam niet expliciet is ingesteld door het decoreren van de wrapper-klasse met een SettingsGroupNameAttribute, wordt er automatisch een naam gegenereerd.
Standaard gebruiken alle clienttoepassingen de LocalFileSettingsProvider opslag. Als een alternatieve instellingenprovider gewenst is, moet de wrapperklasse of eigenschap worden ingericht met een bijbehorende SettingsProviderAttribute.
Zie Application Settings for Windows Forms voor meer informatie over het gebruik van toepassingsinstellingen.
Constructors
| Name | Description |
|---|---|
| ApplicationSettingsBase() |
Initialiseert een exemplaar van de ApplicationSettingsBase klasse tot de standaardstatus. |
| ApplicationSettingsBase(IComponent, String) |
Initialiseert een exemplaar van de klasse met behulp van het opgegeven onderdeel van de ApplicationSettingsBase eigenaar en de instellingensleutel. |
| ApplicationSettingsBase(IComponent) |
Initialiseert een exemplaar van de ApplicationSettingsBase klasse met behulp van het opgegeven eigenaaronderdeel. |
| ApplicationSettingsBase(String) |
Initialiseert een exemplaar van de ApplicationSettingsBase klasse met behulp van de opgegeven instellingensleutel. |
Eigenschappen
| Name | Description |
|---|---|
| Context |
Hiermee haalt u de context van de toepassingsinstellingen op die is gekoppeld aan de groep instellingen. |
| IsSynchronized |
Hiermee wordt een waarde opgehaald die aangeeft of de toegang tot het object wordt gesynchroniseerd (thread safe). (Overgenomen van SettingsBase) |
| Item[String] |
Hiermee haalt u de waarde van de opgegeven eigenschap voor toepassingsinstellingen op of stelt u deze in. |
| Properties |
Hiermee haalt u de verzameling instellingeneigenschappen op in de wrapper. |
| PropertyValues |
Hiermee haalt u een verzameling eigenschapswaarden op. |
| Providers |
Hiermee haalt u de verzameling toepassingsinstellingenproviders op die door de wrapper worden gebruikt. |
| SettingsKey |
Hiermee haalt u de instellingensleutel voor de groep toepassingsinstellingen op of stelt u deze in. |
Methoden
| Name | Description |
|---|---|
| Equals(Object) |
Bepaalt of het opgegeven object gelijk is aan het huidige object. (Overgenomen van Object) |
| GetHashCode() |
Fungeert als de standaardhashfunctie. (Overgenomen van Object) |
| GetPreviousVersion(String) |
Retourneert de waarde van de eigenschap benoemde instellingen voor de vorige versie van dezelfde toepassing. |
| GetType() |
Hiermee haalt u de Type huidige instantie op. (Overgenomen van Object) |
| Initialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection) |
Initialiseert interne eigenschappen die door SettingsBase het object worden gebruikt. (Overgenomen van SettingsBase) |
| MemberwiseClone() |
Hiermee maakt u een ondiepe kopie van de huidige Object. (Overgenomen van Object) |
| OnPropertyChanged(Object, PropertyChangedEventArgs) |
Hiermee wordt de PropertyChanged gebeurtenis gegenereerd. |
| OnSettingChanging(Object, SettingChangingEventArgs) |
Hiermee wordt de SettingChanging gebeurtenis gegenereerd. |
| OnSettingsLoaded(Object, SettingsLoadedEventArgs) |
Hiermee wordt de SettingsLoaded gebeurtenis gegenereerd. |
| OnSettingsSaving(Object, CancelEventArgs) |
Hiermee wordt de SettingsSaving gebeurtenis gegenereerd. |
| Reload() |
Vernieuwt de eigenschapswaarden van de toepassingsinstellingen uit permanente opslag. |
| Reset() |
Hiermee herstelt u de waarden van de persistente toepassingsinstellingen naar de bijbehorende standaardeigenschappen. |
| Save() |
Slaat de huidige waarden van de eigenschappen van de toepassingsinstellingen op. |
| ToString() |
Retourneert een tekenreeks die het huidige object vertegenwoordigt. (Overgenomen van Object) |
| Upgrade() |
Hiermee worden toepassingsinstellingen bijgewerkt om een recentere installatie van de toepassing weer te geven. |
gebeurtenis
| Name | Description |
|---|---|
| PropertyChanged |
Vindt plaats nadat de waarde van een eigenschap toepassingsinstellingen is gewijzigd. |
| SettingChanging |
Vindt plaats voordat de waarde van een eigenschap toepassingsinstellingen wordt gewijzigd. |
| SettingsLoaded |
Vindt plaats nadat de toepassingsinstellingen zijn opgehaald uit de opslag. |
| SettingsSaving |
Vindt plaats voordat waarden worden opgeslagen in het gegevensarchief. |