|
using System;
using System.Data;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows;
namespace PlaylistCreatorUI
{
public class WindowBase : Window
{
#region Constants
private const int WIN_DEFAULT_WIDTH = 640;
private const int WIN_DEFAULT_HEIGHT = 480;
private const string WIN_SETTINGS_COLUMNNAME_TOP = "WinTop";
private const string WIN_SETTINGS_COLUMNNAME_LEFT = "WinLeft";
private const string WIN_SETTINGS_COLUMNNAME_WIDTH = "WinWidth";
private const string WIN_SETTINGS_COLUMNNAME_HEIGHT = "WinHeight";
private const string WIN_SETTINGS_COLUMNNAME_STATE = "WinState";
private const string WIN_SETTINGS_TABLENAME = "WindowSettings";
#endregion
private IsolatedStorageFile _isoStoreFile;
private string WinSettingFilename
{
get
{
return string.Format("{0}_win.xml", this.ToString());
}
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
InitIsoStor();
if (_isoStoreFile.GetFileNames(WinSettingFilename).Contains(WinSettingFilename))
{
_isoStoreFile.DeleteFile(WinSettingFilename);
}
DataSet ds = InitDS();
DataRow dr = ds.Tables[WIN_SETTINGS_TABLENAME].NewRow();
dr[WIN_SETTINGS_COLUMNNAME_TOP] = this.Top;
dr[WIN_SETTINGS_COLUMNNAME_LEFT] = this.Left;
dr[WIN_SETTINGS_COLUMNNAME_WIDTH] = this.Width;
dr[WIN_SETTINGS_COLUMNNAME_HEIGHT] = this.Height;
dr[WIN_SETTINGS_COLUMNNAME_STATE] = this.WindowState;
ds.Tables[WIN_SETTINGS_TABLENAME].Rows.Add(dr);
try
{
using (IsolatedStorageFileStream oStr
= new IsolatedStorageFileStream(WinSettingFilename, FileMode.Create, _isoStoreFile))
{
ds.WriteXml(oStr);
}
}
catch (Exception){}
base.OnClosing(e);
}
protected override void OnInitialized(EventArgs e)
{
InitIsoStor();
if (!_isoStoreFile.GetFileNames(WinSettingFilename).Contains(WinSettingFilename))
{
SetWindowDefaults();
base.OnInitialized(e);
return;
}
DataSet ds = new DataSet();
using (IsolatedStorageFileStream oStr
= new IsolatedStorageFileStream(WinSettingFilename, FileMode.Open, _isoStoreFile))
{
ds.ReadXml(oStr);
}
if (!ds.Tables.Contains(WIN_SETTINGS_TABLENAME))
{
SetWindowDefaults();
base.OnInitialized(e);
return;
}
if (ds.Tables[WIN_SETTINGS_TABLENAME].Rows.Count != 1)
{
SetWindowDefaults();
base.OnInitialized(e);
return;
}
DataRow dr = ds.Tables[WIN_SETTINGS_TABLENAME].Rows[0];
double wtop = -1;
double wleft = -1;
double wwidth = -1;
double wheight = -1;
if (!dr.IsNull(WIN_SETTINGS_COLUMNNAME_TOP))
{
double.TryParse(dr[WIN_SETTINGS_COLUMNNAME_TOP].ToString(), out wtop);
this.Top = wtop;
}
if (!dr.IsNull(WIN_SETTINGS_COLUMNNAME_LEFT))
{
double.TryParse(dr[WIN_SETTINGS_COLUMNNAME_LEFT].ToString(), out wleft);
this.Left = wleft;
}
if (!dr.IsNull(WIN_SETTINGS_COLUMNNAME_WIDTH))
{
double.TryParse(dr[WIN_SETTINGS_COLUMNNAME_WIDTH].ToString(), out wwidth);
this.Width = wwidth;
}
if (!dr.IsNull(WIN_SETTINGS_COLUMNNAME_HEIGHT))
{
double.TryParse(dr[WIN_SETTINGS_COLUMNNAME_HEIGHT].ToString(), out wheight);
this.Height = wheight;
}
if (!dr.IsNull(WIN_SETTINGS_COLUMNNAME_STATE))
{
this.WindowState
= (WindowState)Enum.Parse(typeof(WindowState),
dr[WIN_SETTINGS_COLUMNNAME_STATE].ToString()
);
}
base.OnInitialized(e);
}
#region Window Settings Helper methods
private void InitIsoStor()
{
if (_isoStoreFile == null)
{
if (AppDomain.CurrentDomain.ActivationContext != null)
{
_isoStoreFile = IsolatedStorageFile.GetUserStoreForApplication();
}
else
{
_isoStoreFile
= IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
}
}
}
private DataSet InitDS()
{
DataTable dt = new DataTable(WIN_SETTINGS_TABLENAME);
dt.Columns.Add(new DataColumn(WIN_SETTINGS_COLUMNNAME_TOP, typeof(double)));
dt.Columns.Add(new DataColumn(WIN_SETTINGS_COLUMNNAME_LEFT, typeof(double)));
dt.Columns.Add(new DataColumn(WIN_SETTINGS_COLUMNNAME_WIDTH, typeof(double)));
dt.Columns.Add(new DataColumn(WIN_SETTINGS_COLUMNNAME_HEIGHT, typeof(double)));
dt.Columns.Add(new DataColumn(WIN_SETTINGS_COLUMNNAME_STATE, typeof(WindowState)));
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
private void SetWindowDefaults()
{
this.Width = WIN_DEFAULT_WIDTH;
this.Height = WIN_DEFAULT_HEIGHT;
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
this.WindowState = WindowState.Normal;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Dave has been working in the software industry for a few years now. Having graduated with his BS in Computer Science & Engineering from the University of Toledo in 2002, Dave enjoys learning about new technologies and likes to apply new programming methods to solve age-old challenges. In his spare time he is addicted to movies (sci-fi, horror, anything good!) and spending time with family and friends. Dave also harbors a secret desire to make it big in the film/music industry—here’s lookin’ at you, kid! ;o)