13 Jan

New Years Resolutions 2015

This is my annual post where I publicly declare a few goals for the coming year.  Next year around this time I will take a look back to see how I did.  Looking back at last year I noticed that some of my resolutions didn’t have objective measurements, so I’m trying to include those this year.

Speak More

They say you really learn something when teaching it, and I can speaking forced me to deeply learn the topics I was presenting.  I really enjoyed speaking at my local .Net user’s group as well as Boston Code Camp and Vermont Code Camp.  I’m going to submit sessions to all both code camps, my local user group and at least 2 larger conferences.  All I can do is submit, the decision about whether I speak is out of my hands.

Blog More Consistently

Last year I resolved to blog more, which I did, but I had long stretches without any new posts.  This year I’ve been receiving John Sonmez‘s email course on building a blog.  It’s been an excellent course, and I would highly recommend signing up.    One of the steps in the course is to commit to a blogging schedule.  I’m committing to 2 posts per month.  A much more objective goal than I had last year.  Don’t be surprised if you see 2 posts on the last day of every month :)

Finish My Side Project

A friend and I have been working on a rental property management web application for a while now.  We both started new jobs in the middle of it and we’re both burnt out to the point that we were close to ditching it.  We are down to a handful of tough bugs and a few minor features we want to implement before releasing.  We’ve been on a hiatus for a month now, I’m thinking I’ll take another month hiatus before picking it back up, but I will release it by May 1, 2015 with bugs and missing features if I have to.

Personal Goals

Lose Weight – As I said last year, this one is a bit cliche for a New Year’s resolution, and last year wasn’t a grand success.  I only lost 5 of the 40 I figured I needed to lose.  This year I’m going to target losing 20 of the remaining 35.  I’ll tackle the rest in 2016.

Finish House Projects – I have projects that I started basically as soon as I bought this house 5+ years ago that still haven’t been completed.  These are things I can do myself and I have been loath to hire them out.  I’m either going to do them myself or hire them out this year:

  • Fix the hole in the kitchen ceiling
  • Sheetrock the attic
  • Finish the trim on the second floor
  • Remove the unused oil tank in the basement
  • Paint the house
13 Jan

New Years Resolutions 2014 – Retrospective

It’s time for my annual resolutions blog post, but first I wanted to do a little retrospective to see how I did on last year’s resolutions.

Blog More – I did blogged more, but not as much as I had hoped.  Posts were still less than once per month.

Find more ways to share knowledge – My talk at my local user group went well enough that I submitted and gave talks at the June Boston Code Camp, the Vermont Code Camp, and November Boston Code Camp.  I really enjoyed it, and I’m definitely going to continue.  I tried submitting talks to some bigger conferences but was not accepted.  I’ll keep trying.

X Networking – I got off to a good start, checking in with some people I hadn’t spoken to in a while to see what they were up to, but I didn’t continue it and I didn’t really do anything to expand my network aside from chatting with some people at the code camps.

 Going to Bed Earlier – I mostly nailed this one.

X Lose weight – I did shed 5 pounds, and if I didn’t change anything I think I’d lose another 5 this next year.

So I was 3 for 5 last year.  I would list the year as a success overall.  I made progress, and I’m still heading in the right direction.

28 Oct

Sharing Code with Dependency Injection

This is a multipart series on how to share code between .Net platforms.  All the examples will be showing how to share between Windows 8 Store applications and Windows Phone 8 application, but the techniques are useful for sharing code between any .Net/Xamarin platforms.

  1. Linked Files
  2. Conditional Compilation
  3. Partial Classes
  4. Inheritance
  5. Dependency Injection

What is Dependency Injection?

Dependency Injection is a form of inversion of control where the consumer of a service passes some of the objects on which that service is dependent to the service, rather than having the service create them.

How do I do it?

In this case we’re not going to use a Dependency Injection framework, we’re going to use what I call “the poor mans dependency injection”.  I’m doing it this way to keep it simple and this could still work well with a framework.

First we’re going to create a Portable Class Library and in that library we’re going to define an interface for our data storage methods

public interface IMyStorage
{
    void Save(string key, object value);
    object Get(string key);
}

