Tech Tock

Time is of the essence.

Narcissism–I Wrote the App for That

My Android App is now functional enough to do basically everything I need.  With very few taps, it quickly dates and records statistics and notes that I want to keep track of daily.  The app is called DailyDo, but my sister has dubbed it the narcissism app.  Its actually a self improvement app.  Call it what you like.  Its all function with an interface completely lacking in style, optimized for data entry and navigation speed.

You can download the apk from github here. Or browse the code here.

Here’s a brief tour:

The app opens to the current date and a category group of items that I’m tracking.  For example, in the morning I check my weight (working on that last 5 pounds) and note my departure time:

 

AM Items blank

 

With a single click on the incrementors or decrementors (<< – + >>) the previous value is entered and I only have to click once or twice to update to the current value (things never change as quickly as one would wish). I only support ints and dates, so my weight below is in tenths.

 

AM Items

 

For time values, the “now” is clickable to set the current time.

Click an item row (above – click on empty space in the row) to see its history.  Any entry can also have a note that shows in the history.  Clicking the “D” above opens the note entry form.  And clicking any history row below will open that date so other data can be reviewed.

Weight History2

Of course there’s add items, where you can select and create categories.  But unfortunately, there’s no delete item or delete category… Yet.

Add Items

Advertisements

February 15, 2012 Posted by | Uncategorized | , , , | Leave a comment

WPF MetUp

I had a great time presenting at the .Net Meetup last week.  It was a lively discussion thanks to everyone who showed up.  David Barnhill gave me great support and his extensive knowledge and experience really added depth to the conversation.  Everyone’s feedback and prompting was just great and it was very interactive.

Thanks everybody.

Some of what we covered:

WPF is not the same old system.  Some older ways of doing things in WinForms are just alien to the WPF paradigm.  Extensive use of UI inheritance is out.  Use of templates and Prism is in.

WPF is 4 years old and still early in the adoption cycle.  Paradoxically, this is because its so good.  Its entirely revolutionary (at least compared to WinForms) and so different from what came before that in some ways its like starting over.  This leads to learning curves, staffing and project delivery issues during adoption.

The question is what does it really do for business?  The main thing is that you can do more.  Many things you may not have attempted in WinForms are done simply in WPF.

But how do you get started with WPF?  Same as any new technology I suppose:

  • Read the books.  WPF Unleashed is the top recommendation.
  • The videos on Channel9 are great.
  • There’s more videos from Microsoft here and here.
  • Jason Dolinger’s MVVM video is a classic.
  • Take training.
  • Hire an experienced WPF developer to join the team or bring in a consultant to help out on your first project.  A mentor is invaluable when starting out.
  • You can use WPF simply to start.  Don’t try to use every feature, just get the basics. At its most simple its not so different from WinForms.
  • You can have WPF controls, modules and/or pages in a WinForms app and vice versa so it can come slowly into an organization even with a legacy application.

September 27, 2010 Posted by | Uncategorized | , , , | Leave a comment

INotify Snippet Update

Here’s an update to my INotify snippet.  The only change is that I took out an unnecessary ref from the CheckPropertyChanged signature.  Now I’m hosting the code at google so its easier to keep updated.

protected bool CheckPropertyChanged<T>(string propertyName, ref T oldValue, (no ref here)  T newValue)

September 15, 2010 Posted by | Uncategorized | , , , , | Leave a comment

Oh, The Things You Can Do With WPF

Here’s an animation I made for my upcoming WPF presentation.

You can download the code here, but you might want to wait till after the presentation when it will be finished.

September 13, 2010 Posted by | Uncategorized | , , | 3 Comments

Freakin’ Moq

While I, like many developers,  appreciate Moq immensely, its not perfect.

Here’s an interesting way to freak out Moq 3.1:

It seems that if you use a 2 dimensional array as a parameter (a common scenario in fixed income) Moq will throw an error when accessing the Moq’d object.

This code:

public interface IFreakOutMoq { void FooBar(string[,] Strings); }

