Shrink File Paths with an Ellipsis in C#

I searched online for a way to shorten a path in .NET. I couldn’t find a solid solution so I had to roll my own. The following method will handle your basic compression needs. It has also been smartened-up to perform some base filename care to reveal the most import characters first. It can handle both Windows and Mac file system structures. It can also properly compact network paths.

The method can also take a custom delimiter of one or more characters. A custom delimiter is nice in cases where an actual ellipsis (the default character) won’t render properly in the UI. Here is what you can expect:

Win Mac Network
…s… …s… …s…
…stripes… …stripes… …stripes…
…stripes.jpg …stripes.jpg …stripes.jpg
…\stripes.jpg …/stripes.jpg …\stripes.jpg
C…\stripes.jpg /…/stripes.jpg \…\stripes.jpg
C:…\stripes.jpg /U…/stripes.jpg \\…\stripes.jpg
C:\Users\chadk\…\stripes.jpg /Users/chadk/Pi…/stripes.jpg \\ps1\Themes\Mi…\stripes.jpg

Read More

Cancel/Abort a Closing Window in Xamarin.Mac

Close Dialog

I began by researching the WillClose event to accomplish this. With it I could detect the window as it was closing but could not find a way to abort with the EventArgs provided. I achieved success with a delegate class that overrides the WindowShouldClose method .
Put this class in the window.cs below the partial class that was pregenerated:

Assign the delegate wherever you instantiate the window controller:

Read More

Center a Window in Xamarin.Mac

To make my window appear front, centered and focused I have had success with the following:

This works exceptionally well when invoked from a NSStatusBarItem.

Read More