Windows - James Montemagno https://staging.montemagno.com/tag/windows/ https://staging.montemagno.com/content/images/2017/08/MonogramIcon_Orange_NoBuffer.png Windows - James Montemagno https://staging.montemagno.com/tag/windows/ Hugo -- gohugo.io Tue, 13 Oct 2026 23:21:57 GMT 60 Connectivity Plugin 4.0: Important and Awesome Changes https://staging.montemagno.com/connectivity-plugin-4-0-important-and-awesome-changes/ 59b1e003d65cfa0022681a47 Fri, 08 Sep 2017 00:47:05 GMT

The Connectivity Plugin for Xamarin is my second most installed NuGet package with nearly 600,000 installs at time of writing this. It is easy to see why as it gives developers the ability to check connectivity on any platform with a single line of code, subscribe to events, and see what the current connection type is, with a complete cross platform API for iOS, Android, macOS, and Windows.

Connectivity

I mean look at this thing, it is beautiful:

if(CrossConnectivity.Current.IsConnected)
{
    //You are connected to the internet!!!
}

However, there is a feature that isn't used as much that let's developers ping a local or remote host to see if the device can connect to it. This is often good to use when you want to see if your web host or API is up and running before making full network calls to pull down data.

This "Ping" support was implemented a few different ways across iOS, Android, and Windows, which lead to a few rare issues based on how the developers were using the API. This has all changed with the standardization of checking for reachability in version 4.0, which is available in pre-release today, and took me a long time and a lot of code to refactor with Frank Krueger. So much that I even tweeted about it.

API Changes

In the past the API wasn't really clear if developers should use:

Task<bool> IsReachable(string host, int msTimeout = 5000);
or
Task<bool> IsRemoteReachable(string uri, int port = 80, int msTimeout = 5000);

In addition, what is with the weird port and what is with timeout in milliseconds! The API is now much cleaner:

Use:

  • IsReachable: to check a hostname such as a machine name or IP address
  • IsRemoteReachable: to check reachability of a full valid URI

They now take in a bit different parameters:

Task<bool> IsReachable(string host, TimeSpan timeout);
Task<bool> IsRemoteReachable(string uri, TimeSpan timeout);
Task<bool> IsRemoteReachable(Uri uri, TimeSpan timeout);

This means that you can simply add the port into the URI or the string, which will be converted to a URI.

Notice that everything now uses TimeSpan for the timeout :) very nice!

Under the hood upgrades

One of the original problems with IsReachable on iOS/macOS was that there is actually no built in APIs to ping a local IP address or hostname. This is why I asked very nicely for our components team to create a C# binding around Apple's SimplePing and push it up to NuGet so I can consume it!

On Android things are even more tricky as the old code only attempted to use Java.Net.InetAddress to check for reachability.... which actually doesn't work with hostnames. Now, it detects if you passed it an IP Address and checks, else simply just attempts to resolve the actual hostname.

When it comes to IsRemoteReachable each platform was using different mechanisms to do the same thing. This is no good! This is .NET and we have Sockets! Every platform now uses Socket Streams as their core.

Test it today

If you are using the Connectivity Plugin with these methods in your application, I encourage you to test it out today and give feedback on GitHub.

]]>
Cross Platform Photos with Media Plugin https://staging.montemagno.com/cross-platform-photos-with-media-plugin/ 598df8f2c121b60019bf2fe5 Sun, 30 Oct 2016 23:10:30 GMT After months of work, contributions, code reviews, testing, new operating system releases, and tons of awesome new features the brand new Media Plugin for Xamarin and Windows is ready for mass consumption. Not matter how your developing your applications with Xamarin or Xamarin.Forms you can easily have your users take or pick photos and videos from shared code!

Here is the easiest example of taking a photo and displaying it in a Xamarin.Forms image.

takePhoto.Clicked += async (sender, args) =>
{
    await CrossMedia.Current.Initialize();

    var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
    {
        Directory = "Sample",
        Name = "test.jpg"
    });

    if (file == null)
        return;

    image.Source = ImageSource.FromStream(() =>
    {
        var stream = file.GetStream();
        file.Dispose();
        return stream;
    });
};

Improvements

With this release I really focused on performance, memory optimizations, and adding new features that developers have been asking for. The top of the list has been Image Resizing when taking and picking photos. Now, all you need to do is set the PhotoSize in the Media Options:

PhotoSize = PhotoSize.Medium

This will shrink it in half, or you can simply specify a custom resize ratio:

PhotoSize = PhotoSize.Custom, CustomPhotoSize = 90

Better yet, I have also added a new way of compressing the image quality by just setting another parameter when taking or picking a photo:

CompressionQuality = 92

These can really help when displaying photos to users and when you don’t want a huge 2400x4600 photo saved locally. However, you may want to have the original photo saved to the users gallery, and the Media Plugin has you covered! Simply set:

SaveToAlbum = true

And what do you know the original photo will be saved to the camera roll on each device!

There are a bunch of other new features like specifying cropping on iOS and UWP, new permissions support for iOS 10 and Android N (new fancy File Providers), and even overlay support for iOS:

Optimizations

I have spent a lot of time on optimizing the code especially for Android. This means that photos will be automatically rotated based on Exif information, bitmaps are only loaded into memory when needed for continuous photo taking, and a plethora of other bug fixes that have been reported on the GitHub page. This release also has a nice update to the new Permissions Plugin that you can now get your hands on that has some awesome new features.

I am also really excited for all of the new documentation that I wrote up for this release, documenting just about every feature that I could and even including a lot of nice sample apps in the tests folders to check out.

What’s Next

I would  love to reach a new 3.0 release with some new optimizations including custom photo resizing to a max width and height, but would also love to hear what you want, or is it finally everything that you want :) Let me know on the GitHub Page.

]]>