The we’re going to move our MainePageViewModel class to that library.  We need to modify that class to accept an IMyStorage in it’s constructor, then use that object to get and save it’s data.

public class MainPageViewModel : INotifyPropertyChanged
{
    private IMyStorage _myStorage;
    public MainPageViewModel(IMyStorage myStorage)
    {
        _myStorage = myStorage;
    }

    private string _helloMessage = "Hello World";
    public string HelloMessage
    {
        get { return _helloMessage; }
        set
        {
                
            if (_helloMessage != value)
            {
                _helloMessage = value;
                RaisePropertyChanged("HelloMessage");
            }
        }
    }

    private string _name = "";
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }
    }

    public ICommand SaveAction { get { return new RelayCommand(() => Save()); } }

    public ICommand LoadAction { get { return new RelayCommand(() => Load()); } }

    private void Load()
    {
        object name = null;

        name = _myStorage.Get("Name");

        if (name != null)
        {
            HelloMessage = "Load " + name.ToString();
        }
    }

    private void Save()
    {
        _myStorage.Save("Name", Name);

        HelloMessage = "Save " + Name;
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

In the platform specific projects we need to add a reference to the PCL and create a class that implements IMyStorage. Windows Phone specific code below

class MyStorage : IMyStorage
{
    public void Save(string key, object value)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
        {
            IsolatedStorageSettings.ApplicationSettings[key] = value;
        }
        else
        {
            IsolatedStorageSettings.ApplicationSettings.Add(key, value);
        }
    }

    public object Get(string key)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
        {
            return IsolatedStorageSettings.ApplicationSettings[key];
        }
        return null;
    }
}

Then in the MainPage.xaml.cs where we are setting the page’s datacontext to the MainPageViewModel, we need to make sure we’re passing a new instance of the MyStorage class (this is the actual Dependency Injection, poor man’s style)

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    this.DataContext = new MainPageViewModel(new MyStorage());
}
14 Oct

Sharing Code with Inheritance

This is a multipart series on how to share code between .Net platforms.  All the examples will be showing how to share between Windows 8 Store applications and Windows Phone 8 application, but the techniques are useful for sharing code between any .Net/Xamarin platforms.

  1. Linked Files
  2. Conditional Compilation
  3. Partial Classes
  4. Inheritance
  5. Dependency Injection

What is Inheritance?

I’m just going to go ahead and assume you understand inheritance, but I’m keeping this section to stay with the theme from the other posts in this series.

How do I do it?

To share code in this way we’re going to create an abstract class MainViewModelBase with an abstract method defined for the place where we’re going to do our platform specific coding.  We can share that class between projects either through file linking or creating a Portable Class Library

abstract class MainPageViewModelBase : INotifyPropertyChanged
{
    protected abstract void Load();
    protected abstract void Save();

    //The rest of the common code would go here
    private string _helloMessage = "Hello World";
    public string HelloMessage
    {
        get { return _helloMessage; }
        set
        {
            if (_helloMessage != value)
            {
                _helloMessage = value;
                RaisePropertyChanged("HelloMessage");
            }
        }
    }

    private string _name = "";
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }
    }

    public ICommand SaveAction { get { return new RelayCommand(() => Save()); } }

    public ICommand LoadAction { get { return new RelayCommand(() => Load()); } }

    

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;        
}

Then we’re going to inherit from that class in each of our projects.  Here’s what the code for the Windows Phone project looks like

class MainPageViewModel : MainPageViewModelBase
{
    protected override void Load()
    {
        object name = null;
        if (IsolatedStorageSettings.ApplicationSettings.Contains("Name"))
        {
            name = IsolatedStorageSettings.ApplicationSettings["Name"];
        }
        if (name != null)
        {
            HelloMessage = "Hello " + name.ToString();
        }
    }

    protected override void Save()
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("Name"))
        {
            IsolatedStorageSettings.ApplicationSettings["Name"] = Name;
        }
        else
        {
            IsolatedStorageSettings.ApplicationSettings.Add("Name", Name);
        }
        HelloMessage = "Hello " + Name;
    }
}
01 Oct

Sharing Code with Partial Classes

