Ubuntu Mate on Raspberry Pi

I had some trouble getting PINN, a version of Noobs that is supposed to be able to install Ubuntu, to work with my 64GB SD Card.  I ended up just burning an Ubuntu image to the card.

Setting up SD Card

  1. Download Ubuntu Mate 16.05.1 LTS image (ubuntu-mate-16.04.2-desktop-armhf-raspberry-pi.img) from https://ubuntu-mate.org/raspberry-pi/
  2. Download and install Etcher if necessary from etcher.io
  3. Use Etcher to burn the image onto the SD card
  4. Boot the Pi from the card and run through the initial setup

Initial Configuration

  1. Enable SSH
    1. Open a terminal
    2. $ sudo apt-get update
    3. $ sudo apt-get install openssl-server
    4. $ sudo ufw allow 22
    5. $ sudo service ssh restart
  2. The ssh service will now be running but it does not start when you reboot. Beware: googling for this will bring up many answers that are no longer valid.  I found the right answer (as well as many wrong answers) at https://ubuntu-mate.community/t/automatically-start-ssh-server-on-boot/11971/15
    1. $sudo systemctl enable ssh.service
  3. Installing ssh appears to also set up sftp as well.  You can now use ssh and sftp to manage the Pi
  4. Install TightVNC Server from https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-16-04
    1. $ sudo apt-get install xfce4 xfce4-goodies tightvncserver
    2. $ vncserver (you will be prompted for a password – I used raspberry, which is too long but will work ok)
    3. Configure VNC
      1. $ vncserver -kill :1
      2. $ cd ~/.vnc
      3. $ cp xstartup xstartup.save
      4. $ vi xstartup – replace what is there with the following

        #!/bin/bash
        xrdb $HOME/.Xresources
        startxfce4 &

      5. $ sudo chmod +x xstartup
      6. $ vncserver
  5. Configure your favorite VNC client – use port 5901 instead of the default 5900.  When you open a session it will prompt for a password – use the one from step

 

Parallels vs. VMWare

I use both Parallels and VMWare daily on a variety of macs.  I prefer Parallels because of its graphics performance, which I think is a little snappier.  However, there is a major problem with Parallels that has caused me to switch to VMWare : you have to buy separate licenses for each mac that you run it on.  VMWare uses standard Apple-style licensing, allowing you to install on all the computers you own.

From the VMWare blog:

Starting with VMware Fusion 4, personal users only need one license to install on all the Macs you own. If you’re a business or educational institution you’ll need one license for each copy of VMware Fusion. The relevant excerpt from the VMware Fusion 4.0 license agreement says:

You may install and use the Software for personal, non-commercial use on any Apple-branded products running Mac OS X (“Mac Computer”) that you own or control.