var badMock = new Mock<IFreakOutMoq>();
var badMockObj = badMock.Object;   //exception here

Will throw one of these exceptions (I’ve seen both on different systems):

System.TypeLoadException: System.TypeLoadException: Signature of the body and declaration in a method implementation do not match.

System.TypeLoadException: Method ‘FooBar’ does not have an implementation..

A simple solution is to use a Moq friendly object instead of the 2D array:

public class TwoDArray<T> {
private readonly T[,] _array;

public TwoDArray(T[,] array)     {    _array = array;     }

public T this[int x, int y]     {         get { return _array[x, y]; }     }
public T[,] Array     {         get { return _array; }     }   }
}

Here’s a test class that shows a working and failing example:

using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using TestProject;   namespace TestProject {   /// <summary> /// an array implementation that doesn't ///     freak Moq out on MockObj.Obj /// /// if you put a 2d array as a param and call mockedObj.Object /// moq will throw exception: ///     System.TypeLoadException: System.TypeLoadException: ///             Signature of the body and declaration ///             in a method implementation do not match. ///     or  System.TypeLoadException: Method 'FooBar' ///             does not have an implementation.. /// /// this object can be used instead and moq is fine /// </summary> /// <typeparam name="T"></typeparam> public class TwoDArray<T> {
private readonly T[,] _array;       public TwoDArray(T[,] array)     {         _array = array;     }
public T this[int x, int y]     {         get { return _array[x, y]; }     }
public T[,] Array     {         get { return _array; }     }
}   }
/// <summary> /// mocking this interface causes: /// System.TypeLoadException: ///         Method 'FooBar' in type ///         'IFreakOutMoqProxy2dec23fc008646958fc3bae70cbe067b' ///         does not have an implementation.. /// </summary> public interface IFreakOutMoq { void FooBar(string[,] Strings); }     public interface IMoqOK { void FooBar(TwoDArray<string> Strings); }   /// <summary> /// Summary description for UnitTest1 /// </summary> [TestClass] public class UnitTest1 {   [TestMethod] public void TestMethod1() {
var goodMock = new Mock<IMoqOK>();
var mockObj = goodMock.Object;
var badMock = new Mock<IFreakOutMoq>();
var badMockObj = badMock.Object; //exception will be thrown here
Assert.IsNotNull(mockObj);
Assert.IsNotNull(badMockObj);   } }

September 9, 2010 Posted by | Uncategorized | , , , | Leave a comment

An Improved Google Stock Watcher

image I may be late again, now that Google is Ditching Windows, but I finally posted the code for my Google Sidebar Stock Monitor app.

Google made this very nice stock monitoring app for their sidebar, but it had one really annoying bug where saving new settings can delete tickers from your list.  Fortunately, the code was included under an Apache license, so I played around with it a bit and made my own version.

I fixed the annoying bug in Google’s version and added a bunch of features, noted here.  Primarily it allows you to monitor a bracket around the price to see if a stock is approaching a high or low target.

You can download the gg file here.  With Google desktop installed on your system, just click on the gg file to install it to your sidebar.

June 14, 2010 Posted by | Uncategorized | , , , , , | Leave a comment

Focus on Silverlight App At Startup – Problem Solved

I finally figured out the way to set focus to a Silverlight control on start-up:  just set focus to your app, then set the control focus on the dispatcher to give the app time to process whatever it needs to when it gets focus.  In other words, in the root visual constructor use this code:

HtmlPage.Plugin.Focus();
Dispatcher.BeginInvoke(() => YourControl.Focus());

This may be the first (and probably the last) actual Silverlight mystery I have solved.  A couple months ago, I was searching the internet for the way to do this and found absolutely nothing.

Yippee-Kai-Yay for me 😉

April 20, 2010 Posted by | Uncategorized | , , | 3 Comments

Visual Studio INotifyPropertyChanged Snippet

I just made my first Visual Studio snippet.  It’s really easy.

