|
using System;
using System.Globalization;
using System.Windows;
using Microsoft.Win32;
using TaskList.ViewModel;
namespace TaskList
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, IMainWindowView
{
public MainWindow()
{
InitializeComponent();
}
#region IMainWindowView Members
public string GetOpenFileName()
{
ITaskListApplication app = Application.Current as ITaskListApplication;
OpenFileDialog dialog = new OpenFileDialog();
if (!string.IsNullOrEmpty(app.FileName))
{
dialog.FileName = app.FileName;
}
else
{
dialog.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
dialog.Filter = "Task List (*.tsk)|*.tsk|All files (*.*)|*.*";
bool? result = dialog.ShowDialog();
if (result.HasValue && result.Value)
{
return dialog.FileName;
}
return string.Empty;
}
public string GetSaveAsFileName()
{
ITaskListApplication app = Application.Current as ITaskListApplication;
SaveFileDialog dialog = new SaveFileDialog();
if (!string.IsNullOrEmpty(app.FileName))
{
dialog.FileName = app.FileName;
}
else
{
dialog.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
dialog.Filter = "Task List (*.tsk)|*.tsk|All files (*.*)|*.*";
bool? result = dialog.ShowDialog();
if (result.HasValue && result.Value)
{
return dialog.FileName;
}
return string.Empty;
}
#endregion
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MainWindowViewModel viewModel = CodeProject.Windows.Markup.ViewModel.GetInstance(this) as MainWindowViewModel;
if (viewModel != null)
{
if (viewModel.Tasks.IsDirty)
{
MessageBoxResult result = MessageBox.Show(this,
"Task List was modified. Save?",
string.Format(CultureInfo.CurrentUICulture,
"{0} - Save Task List?", Title),
MessageBoxButton.YesNoCancel,
MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
string fileName = viewModel.TaskListApplication.FileName;
if (string.IsNullOrEmpty(fileName))
{
fileName = GetSaveAsFileName();
}
if (!string.IsNullOrEmpty(fileName))
{
viewModel.TaskListApplication.SaveTasks(fileName);
}
else
{
e.Cancel = true;
}
}
else if (result == MessageBoxResult.Cancel)
{
e.Cancel = true;
}
}
}
}
}
}
|
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.
Windows developer with 10+ years experience working in the banking industry.