(http://blogs.vmware.com/teamfusion/2011/09/got-more-than-one-mac-weve-got-you-covered.html)

From Parallels:

In accordance with the End User License Agreement terms and conditions, a Parallels Desktop for Mac retail license can only be used for activation of Parallels Desktop for Mac on a single Mac machine.

Note: In order to use Parallels Desktop for Mac on several Mac machines, you need to purchase the corresponding number of licenses.

(http://kb.parallels.com/en/112971)

ListView Binding in WPF

Although WPF is pretty old now and doesn’t seem to get much support these days, it is still my preferred framework for Windows desktop applications.  One thing that I seem to do frequently in WPF is to display data in grids.  The ListView works great for this and it is easy to bind data in your C# code to it.

For a really good description of this process, see http://tech.pro/tutorial/742/wpf-tutorial-using-the-listview-part-1.

First off you need a class to hold the data you want to show in the ListView.

public class Contact
{
    public String Name { get; set; }
    public String Address { get; set; }
    public String Phone { get; set; }
}

Then, in your main window code, define an ObservableCollection of your class objects as a member variable, and expose that collection in a public property.

private ObservableCollection<Contact> m_contacts = new ObservableCollection<Contact>();  

public ObservableCollection<Contact> Contacts { get { return m_contacts; } }

In your XAML, define a ListView with ItemsSource set to the name of your collection property and bind up the column values of the ListView down inside its GridView.  Add a DataContext to your Window definition to tell the ListView where to find the ItemsSource.

<Window x:Class="ListViewTest.Test2.ListViewTest"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   DataContext="{Binding RelativeSource={RelativeSource Self}}"
   Title="Contacts" Height="216" Width="435">
  <ListView ItemsSource="{Binding Contacts}">              
    <ListView.View>
      <GridView>
        <GridViewColumn Width="140" 
                        Header="Name" 
                        DisplayMemberBinding="{Binding Name}"/>
        <GridViewColumn Width="140" 
                        Header="Address" 
                        DisplayMemberBinding="{Binding Address}"/>
        <GridViewColumn Width="140" 
                        Header="Phone #"
                        DisplayMemberBinding="{Binding Phone}" />
      </GridView>
    </ListView.View>
  </ListView>
</Window>

Whenever you add or delete items to your m_contacts collection, the ListView will be updated automatically.

 

MVC 4 Custom Html Helpers

One of the most useful features of MVC is that it gives developers the ability to create custom helper functions.  You are probably using helpers in your cshtml code already, such as the ActionLink helper for generating links to pages within your site, e.g. @Html.ActionLink(“Home”, “Index”, “Home”).  By using extension methods for the Html class, you can create your own Html helper functions and invoke them with the same Razor syntax as ActionLink.

When I first looked into how to do this, I found many articles out there about it but they weren’t very consistent because the implementation of custom helpers has changed over time.  It turns out that this is very simple to do.  Here’s a step-by-step recipe.

In your MVC 4 project, add a plain folder (not an ASP.NET folder) called HtmlHelpers and add a class within that folder called MyHelpers.  Add a method to your class that you want to invoke in your razor code.  Here is an example of a method to display some bold text.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Mvc;

namespace MyProject.HtmlHelpers
{
    public static class MyHelpers
    {
        public static MvcHtmlString MakeBold(this HtmlHelper helper, string text)
        {
            return new MvcHtmlString("<b>" + text + "</b>");
        }
    }
}

Note the first argument to the MakeBold function.  The “this HtmlHelper” syntax marks this method as an extension method for the HtmlHelper class.  Both the class and the method must be static.  Also note that the method return an MvcHtmlString.  This causes the razor engine insert the string without encoding – if you just returned a string you would see the HTML code to make the string argument bold rather than a bolded string.

To use this method, you have to make it visible in your cshtml files.  You can do this by adding a namespace reference your helpers (MyProject.HtmlHelpers) in the web.config file inside you View folder, but I found that approach did not give me intellisense prompts for my helpers.  Including an explicit reference in my cshtml did, e.g. “@using MyProject.HtmlHelpers” at the top of my cshtml file did.

That’s it.  Here is some razor code that uses the helper.

@using MyProject.HtmlHelpers

@{
    ViewBag.Title = "Dashboard";
}

<h2>@Html.MakeBold("Dashboard")</h2>

Here is a link to an article that best explained this process for me.

MVC-4 Simple Membership Walkthrough

This post will walk through the process of creating an MVC-4 web application that uses SimpleMembership with an existing database.  The objective is to have a web site with the following properties.

  • Uses SimpleMembership user authentication.
  • Integrates the ASPNETDB tables into our existing SQL Server database.

Open Visual Studio 2012 and create a new project.  Select the MVC-4 template under C# project type.  You will be presented with a number of possible templates.  The Internet Application template includes all the parts we need, including an Account controller with views for logging in and registering new users.

Select a project name and build your initial project skeleton.

Next we want to specify our existing database as the location for user account management tables.  The Account controller will create these tables for us if they do not already exist when we first run the application.

Open your web.config and look for the <add> element for DefaultConnection element under <connectionStrings>.  First we want to change the definition to use our existing database on SQL Server.  Set the database providerName to  “System.Data.SqlClient”, the data source to “(local)”, set the Initial Catalog to our database, and set your database login values.  I am using “Integrated Security=true” to use my Windows login credentials for logging in to the database. These settings ought to get you up and running, but just to explore these settings a bit more, set the name of your connection string to something more informative – I am using “PetesDatabaseConnection”  – see below.

    <add name="PetesDatabaseConnection"
         connectionString="Data Source=(local);Initial Catalog=PetesDatabase;Integrated Security=true;"
         providerName="System.Data.SqlClient" />

Build your project and run it.  You will generate an exception because the Account management code can’t find DefaultSettings in your web config.  This happens down in an obscure bit of filter code,  the InitializeSimpleMembershipAttribute class.  Look a little further down in this file and you will see a SimpleMembershipInitializer class with a call to InitializeDatabaseConnection.  This bit of code is looking for a connection string named DefaultConnection.  Change that to the name of your connection string and rebuild and run.  You should be able to register your login and password now and if you look at your database after registering you will see the new tables that have been added.