“…with great pleasure, comes great responsibility.”
The proper way to serialize and deserialize DateTimeOffset in XML is to use the DataContractSerializer instead of the standard XmlSerializer. The reason is that the XMLSerializer does not support certain aspects of the DateTimeOffset structure (don’t ask me what they are, but that’s what Microsoft states is their reasoning for not supporting it). I tend to try and code using generics whenever possible, especially when it’s stuff like this.
Thus, here’s an example of how to serialize any class using it (I’m using UTF8 encoding here, but you can use whatever you like):
public static string SerializeObject<T>(T instance) where T : class
{
try
{
MemoryStream stream = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(stream, instance);
return new UTF8Encoding().GetString(stream.ToArray());
}
catch(Exception ex)
{
...
}
}
And here’s how to deserialize it again:
public static T DeserializeObject<T>(string data) where T : class
{
try
{
MemoryStream memoryStream = new MemoryStream(new UTF8Encoding().GetBytes(data));
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(memoryStream);
}
catch
{
...
}
}
There are still modules out there using the old setting style for connection strings:
<appSettings>
<add key="SiteSqlServer" value="Server=(local);Database=DNN;Integrated Security=True" />
</appSettings>
I spent the better part of a day and a half trying to diagnose why all the modules from one particular 3rd party vendor wouldn’t work on my box… lesson learned.
After receiving a promising resume for a job request, I scheduled an interview with the candidate. The interview itself was fairly straightforward, having all the standard questions about .NET, C# and SQL Server.
The candidate seemed fairly knowledgeable although I would probably get them to do a technical interview if we decided to move forward.
I thanked the individual for coming in, and asked them to forward me 2 or 3 references.
I was somewhat taken aback by their response. “I prefer to get my jobs on my own. I don’t like to bother my former employers with those details.”
I explained to them that we require references to verify their employment, skills and experience. Again, they responded with “I prefer to get my jobs on my own.”
I wasn’t exactly sure what to say, so I simply told them that it’s standard practice and that we would not be able to consider them for a position without references. Begrudgingly, I was told that they’d be sending an email with some references.
Needless to say, I never received a follow-up email.
I’ve worked as both an employee and as a director, so I know how important it is to be sensitive to people’s need to take time off and decompress. Vacations are an inconvenience of course, because they impact project timelines, but booked far enough in advance and approved prior to scheduling and incurring travel expenses will ensure that the impact is minimized.
That is, until you’ve met a certain character whom I had in my employ. As a senior software developer, in charge of a key component to our system, and like everyone else in the company, this individual was expected to book their vacation well in advance (3 months or more).
I received a vacation request email from the individual around the end of April. The request was for the first 18 business days of September off, as this individual and their family would be flying to Europe.
I had no problem with the request, and after checking with their PM, I sent an approval email. I also cc:’d HR and entered the time into our timesheet system.
Now, fast forward a couple of months.
It’s now mid-August, and this individual had been having some troubles on a particular project they were working on. Things were definately NOT going well, they were well behind and burning through our budget like there was no tomorrow.
It was a Tuesday afternoon, and we’d just finished having a management meeting to discussed the situation. We felt that if he remained heads down for the next 2 weeks, there was a good possibility we’d be able to meet the required deadline and this individual could enjoy their vacation knowing things were well in hand.
That is, until about 2:30pm, when this individual stopped by my desk to remind me that they were starting their vacation at the end of the day.
At first I was rather puzzled. I remarked that I understood this individual to be taking vacation at the start of September. The response was, “No, I asked to start my vacation end of day today.” Considering the circumstances, I was taken a back somewhat. I reiterated my previous statement and assured them that I had approved the dates, and added them to the timesheet system.
They looked at me rather blank faced for a moment, then said, “Well I’ve already booked the tickets for my family and we leave tomorrow morning so….”
I told them I would have to review things and would get back to them shortly. As they went back to their desk, I quickly reviewed my emails, and sure enough, the dates I approved were for the beginning of September for 18 days.
I discussed the matter with both our Senior PM and the department head, both of whom were take aback somewhat. In the midst of this, the individual came back over and informed us that they’d gotten their dates mixed up and was it all right to take the time off starting end of day today instead.
We discussed the matter, but we pretty much knew we couldn’t simply deny this individual the timeoff considering the situation, so the decision was made to adjust the schedule and allow the individual to take the time off.
Fast forward 18 business days.
Monday morning comes around, and this individual is nowhere to be found. I checked my email, and there’s a message saying “Hi, enjoying my vacation, wow this has been great.” Our plane will be leaving this evening at such and such a time, I’ll see you Tuesday. Funny, I don’t remember approving a 19th vacation day off.
So Tuesday morning comes around, and again, this individual is nowhere to be found. All day, no emails, no phone calls, nothing. Funny, I don’t recall approving a 20th vacation day off either.
So now we’re on to Wednesday… you keeping up? Around noon, I get an email: “Hey there, we’re in Seattle and I’ll be in tomorrow.” Funny thing was, the email message didn’t come from this person’s private email account like it had previously, instead it came from their company account. We didn’t have webmail, and the only way it’s accessible from the outside is through our VPN… hmmm. Well, maybe the person brought their laptop to Europe with them, or maybe they were already at home?
Okay, this is no longer funny, I definately did NOT approve a 21st vacation day off. This individual finally made it into work on Thursday. Four full weeks (plus a day) since they last graced us with their presence. Needless to say, their tenure at our organization did not last too many weeks longer.
So it’s been a while since I posted last, but recently I had the opportunity to do some serious XAML binding stuff, which I thought I’d share with you. If you’ve ever wanted an easy way to create an editable gridview inside a listview, it can seem like a pretty daunting task, but actually, it’s really not that tough. You can setup a binding scenario that will do 80% of the work for you. Try the following out for size: First, the XAML code:
<Window x:Class="EditableGridViewExample.Window1"
xmlns:local="clr-namespace:EditableGridViewExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Window.Resources>
<local:BoolToVisibilityConverter x:Key="b2v" />
<Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyle">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource b2v}" ConverterParameter="False" >
<Binding ElementName="EditModeCheckBox" Path="IsChecked" />
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}" Path="IsSelected" />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyle">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource b2v}" ConverterParameter="True" >
<Binding ElementName="EditModeCheckBox" Path="IsChecked" />
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}" Path="IsSelected" />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CheckBox x:Name="EditModeCheckBox" Content="Edit Mode" />
<ListView Grid.Row="1" x:Name="WindowListView" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumnHeader Tag="ColumnA" Content="Object" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=ColumnA}" Style="{StaticResource TextBlockStyle}" />
<TextBox Text="{Binding Path=ColumnA}" Style="{StaticResource TextBoxStyle}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumnHeader Tag="ColumnB" Content="Type" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=ColumnB}" Style="{StaticResource TextBlockStyle}"/>
<TextBox Text="{Binding Path=ColumnB}" Style="{StaticResource TextBoxStyle}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
Now, the codebehind (I put everything in one file to make it easy to use):
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
namespace EditableGridViewExample
{
public class ListItems : ObservableCollection<ListData>
{
/// <summary>
/// Initializes a new instance of the <see cref="ListItems"/> class.
/// </summary>
public ListItems()
{
this.Add(new ListData() { ColumnA = "apple", ColumnB = "fruit" });
this.Add(new ListData() { ColumnA = "jaguar", ColumnB = "animal" });
this.Add(new ListData() { ColumnA = "lullaby", ColumnB = "music" });
this.Add(new ListData() { ColumnA = "monkey", ColumnB = "animal" });
this.Add(new ListData() { ColumnA = "orange", ColumnB = "fruit" });
this.Add(new ListData() { ColumnA = "whale", ColumnB = "mammal" });
this.Add(new ListData() { ColumnA = "coathanger", ColumnB = "other" });
}
}
public class BoolToVisibilityConverter : IMultiValueConverter
{
/// <summary>
/// Converts source values to a value for the binding target. The data binding engine calls this method when it propagates the values from source bindings to the binding target.
/// </summary>
/// <param name="values">The array of values that the source bindings in the <see cref="T:System.Windows.Data.MultiBinding"/> produces. The value <see cref="F:System.Windows.DependencyProperty.UnsetValue"/> indicates that the source binding has no value to provide for conversion.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// A converted value.If the method returns null, the valid null value is used.A return value of <see cref="T:System.Windows.DependencyProperty"/>.<see cref="F:System.Windows.DependencyProperty.UnsetValue"/> indicates that the converter did not produce a value, and that the binding will use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"/> if it is available, or else will use the default value.A return value of <see cref="T:System.Windows.Data.Binding"/>.<see cref="F:System.Windows.Data.Binding.DoNothing"/> indicates that the binding does not transfer the value or use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"/> or the default value.
/// </returns>
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool param = bool.Parse(parameter as string);
bool combined = true;
foreach (bool val in values) combined &= val;
return combined == param ? Visibility.Visible : Visibility.Hidden;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class ListData : DependencyObject
{
public static readonly DependencyProperty ColumnAProperty =
DependencyProperty.Register("ColumnAProperty", typeof(string),
typeof(ListData), new UIPropertyMetadata(null));
public static readonly DependencyProperty ColumnBProperty =
DependencyProperty.Register("ColumnBProperty", typeof(string),
typeof(ListData), new UIPropertyMetadata(null));
/// <summary>
/// Gets or sets the column A.
/// </summary>
/// <value>
/// The column A.
/// </value>
public string ColumnA
{
get { return (string)GetValue(ColumnAProperty); }
set { SetValue(ColumnAProperty, value); }
}
/// <summary>
/// Gets or sets the column B.
/// </summary>
/// <value>
/// The column B.
/// </value>
public string ColumnB
{
get { return (string)GetValue(ColumnBProperty); }
set { SetValue(ColumnBProperty, value); }
}
}
public partial class Window1 : Window
{
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
public Window1()
{
InitializeComponent();
// bind datacontext of the list view to a new listitems observable collection
this.WindowListView.DataContext = new ListItems();
}
}
}
Oh, and if you’re wondering how I got it to format so nicely, I’m using this the following blogspot source code formatting tool.
I live in the Western Communities on Vancouver Island, in British Columbia. I recently heard this story and I think it’s a great argument why public services like search and rescue should charge for rescues if the person(s) being rescued break the law:
The story starts with a plane crash landing on the beach at the Carmanah Point Lighthouse back in February. The pilot and one passenger, were flying his little plane (apparently something he built himself from a hobbyist kit) when they crash landed on the beach. After climbing to the lighthouse to seek help, the Canadian Armed Forces sent out a helicopter which picked them up and took them to Victoria.
Two days later, the pilot, along with his two brothers, headed back to the plane. Apparently they planned to repair the plane and fly it out before the wind and ocean tides tore it apart. The trek was roughly 16km and the terrain made it extremely difficult to navigate. The three lugged in their tools, a gun for protection (from bears and cougars I imagine), and a replacement propeller.
You ready for this? Guess what, they got lost. When they didn’t show up at the lighthouse, they were reported missing and a search began. They eventually made it to the lighthouse, but were not allowed to fly the plane out. The coast guard flew them out by helicopter the next day.
The crown has charged the pilot with unlawfully landing an aircraft in a park and for firearms possession. Additionally, both he and his two brothers, are also charged with trespassing in a closed area.
Uh, hold on a minute, why don’t they make them pay the cost of BOTH rescues? It costs the taxpayers money.
As a footnote, from what I understand, the pilot did manage to get a helicopter to hoist the plane off the beach so it could be salvaged.
…and in case you’re curious where this happened, here’s the location of Carmanah Point Lighthouse:
Here’s a handy piece of code for finding a style in the ESRI Style Gallery. You simply pass in the style and the category and it returns the appropriate object (if it exists):
public static object FindEsriWidget(string style, string category)
{
try
{
// style comparision is case insensitive, without leading/trailing spaces
string normalizedStyle = style == null ? "" : style.Trim().ToUpper();
// these are our interfaces and coclasses
IStyleGallery gallery = new StyleGalleryClass();
IStyleGalleryItem item = new StyleGalleryItemClass();
IStyleGalleryStorage storage = (IStyleGalleryStorage)gallery;
IEnumStyleGalleryItem list = null;
// get the gallery storage item
string path = storage.DefaultStylePath + "ESRI.Style";
// what we're interested in
list = gallery.get_Items(category == null ? "" : category, path, null);
// reset our cursor
list.Reset();
// enumerate the list as required
for (item = list.Next(); item != null; item = list.Next())
{
// if we find a match, use it
if (item.Name.Trim().ToUpper().Equals(normalizedStyle))
{
return item.Item;
}
}
}
catch (Exception ex)
{
// do something here
}
// not found
return null;
}
So here a simple way to find a point’s intersect along a polyline. The code includes the ability to include buffer distance. So if the point doesn’t actually intersect, but is within X meters it will also return intersection for the point. Very handy little piece of code. Enjoy.
public static double GetPointCollectionIntersectIndex(
IPointCollection points, IPoint point, double? bufferDistanceInMeters)
{
object Missing = Type.Missing;
PointClass queryPoint = new PointClass();
Polyline testPolyline = new Polyline();
// first see if point is in collection
for (int i = 0; i < points.PointCount; i++)
{
// get the point in the collection
points.QueryPoint(i, queryPoint as IPoint);
// we've found our point along the polyline
if (queryPoint.Compare(point) == 0) return i;
}
// time to do some intersect checks
for (int i = 1; i < points.PointCount; i++)
{
// remove old points if reusing
if (testPolyline.PointCount > 0) testPolyline.RemovePoints(0, 2);
// create first point
points.QueryPoint(i - 1, queryPoint as IPoint);
testPolyline.AddPoint(queryPoint as IPoint, ref Missing, ref Missing);
// create second point
points.QueryPoint(i, queryPoint as IPoint);
testPolyline.AddPoint(queryPoint as IPoint, ref Missing, ref Missing);
// get the relation operator for the target geometry and topological operator for buffering
ITopologicalOperator bufferOperator = testPolyline as ITopologicalOperator;
// get the row cursor
IRelationalOperator relationOperator =
(bufferDistanceInMeters == null ?
(IGeometry)testPolyline :
bufferOperator.Buffer((double)bufferDistanceInMeters)) as IRelationalOperator;
// a disjoint indicates a non-intersection, so if it's false, we have our intersect point
if (relationOperator.Disjoint(point) == false) return ((double)i) - 0.5;
}
// return -1
return result;
}
FYI, if you happen to be in the market for someone with over 21 years experience in Microsoft related technologies, I’m available on contract as a consultant. Just send me a message through LinkedIn (http://www.linkedin.com/pub/ted-neustaedter/7/a1a/761), mention me in a twitter post @chessknught or email me through one of my webites: http://www.ProductLair.com (at the bottom) or http://www.slow-pc.com.
I will ALWAYS respond to every inquiry, regardless of whether I’m interested or not, it’s just polite to do so. So if I don’t get back to you within 24 hrs, try again ‘cuz sometimes I miss stuff.
I don’t want my email posted here ‘cuz I get tonnes of spam daily and it drives me crazy.
