Skip to content

Instantly share code, notes, and snippets.

@renestein
Created December 17, 2010 14:49
Show Gist options
  • Save renestein/745046 to your computer and use it in GitHub Desktop.
Save renestein/745046 to your computer and use it in GitHub Desktop.
Třída ViewModelBase.cs
public abstract class ViewModelBase : PropertyNotificationBase, ITransientStateManager, IInitialize, IActivated, IDeactivated
{
public ViewModelBase()
{
}
static ViewModelBase()
{
TransientStateHelper = new TransientStateHelper();
}
public static ITransientStateHelper TransientStateHelper
{
get;
set;
}
public ViewModelBase(string pageTitle) : this()
{
PageTitle = pageTitle;
}
public string AppTitle
{
get;
set;
}
public string PageTitle
{
get;
set;
}
public bool SuppressValidating
{
get;
set;
}
public bool HasValidData
{
get
{
return ValidateData();
}
}
public virtual bool ValidateData()
{
return true;
}
public bool IsInvalidModel
{
get;
set;
}
public void Init()
{
try
{
IsInvalidModel = true;
AppTitle = GlobalConstants.APP_MAIN_TITLE;
SuppressValidating = false;
IsInvalidModel = false;
DoInternalInit();
ThreadPool.QueueUserWorkItem(_ =>
{
try
{
DoInternalAsyncInit();
}
catch (Exception e)
{
Debug.WriteLine(e);
if (!isThreadAbortException(e))
{
throw;
}
}
});
}
catch (Exception e)
{
Debug.WriteLine(e);
throw;
}
}
protected virtual void DoInternalAsyncInit()
{
}
protected virtual void DoInternalInit()
{
}
public virtual void Activate()
{
}
public virtual void Deactivate()
{
}
public bool CanUseModel
{
get
{
return !IsInvalidModel && IsAllInitDataLoaded();
}
}
protected virtual bool IsAllInitDataLoaded()
{
return true;
}
protected virtual Object DoInternalSaveTransientState()
{
var state = TransientStateHelper.GetTransientData(this);
if (state == null)
{
return state;
}
var retKnownTypesDictionary = new KnownTypesDictionary(state);
return retKnownTypesDictionary;
}
protected virtual void DoInternalLoadTransientState(Object obj)
{
var state = obj as Dictionary<string, object>;
if (state == null)
{
return;
}
TransientStateHelper.RestoreTransientData(this, state);
if (!CanUseModel)
{
Init();
}
}
private bool isThreadAbortException(Exception e)
{
Exception currentException = e;
while (currentException != null)
{
if (currentException is ThreadAbortException)
{
return true;
}
currentException = currentException.InnerException;
}
return false;
}
Object ITransientStateManager.SaveState()
{
return DoInternalSaveTransientState();
}
void ITransientStateManager.LoadState(Object obj)
{
DoInternalLoadTransientState(obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment