I have an app with quite a few different GridViews all of which need to have their settings persisted. Currently, I am creating a file for each GridView to persist the settings between sessions. Is there a way to consolidate this so that more than one gridview can share a settings file? Here is the current code I have for doing persistence:
/// <summary>
/// Interaction logic for BillingActivityView.xaml
/// </summary>
public
partial
class
BillingActivityView
{
private
static
readonly
string
_appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
private
static
readonly
string
_bambooSettingsPath = Path.Combine(_appDataPath, @
"Bamboo\Settings"
);
private
static
readonly
string
_testsGridSettingsFilePath = Path.Combine(_bambooSettingsPath, @
"TestsGridSettings.bin"
);
private
static
readonly
string
_oldBillClassesGridSettingsFilePath = Path.Combine(_bambooSettingsPath, @
"OldBillClassesGridSettings.bin"
);
private
static
readonly
string
_insuranceStatusGridSettingsFilePath = Path.Combine(_bambooSettingsPath, @
"InsuranceStatusGridSettings.bin"
);
/// <summary>
/// Initializes a new instance of the <see cref="DocumentImageView"/> class.
/// </summary>
public
BillingActivityView()
{
InitializeComponent();
ServiceProvider.RegisterPersistenceProvider<ICustomPropertyProvider>(
typeof
(RadGridView),
new
GridViewCustomPropertyProvider());
}
private
void
TestsGridView_Initialized(
object
sender, EventArgs e)
{
LoadGridSettings(TestsGridView, _testsGridSettingsFilePath);
}
private
void
OldBillClassesGridView_Initialized(
object
sender, EventArgs e)
{
LoadGridSettings(OldBillClassesGridView, _oldBillClassesGridSettingsFilePath);
}
private
void
InsuranceStatusGridView_Initialized(
object
sender, EventArgs e)
{
LoadGridSettings(InsuranceStatusGridView, _insuranceStatusGridSettingsFilePath);
}
public
void
LoadGridSettings(RadGridView gridView,
string
settingsPath)
{
var manager =
new
PersistenceManager();
if
(!Directory.Exists(_bambooSettingsPath))
{
Directory.CreateDirectory(_bambooSettingsPath);
}
// Load settings if there is something to load
if
(File.Exists(settingsPath))
{
try
{
var fileStream = File.OpenRead(settingsPath);
manager.Load(gridView, fileStream);
fileStream.Close();
}
catch
(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message,
"BILLINGACTIVITYVIEW"
);
}
}
}
public
void
PersistAllGridSettings()
{
PersistGridSettings(TestsGridView, _testsGridSettingsFilePath);
PersistGridSettings(OldBillClassesGridView, _oldBillClassesGridSettingsFilePath);
PersistGridSettings(InsuranceStatusGridView, _insuranceStatusGridSettingsFilePath);
}
public
void
PersistGridSettings(RadGridView gridView,
string
settingsPath)
{
var manager =
new
PersistenceManager();
var stream = manager.Save(gridView);
using
(var fileStream = File.Create(settingsPath))
{
stream.CopyTo(fileStream);
}
}
}