CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
ErrorDialog.cs
Go to the documentation of this file.
1#if !NETFX_CORE && !XAMARIN
2//-----------------------------------------------------------------------
3// <copyright file="ErrorDialog.cs" company="Marimer LLC">
4// Copyright (c) Marimer LLC. All rights reserved.
5// Website: https://cslanet.com
6// </copyright>
7// <summary>Displays an error dialog for any exceptions</summary>
8//-----------------------------------------------------------------------
9using System;
10using System.Windows;
11using System.Windows.Controls;
12
13namespace Csla.Xaml
14{
19 public class ErrorDialog : Control, IErrorDialog
20 {
24 public ErrorDialog()
25 {
26 this.DialogIcon = MessageBoxImage.Exclamation;
27 this.DataContextChanged += ErrorDialog_DataContextChanged;
28 }
29
30 void ErrorDialog_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
31 {
32 DetachSource(e.OldValue);
33 AttachSource(e.NewValue);
34 }
35
40 public static readonly DependencyProperty DialogTitleProperty = DependencyProperty.Register(
41 "DialogTitle",
42 typeof(string),
43 typeof(ErrorDialog),
44 null);
45
50 public string DialogTitle
51 {
52 get { return (string)GetValue(DialogTitleProperty); }
53 set { SetValue(DialogTitleProperty, value); }
54 }
55
61 public static readonly DependencyProperty DialogFirstLineProperty = DependencyProperty.Register(
62 "DialogFirstLine",
63 typeof(string),
64 typeof(ErrorDialog),
65 null);
66
72 public string DialogFirstLine
73 {
74 get { return (string)GetValue(DialogFirstLineProperty); }
75 set { SetValue(DialogFirstLineProperty, value); }
76 }
77
83 public static readonly DependencyProperty ShowExceptionDetailProperty = DependencyProperty.Register(
84 "ShowExceptionDetail",
85 typeof(bool),
86 typeof(ErrorDialog),
87 null);
88
95 {
96 get { return (bool)GetValue(ShowExceptionDetailProperty); }
97 set { SetValue(ShowExceptionDetailProperty, value); }
98 }
99
105 public static readonly DependencyProperty DialogIconProperty = DependencyProperty.Register(
106 "DialogIcon",
107 typeof(MessageBoxImage),
108 typeof(ErrorDialog),
109 null);
110
115 public MessageBoxImage DialogIcon
116 {
117 get { return (MessageBoxImage)GetValue(DialogIconProperty); }
118 set { SetValue(DialogIconProperty, value); }
119 }
120
121 internal void Register(object source)
122 {
123 AttachSource(source);
124 }
125
126 private void AttachSource(object source)
127 {
128 var dp = source as System.Windows.Data.DataSourceProvider;
129 if (dp != null)
130 dp.DataChanged += source_DataChanged;
131 }
132
133 private void DetachSource(object source)
134 {
135 var dp = source as System.Windows.Data.DataSourceProvider;
136 if (dp != null)
137 dp.DataChanged -= source_DataChanged;
138 }
139
140 private void source_DataChanged(object sender, EventArgs e)
141 {
142 var dp = sender as System.Windows.Data.DataSourceProvider;
143 if (dp != null && dp.Error != null)
144 {
145 string error;
146 if (this.ShowExceptionDetail)
147 error = dp.Error.ToString();
148 else
149 error = dp.Error.Message;
150
151 string output;
152 if (string.IsNullOrEmpty(this.DialogFirstLine))
153 output = error;
154 else
155 output = string.Format("{0}{1}{2}", this.DialogFirstLine, Environment.NewLine, error);
156
157 MessageBox.Show(
158 output,
159 this.DialogTitle,
160 MessageBoxButton.OK,
161 this.DialogIcon);
162 }
163 }
164
165#region IErrorDialog Members
166
167 void IErrorDialog.Register(object source)
168 {
169 AttachSource(source);
170 }
171
172#endregion
173 }
174}
175#endif
Displays an error dialog for any exceptions that occur in a CslaDataProvider.
Definition: ErrorDialog.cs:20
bool ShowExceptionDetail
Gets or sets the first line of text displayed within the error dialog (before the exception message).
Definition: ErrorDialog.cs:95
string DialogFirstLine
Gets or sets the first line of text displayed within the error dialog (before the exception message).
Definition: ErrorDialog.cs:73
ErrorDialog()
Creates a new instance of the control.
Definition: ErrorDialog.cs:24
string DialogTitle
Gets or sets the title of the error dialog.
Definition: ErrorDialog.cs:51
static readonly DependencyProperty DialogTitleProperty
Gets or sets the title of the error dialog.
Definition: ErrorDialog.cs:40
MessageBoxImage DialogIcon
Gets or sets the icon displayed in the dialog.
Definition: ErrorDialog.cs:116
static readonly DependencyProperty DialogIconProperty
Gets or sets a value indicating whether the dialog should include exception details or just the excep...
Definition: ErrorDialog.cs:105
static readonly DependencyProperty DialogFirstLineProperty
Gets or sets the first line of text displayed within the error dialog (before the exception message).
Definition: ErrorDialog.cs:61
static readonly DependencyProperty ShowExceptionDetailProperty
Gets or sets a value indicating whether the dialog should include exception details or just the excep...
Definition: ErrorDialog.cs:83
Interface defining the interaction between a CslaDataSource and an error dialog control.
Definition: IErrorDialog.cs:21