There’s a consensus developing that says dependency properties are usually too heavyweight and people are using INotifyPropertyChanged instead.  I miss the snippet that creates a dependency property, so I made a similar snippet for INotifyPropertyChanged.  You can download the code here.

To make a snippet, just go to Tools / Code Snippets Manager and find a snippet to start with.  I started with the “Define a Dependency Property” snippet since I was doing almost the same thing.  The Snippets Manager will tell you where that file is:

image

Go to the file system and copy the file to your My Code Snippets folder.  If you highlight the folder in the Code Snippet Manager, it will tell you where it is — you can copy the path from the location field.  The file is an xml file, but needs to be named “.snippet”.

Editing the file is very straightforward and the snippet will be available immediately – no restart needed.

One interesting thing about the code is that I’m using a simple method to do all the work:

I basically combined this CodeProject work with Jeff Yates object.Equals idea from here in order to handle both reference and value types in a single call:

Here’s how the snippet comes out by default (the myNames and type are the replaceable fields):

int _myField;

public int MyProperty
{
    get { return _myField; }
    set
    {
        CheckPropertyChanged("MyProperty",
            ref _myField, ref value);
    }
}

This is the method that does a check and notify in the base class:

protected bool CheckPropertyChanged<T>
      (string propertyName, ref T oldValue, ref T newValue)
      {
            if (!Equals(oldValue, newValue))
            {
                oldValue = newValue;
                RaisePropertyChanged(propertyName);
                return true;
            }
            return false;
        }

February 11, 2010 Posted by | Uncategorized | , , , , , | 3 Comments

4 C# Things You Might Not Have Heard Of

Partial Interfaces

Just learned that Interfaces can be partial.  Not sure how I’ll use this info but it is interesting.

This compiles:

namespace NameSpace
{
    partial interface IInterface
    {
    }
    partial interface IInterface
    {
    }
}

Partial Methods

Some folks were unaware that methods can be partial.  But I’ve seen this in my favorite DAL for quite a while.  Its seems most useful in (usually autogenerated) shared code situations as a replacement for virtual functions.  Code calling a partial method is removed from the compiled binary if the method isn’t implemented.  In that way it seems somewhat similar to events as well.

        partial void OnMyMethod();
        private MyMethod()
        {
            OnMymethod();
        }

If OnMyMethod were virtual or an event call, the functionality would be very similar.

Assignment Is A Value

The return value of an assignment expression is the assigned value itself.

            //set both x and y = 5
            int x = (y = 5);

I saw this feature years ago in C/C++ but never thought of using it in C#.

Coalesce

Here’s a use for the return value of assignment with the C# null coalescing operator (another little known feature – coalesce will return the first non null value).  This compresses the code for a common operation while (some would argue) not losing clarity.

private object _aProperty;
public object AProperty
{
    get { return _aProperty ??
            (_aProperty = new object()); }
}

January 23, 2010 Posted by | Uncategorized | , , | 3 Comments

Linq.Debug.Print

Here’s a great explanation of System.Interactive.

I really liked this part about being able to do debug.print within a multipart Linq Statement:

var res = GetRandomNumbers(100)
.Take(10) .Do(x => Console.WriteLine(“Source -> {0}”, x))
.Where(x => x % 2 == 0)
.Do(x => Console.WriteLine(“Where -> {0}”, x))
.OrderBy(x => x)
.Do(x => Console.WriteLine(“OrderBy -> {0}”, x))
.Select(x => x + 1)
.Do(x => Console.WriteLine(“Select -> {0}”, x));

The below shows what’s triggered by the call to Run:

Source  -> 96
Where   -> 96
Source  -> 25
Source  -> 8
Where   -> 8
Source  -> 79
Source  -> 25
Source  -> 3
Source  -> 36
Where   -> 36
Source  -> 51
Source  -> 53
Source  -> 81
OrderBy -> 8
Select  -> 9
9
OrderBy -> 36
Select  -> 37
37
OrderBy -> 96
Select  -> 97
97

January 12, 2010 Posted by | Uncategorized | , , | 1 Comment