This is a multipart series on how to share code between .Net platforms.  All the examples will be showing how to share between Windows 8 Store applications and Windows Phone 8 application, but the techniques are useful for sharing code between any .Net/Xamarin platforms.

  1. Linked Files
  2. Conditional Compilation
  3. Partial Classes
  4. Inheritance
  5. Dependency Injection

What are Partial Classes?

Partial Classes are a way to split a class up into multiple files. It is frequently used when one part of your class is generated code and another part is written by hand.  In this case we’ll use partial classes to separate code that is shared between platforms and code that is platform specific.

How do I do it?

First you would need to use linked files to share the code between your projects. Then you need to change the class declaration to include the keyword partial.

partial class MainPageViewModel

Then you create a new files in each of your projects.  In this example we’ll create MainPageViewModel.WP8.cs and MainPageViewModel.W8.cs.  The WP8 file would be created in the Windows Phone project and the W8 file would be created in the Window 8 project.  We then need to include that same class declaration in those files.

Then we just move the methods with platform specific code into those platform specific partial classes.

The WP8 file would look like this:

partial class MainPageViewModel
{
    private void Load()
    {
        object name = null;
        if (IsolatedStorageSettings.ApplicationSettings.Contains("Name"))
        {
            name = IsolatedStorageSettings.ApplicationSettings["Name"];
        }
        if (name != null)
        {
            HelloMessage = "Load " + name.ToString();
        }
    }
}

The W8 file would look like this:

partial class MainPageViewModel
{
    private void Load()
    {
        object name = null;
        if (ApplicationData.Current.LocalSettings.Values.ContainsKey("Name"))
        {
            name = ApplicationData.Current.LocalSettings.Values["Name"];
        }

        if (name != null)
        {
            HelloMessage = "Hello " + name.ToString();
        }
    }
}
27 Aug

Sharing Code with Conditional Compilation

This is a multipart series on how to share code between .Net platforms.  All the examples will be showing how to share between Windows 8 Store applications and Windows Phone 8 application, but the techniques are useful for sharing code between any .Net/Xamarin platforms.

  1. Linked Files
  2. Conditional Compilation
  3. Partial Classes
  4. Inheritance
  5. Dependency Injection

What is Conditional Compilation?

Conditional Compilation tells the compiler to selectively compile code.  Many developers use these to run certain code only while in debug that they don’t want to run in production.

#if DEBUG
    //do some stuff
#endif

How do I do it?

The first step would be to link a file between two projects.  Then you can either use an existing Conditional Compilation Symbol or create your own.  For example Windows Phone 8 defines SILVERLIGHT and WINDOWS_PHONE as Conditional Compilation Symbols.  You see see what symbols have been defined by your project and even add your own by going to the Build tab in Project Properties.
CompilerDirective

To use the Conditional Compilation Symbols you just use the #if statement.  For example if you are using a namespace exists in Windows Phone, but doesn’t exist in Windows 8 you could do this:

#if WINDOWS_PHONE
using System.IO.IsolatedStorage;
#else
using Windows.Storage;
#endif

The System.IO.IsolatedStorage using statement will only be compiled in the Windows Phone projects.  The Windows.Storage using statement will be compiled in all other projects.

15 Aug

Sharing Code with Linked Files

This is a multipart series on how to share code between .Net platforms.  All the examples will be showing how to share between Windows 8 Store applications and Windows Phone 8 application, but the techniques are useful for sharing code between any .Net/Xamarin platforms.

  1. Linked Files
  2. Conditional Compilation
  3. Partial Classes
  4. Inheritance
  5. Dependency Injection

 What are Linked Files?

Linked files are a way for two or more projects to both reference the same file.  One project generally keeps that file under it’s file structure, other projects reference the file there.  You can open the file from any of the projects as you normally would and edit it.  All changes are saved to the one file.

Be Aware

  • You need to manually link the files.  If you have 50 code files you want to share between project, you need to add those 50 files as links individually.
  • If you rename a file that has been shared, you need to delete the old link and add the new link.

How do I do it?

Go through the regular process for adding an existing file, but after you’ve selected the file you need to select “Add as Link” from the dropdown at the bottom.

Link Files Dialog