Things in processing
2012-12-11 14:59 PingX 阅读(230) 评论(0) 收藏 举报WPF/Prism http://compositewpf.codeplex.com/
Bootstrapper
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return new ShellWindow();
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)this.Shell;
App.Current.MainWindow.Show();
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new AggregateModuleCatalog();
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
DirectoryModuleCatalog directoryCatalog = new DirectoryModuleCatalog() { ModulePath = @"Modules" };
((AggregateModuleCatalog)ModuleCatalog).AddCatalog(directoryCatalog);
//ConfigurationModuleCatalog configurationCatalog = new ConfigurationModuleCatalog();
//((AggregateModuleCatalog)ModuleCatalog).AddCatalog(configurationCatalog);
}
}
RequestNavigate
public static void RequestNavigate<T>(this IRegionManager regionManager, IUnityContainer container, string regionName, string viewName)
{
// Get a reference to the main region.
IRegion region = regionManager.Regions[regionName];
if (region == null) return;
// Check to see if we need to create an instance of the view.
var view = region.GetView(viewName);
if (view == null || !(view is T))
{
// Create a new instance of the EmployeeDetailsView using the Unity container.
view = container.Resolve<T>();
// Add the view to the main region. This automatically activates the view too.
region.Add(view, viewName);
region.Activate(view);
}
else
{
// The view has already been added to the region so just activate it.
region.Activate(view);
}
}
ToString
public override string ToString()
{
string ret;
try
{
StringBuilder objectText = new StringBuilder();
PropertyInfo[] properties = this.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
string _name = property.Name;
try
{
object _valObj = property.GetValue(this, null);
string _value = (_valObj ?? "").ToString();
objectText.Append(_name + "=" + _value + "|");
}
catch
{
objectText.Append(_name + "=?" + property.PropertyType.ToString() + "|");
}
}
ret = "{" + objectText.ToString() + "}";
}
catch
{
ret = base.ToString();
}
return ret;
}
Logger
public interface ILogger<T>
{
void Debug(string message);
void DebugFormat(string format, string message);
void Info(string message);
void InfoFormat(string format, string message);
void Error(string message);
void ErrorFormat(string format, string message);
}
public class DiagnosticsLogger<T> : ILogger<T>
{
public void Debug(string message)
{
System.Diagnostics.Trace.WriteLine(String.Format("DEBUG [{0}] @{1} ::: {2}", typeof(T).Name, System.DateTime.Now.ToString(), message));
}
public void DebugFormat(string format, string message)
{
System.Diagnostics.Trace.WriteLine(String.Format(String.Format("DEBUG [{0}] @{1} :::", typeof(T).Name, System.DateTime.Now.ToString()) + format, message));
}
public void Info(string message)
{
System.Diagnostics.Trace.WriteLine(String.Format("INFO [{0}] @{1} ::: {2}", typeof(T).Name, System.DateTime.Now.ToString(), message));
}
public void InfoFormat(string format, string message)
{
System.Diagnostics.Trace.WriteLine(String.Format(String.Format("INFO [{0}] @{1} :::", typeof(T).Name, System.DateTime.Now.ToString()) + format, message));
}
public void Error(string message)
{
System.Diagnostics.Trace.WriteLine(String.Format("ERROR [{0}] @{1} ::: {2}", typeof(T).Name, System.DateTime.Now.ToString(), message));
}
public void ErrorFormat(string format, string message)
{
System.Diagnostics.Trace.WriteLine(String.Format(String.Format("ERROR [{0}] @{1} :::", typeof(T).Name, System.DateTime.Now.ToString()) + format, message));
}
}
InvokeDelegateCommandAction
/// <summary>
/// http://weblogs.asp.net/alexeyzakharov/archive/2010/03/24/silverlight-commands-hacks-passing-eventargs-as-commandparameter-to-delegatecommand-triggered-by-eventtrigger.aspx
/// </summary>
public class InvokeDelegateCommandAction : TriggerAction<DependencyObject>
{
/// <summary>
///
/// </summary>
public static readonly DependencyProperty ParameterProperty =
DependencyProperty.Register("Parameter", typeof(object), typeof(InvokeDelegateCommandAction), null);
/// <summary>
///
/// </summary>
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(InvokeDelegateCommandAction), null);
/// <summary>
///
/// </summary>
public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register(
"InvokeParameter", typeof(object), typeof(InvokeDelegateCommandAction), null);
private string commandName;
/// <summary>
///
/// </summary>
public object InvokeParameter
{
get
{
return this.GetValue(InvokeParameterProperty);
}
set
{
this.SetValue(InvokeParameterProperty, value);
}
}
/// <summary>
///
/// </summary>
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
/// <summary>
///
/// </summary>
public string CommandName
{
get
{
return this.commandName;
}
set
{
if (this.CommandName != value)
{
this.commandName = value;
}
}
}
/// <summary>
///
/// </summary>
public object Parameter
{
get
{
return this.GetValue(ParameterProperty);
}
set
{
this.SetValue(ParameterProperty, value);
}
}
/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
protected override void Invoke(object parameter)
{
this.InvokeParameter = parameter;
if (this.AssociatedObject != null)
{
ICommand command = this.ResolveCommand();
if ((command != null) && command.CanExecute(this.Parameter))
{
command.Execute(this.Parameter);
}
}
}
private ICommand ResolveCommand()
{
ICommand command = null;
if (this.Command != null)
{
return this.Command;
}
var frameworkElement = this.AssociatedObject as FrameworkElement;
if (frameworkElement != null)
{
object dataContext = frameworkElement.DataContext;
if (dataContext != null)
{
PropertyInfo commandPropertyInfo = dataContext
.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(
p =>
typeof(ICommand).IsAssignableFrom(p.PropertyType) &&
string.Equals(p.Name, this.CommandName, StringComparison.Ordinal)
);
if (commandPropertyInfo != null)
{
command = (ICommand)commandPropertyInfo.GetValue(dataContext, null);
}
}
}
return command;
}
}
IDatabase
public interface IDatabase
{
String ConnectionString { get; set; }
Int32 Create<T>(string tableName, T item, params Expression<Func<T, object>>[] ignoreProperties);
List<T> Retrieve<T>(string sql);
Int32 Update<T>(string tableName, T item, params Expression<Func<T, object>>[] ignoreProperties);
Int32 Delete<T>(string tableName, string primaryKey, T key);
void BatchOperation<T>(DataOperation operation, string tableName, IEnumerable<T> list, params Expression<Func<T, object>>[] ignoreProperties);
Int32 RunSP<T>(string SPName, T param, params Expression<Func<T, object>>[] ignoreProperties);
}
public enum DataOperation
{
Create,
Retrieve,
Update,
Delete
}
Converters
public class HorzLineConv : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
TreeViewItem item = (TreeViewItem)value;
ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
int index = ic.ItemContainerGenerator.IndexFromContainer(item);
if ((string)parameter == "left")
{
if (index == 0) // Either left most or single item
return (int)0;
else
return (int)2;
}
else // assume "right"
{
if (index == ic.Items.Count - 1) // Either right most or single item
return (int)0;
else
return (int)2;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
public class VertLineConv : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
TreeViewItem item = (TreeViewItem)value;
ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
int index = ic.ItemContainerGenerator.IndexFromContainer(item);
if ((string)parameter == "top")
{
if (ic is TreeView)
return 0;
else
return 2;
}
else // assume "bottom"
{
if (item.HasItems == false)
return 0;
else
return 2;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
Bootstrapper
浙公网安备 33010602011771号