Monthly Archives: January 2015

F# Array Slicing

This should be a short post regarding something I hadn’t seen before – F#’s array slicing syntax. One of the problems of coming to F# from another language is that sometimes you just use language features without realising that the new language offers more options – this is one such feature of arrays in F#.

An array slice is really just a way of creating an array made up of a subset of another array. So for example let’s assume we have the following

let array = [| 1; 2; 3; 4; 5; 6 |]

We can access the array via an indexer, but as you probably know, you access the indexer using dot notation, for example

let v = array.[3]

But we can do more than just get a single item, we use the same basic syntax to get slices of the array, for example

// create a new array of items 2 to 4 inclusive
let a = array.[2..4]

// create a new array of items up to and including item 3 (i.e. 0..3)
let b = array.[..3]

// create a new array of items from item 3 until the end of the array
let c = array.[3..]

// create a clone of all elements
let d = array.[*]

Adding an iOS device via the apple developer portal

I finally got around to looking into Mac & iOS development using Xamarin Studio. I’m sure there will be a series of blog posts on my experiences using the tools but for now, let’s look at the steps required to add a device to your account on the apple developer portal.

We need to add devices to allow us to provision profiles etc. – I’ll be honest, at this time I know very little about the ways of developing iOS apps. so this is definitely a learning process.

Note: These are the manual steps. I read that you can do this from XCode but I couldn’t seem to see the options, if I get that working I’ll create a post on that process, but for now this is the manual process.

  • Firstly, connect the device you want to add to your computer
  • Run iTunes
  • In iTunes click on the device
  • If you’ve not connected the device to the computer you will be asked to trust the computer on the device, obviously do so
  • Click on the serial number so it changes to show the UDID of the device
  • Right mouse click on this and copy the UDID
  • Log into your account on https://developer.apple.com
  • Select Member Center
  • Select Certificates, Identifiers & Profiles
  • Select Devices under the iOS Apps section
  • To the right of the iOS Devices heading, click the + button
  • Put something meaningful to you as the name of the device, i.e. iPad Air or even Mark’s iPad Air or whatever
  • Paste the UDID we copied earlier into the UDID field
  • Click the Continue button
  • You should now see the Review and register page, click Register if you’re happy with the name etc.
  • Finally press the Done button on the Registration complete page and you’re done

You should now see a list of all the iOS devices you’ve registered.

Have you seen the BulletDecorator ?

So I was working on a fancy-ish tooltip (see my previous post). I wanted it to display a bold header and other information in a bullet point fashion underneath. Whilst researching how to do this I came across the BulletDecorator.

There’s so much in WPF, I’m always finding new things or just different ways to do the same thing. This doesn’t really (from what I can see) do anything fantastically clever or the likes, but it’s sort of nice and descriptive in the XAML as to the intent.

So to use the decorator we wrap the control we want to act as the bullet in the Bullet property and the content as a child of the control thus

<BulletDecorator>
   <BulletDecorator.Bullet>
      <Ellipse Height="5" Width="5" Fill="Blue"/>
   </BulletDecorator.Bullet>
   <TextBlock Text="Item1" Margin="3,0,0,0"/>
</BulletDecorator>

Now it’s most likely if you have one bullet point you’ll want several. So an easy way to apply the “style” is as follows.

Create a ControlTemplate in the Resources section of your control (or wherever) that looks something like this

<ControlTemplate x:Key="BulletTemplate" TargetType="{x:Type ContentControl}">
   <BulletDecorator>
      <BulletDecorator.Bullet>
         <Ellipse Height="5" Width="5" Fill="Blue"/>
      </BulletDecorator.Bullet>
      <ContentPresenter Margin="3,0,0,0"/>
   </BulletDecorator>
</ControlTemplate>

Now in your XAML write this instead of the original code

<ContentControl Template="{StaticResource BulletTemplate}">
   <TextBlock Text="Item1" />
</ContentControl>

References

BulletDecorator Class

Creating custom tooltips in WPF

I needed to display a tooltip and wanted it to look a little fancier than the standard one.

First off, here’s how we might create a standard tooltip

<StackPanel Orientation="Horizontal" ToolTip="{Binding ItemName}">
</StackPanel>

Ofcourse we can also expand a ToolTip to allow us to define more interesting content than just the string, like this

<StackPanel Orientation="Horizontal">
   <StackPanel.ToolTip>
      <ToolTip>
         <!-- UI Elements and Bindings -->
      </ToolTip>
   </StackPanel.ToolTip>
</StackPanel>

That’s all there is to it.

Active Patterns in F#

Active patterns have been causing me a few headaches, trying to understand the syntax. Let’s take a look at how we might declare an active pattern.

Note: I will be using the sample from Active Patterns (F#)

So, this is the syntax for declaring an active pattern. The Microsoft documentation states the (| .. |) brackets are known as banana clips. So we declare the types within the banana clips and separated by the | operator, such as

let (|Even|Odd|) input = if input % 2 = 0 then Even else Odd

So you’ll notice that the syntax does not include a function name, but only declares the type that the code can be matched against. Here’s an example of using this code within a pattern match

let test input =
   match input with
   | Even -> printfn "%d is even" input
   | Odd -> printfn "%d is odd" input

Now what happens is – when the function test is called with an input, for example

test 2     // this will output 2 is even

The line below the match tells F# to find the code which matches against a type Even or type Odd and run it. So in this case it will output 2 is even if we had executed the same code with the int 3 Odd would be returned from the active pattern and obvious 3 is odd will be output.

An extremely useful post on how the code can be viewed is on F Sharp Programming/Active Patterns.

To save time I’ll recreate the code here – so the declaration for the active patterns can be viewed as

type oddOrEven =
    | Even
    | Odd
 
let getChoice input = if input % 2 = 0 then Even else Odd

Thread.CurrentPrincipal keeps getting reset in WPF

I’m porting an app. to WPF from Windows Forms. In the Windows Forms application we used to store our own IPrincipal implementation object which stored info. about the user and a security token.

For example

Thread.CurrentPrincipal = 
    new UserPrincipal("username", token);

Whilst porting the code I noticed that the CurrentPrincipal kept getting reset to a GenericPrincipal object.

Long story short, in WPF we need to call the SetThreadPrincipal on the current app domain to set the principal instead of via the CurrentPrincipal property, for example

AppDomain.CurrentDomain.SetThreadPrincipal(
    new UserPrincipal("username", token));