Sunday, January 30, 2011

Getting the last day of the month...

This isn't complicated C# - but somehow I managed to mess it up... Here's the correct version!

        var theDate = DateTime.UtcNow;
        var daysInMonth = DateTime.DaysInMonth(theDate.Year, theDate.Month);
        var daysLeftThisMonth = daysInMonth - theDate.Day;
        var endOfCurrentMonth = theDate.AddDays(daysLeftThisMonth);

Saturday, January 29, 2011

#DDDHack - broken files...

If you want to enter DDD9 Hack, then the RSS Reader project doesn't build because of differences between the CTP and the RTM.

To get it working:
1.  Delete the old references to Microsoft.Phone.Controls.WebBrowser and friend
2. Add references to the new Microsoft.Phone.* assemblies
3.  Add some capabilities to the contents of Properties/WMAssembly.xml:
    <Capabilities>
      <Capability Name="ID_CAP_GAMERSERVICES" />
      <Capability Name="ID_CAP_IDENTITY_DEVICE" />
      <Capability Name="ID_CAP_IDENTITY_USER" />
      <Capability Name="ID_CAP_LOCATION" />
      <Capability Name="ID_CAP_MEDIALIB" />
      <Capability Name="ID_CAP_MICROPHONE" />
      <Capability Name="ID_CAP_NETWORKING" />
      <Capability Name="ID_CAP_PHONEDIALER" />
      <Capability Name="ID_CAP_PUSH_NOTIFICATION" />
      <Capability Name="ID_CAP_SENSORS" />
      <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />
    </Capabilities>
4. In that same file replace Default Task with  <DefaultTask Name="_default" NavigationPage="MainPage.xaml" />
5. In that same file replace RuntimeType="SilverLight"  with RuntimeType="Silverlight" 
6. In the .xaml files, replace the xmlns:phoneNavigation import with xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
7. In the xaml files, replace the mpc:ListViewItem with a TextBlock - e.g. <TextBlock Text="{Binding Title}"  /> and  <TextBlock Text="{Binding FeedName}" />
8. In the xaml files, replace the xmlns:shell import with xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"     
9. In the app.xaml file, remove the style <Style x:Key="PhoneListBoxItemLayout" TargetType="mpc:ListViewItem"> starting on line 238

That's it....

My attempt at fixes are at http://slodge.com/dddhack/fixes.zip - just apply these over the top of the base project.

Friday, January 21, 2011

Interesting post on the number of public deployments on Azure

Very interesting numbers

Although take all numbers with a "pinch of salt" - these are only "guestimates"

AutoLoading more content when you scroll to the bottom of a ListBox in WP7 (Windows Phone 7)

I've just implemented this for Overflow7.

The approach I took was similar to http://blog.slimcode.com/2010/09/11/detect-when-a-listbox-scrolls-to-its-end-wp7/

However, instead of using a Style I did the hook up in code.

Basically derived my parent UserControl from:

     public class BaseExtendedListUserControl : UserControl
{
   
DependencyProperty ListVerticalOffsetProperty = DependencyProperty.Register(
     
"ListVerticalOffset",
     
typeof(double),
     
typeof(BaseExtendedListUserControl),
     
new PropertyMetadata(new PropertyChangedCallback(OnListVerticalOffsetChanged)));

   
private ScrollViewer _listScrollViewer;

   
protected void EnsureBoundToScrollViewer()
   
{
       
if (_listScrollViewer != null)
           
return;

       
var elements = VisualTreeHelper.FindElementsInHostCoordinates(new Rect(0,0,this.Width, this.Height), this);

        _listScrollViewer
= elements.Where(x => x is ScrollViewer).FirstOrDefault() as ScrollViewer;

       
if (_listScrollViewer == null)
           
return;

       
Binding binding = new Binding();
        binding
.Source = _listScrollViewer;
        binding
.Path = new PropertyPath("VerticalOffset");
        binding
.Mode = BindingMode.OneWay;
       
this.SetBinding(ListVerticalOffsetProperty, binding);
   
}

   
public double ListVerticalOffset
   
{
        get
{ return (double)this.GetValue(ListVerticalOffsetProperty); }
        set
{ this.SetValue(ListVerticalOffsetProperty, value); }
   
}

   
private static void OnListVerticalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
   
{
       
BaseExtendedListUserControl control = obj as BaseExtendedListUserControl;
        control
.OnListVerticalOffsetChanged();
   
}

   
private void OnListVerticalOffsetChanged()
   
{
       
OnListVerticalOffsetChanged(_listScrollViewer);

   
}

   
protected virtual void OnListVerticalOffsetChanged(ScrollViewer s)
   
{
       
// do nothing
   
}
}

this then meant that in the user control itself I could just use:

         protected override void OnListVerticalOffsetChanged(ScrollViewer viewer)
   
{
       
// Trigger when at the end of the viewport
       
if (viewer.VerticalOffset >= viewer.ScrollableHeight)
       
{
                // actually do the load here!
        }
   
}

   
private void ListBox1_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
   
{
       
EnsureBoundToScrollViewer();
   
}

The "hacky" thing here was that I had to use ListBox1_ManipulationCompleted and VisualTreeHelper to find my ScrollViewer - I'm sure there are better ways...