The Growth of #Xamarin – An independent metric

download.pngI’ve been using #Xamarin for iPhone and Android app since it was pronounced “Monotouch”. I may have been one of the first commercial users. It’s a great platform, but is it growing? Here’s an interesting and independent metric.

My Xamarin blog’s visitors trend is below. I don’t advertise, I just write articles on things I find interesting. Then let Google and the internet decide if it’s of value.

Note: I wrote this at the start of Feb, 2019 hence the low last value.

Screen Shot 2019-02-03 at 12.07.36 PM.png

Advertisements

VSTS Unit Testing for ASP.NET Core

download.pngUp until recently setting up VSTS for .Net Core unit testing was a bit tricky. In this simple post I’ll show you have you can enable it using the new .NET Core Task.

Step 1: Add .NET Core Task

2018-08-10 08_40_31-Gateway - Visual Studio Team Services.png

Step 2: Click the Task, then change Command to test

2018-08-10 08_41_13-Gateway - Visual Studio Team Services.png

Step 3: Add your projects to be tested

2018-08-10 08_41_42-Gateway - Visual Studio Team Services.png

Live Reload from Visual Studio 2017 for Windows in WMWare Fusion on a Mac – Xamarin

main-qimg-2f9592a9947bee2a14921ac6cadfe091If you’re one of those people with a Mac but LOVE Visual Studio for Windows, here’s how you can set up Live Reload to work.

As you may know the Android Emulator can’t run in VMWare fusion, so you need to connect to it. Here’s how.

First install the Visual Studio for Windows Live Reload Extention and the Nuget package into your app as per instructions on the Live Reload site.

Here are the tweeks needed after that.

In your App.xaml.cs add this line at the top, DO NOT add any MQTT settings:

public partial class App : Application
 {
   public App()
    {
#if DEBUG
       LiveReload.Init();
#endif


Open Tools – Options then add the Hostname and unselect the checkbox as in the picture below.
live

Rebuild and redeploy the app to Android and iOS.

Now you can edit XAML and see changes live in both at the same time (mind blowen)

Using Android Emulator from Windows in WMWare Fusion on a Mac – Xamarin

download.jpgYou can’t run Android in a VM so if you are running Windows on a Mac using VMWare Fusion, you’ll need to connect to the Android running on the Mac. More detail

Once you have installed Visual Studio for Mac, open it and run the emulator for Android.

On the Mac

Open a Terminal on your Mac and enter this to add the Android platform tools into the path:

echo ‘export PATH=$PATH:~/Library/Developer/Xamarin/android-sdk-macosx/platform-tools/’ >> ~/.bash_profile
source ~/.bash_profile
adb devices

If you get a permission error on the bash_profile, run this:

sudo chown YOURACCOUNTNAME ~/.bash_profile

You should see a nice list of the emulators running. That will show you that you have completed the first step. You do not need to ever run this again.

Screen Shot 2018-07-05 at 9.03.31 AM.png

Open TextEdit and add this as a text file:

#!/bin/bash
echo ‘Starting android pipe listener’
cd /tmp
mkfifo backpipe
nc -kl 5555 0<backpipe | nc 127.0.0.1 5555 > backpipe

In TextEdit go to the Format menu and choose Make Plain Text

In TexEdit save the file to your desktop but without the .txt extension. Note that we unchecked the .txt extension

Screen Shot 2018-07-05 at 9.20.27 AM.png

We now need to tell OSX that the file is an executable. Open Terminal and use the following command and path (yours will be a bit different than mine)

chmod 700 /Users/<your account>/Desktop/adb

Now we can run it by double clicking the adb file. This should will run in the background always. so many sure it’s run when you want to connect to Android from the Windows VM

Screen Shot 2018-07-05 at 10.41.07 AM.png

Screen Shot 2018-07-05 at 9.25.24 AM.png

On the PC

Now that you are running the bash script on the Mac, it’s ready to be connected to.

Add a Windows Batch file on your desktop there with the following. Run it one to have the connection set up. It will appear then disappear. If you loose connection, then run it again.

cd C:\Program Files (x86)\Android\android-sdk\platform-tools
adb.exe connect <IP address of the Mac, get it from the Mac Settings app-> network>

You’ll see the Android device in your Visual Studio:

Screen Shot 2018-07-05 at 11.05.24 AM.png

Enjoy!

Problems?

If when you use the adb devices command on the PC, you get an “unauthorized” go check the running android and ensure it isn’t asking for permission. If it is, grant it then run both script on the Mac and PC to re-establish connection.

Kendo Controls in an AngularJS ng-repeat

Mark_verticalKendo has long been an awesome JavaScript framework for UI controls. I had to build a user management system and needed to dynamically show a number of Kendo controls, all created via an AngularJS ng-repeat.

Kendo is smart enough to know that the item in the repeat, a user in this case, is the bound object and will keep a reference to it.

<div  ng-repeat="user in model.users">
        <input kendo-numeric-text-box
               k-min="0" k-max="10000000"
               k-ng-model="user.wealth" />
</div>

 

ASP.Net Core 2: Quick and Dirty Windows Authentication

410329-636167162756711825-16x9.jpgHere’s the bare minimum you need to add to your ASP.Net Core 2 application to restrict it to certain Windows Active Directory groups.

 

 

 

Step 1: Enable Windows Authentication in the Project Properties:

2018-06-06_14-32-08.png

Step 2: add this to your Web.Config <aspNetCore> node:

 <aspNetCore forwardWindowsAuthToken="true"

Step 3: Enable Windows Authentication in IIS

2018-06-06_14-33-18.png

Step 4: Add Authentication to the ConfigureServices call in your Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            // IISDefaults requires the following import:
            // using Microsoft.AspNetCore.Server.IISIntegration;
            services.AddAuthentication(IISDefaults.AuthenticationScheme);
 
            services.Configure<IISOptions>(options => {
                options.AutomaticAuthentication = true;
                options.ForwardClientCertificate = true;
            });

Step 5: Add this to your Configuration call in Startup.cs

app.UseAuthentication();

Step 6: Add [AllowAnonymous] to Controllers which need anonymous access

Step 7: Add [Authorize(Roles…)] to controllers that need to be restricted

namespace App.Web.Areas.Kxg.Controllers
{
 
    [Authorize(Roles = "MyAD\\Developer,MyAD\\IT_Support_Desk_Staff ")]
    [Area("MyArea")]
    public class CustomerSearch : Controller

Bonus!

To get a list of the Groups used in [Authorize], in PowerShell type:

whoami /groups

ps.png

ASP.Net Core 2: Shared Localization

main-qimg-82f43ebb6d5b671466ae8fbce0195735.pngIn this post we’ll cover the absolute simplest way to localize using a shared resource instead of one resx per controller. Here’s a good deep dive as there is much more to know, but this will get you going.

Step 1: Set up the Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<RequestLocalizationOptions>(
        opts =>
        {
            var supportedCultures = new List<CultureInfo>
            {
                new CultureInfo("en"),
                new CultureInfo("fr"),
            };
 
            opts.DefaultRequestCulture = new RequestCulture("en");
            // Formatting numbers, dates, etc.
            opts.SupportedCultures = supportedCultures;
            // UI strings that we have localized.
            opts.SupportedUICultures = supportedCultures;
        });

Step 2: Then update Configure()

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var options = app.ApplicationServices
               .GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(options.Value);

Step 3: Add a folder called “Resources” to the web project. NOT in an external project. Add these three resx files.

2018-05-27 09_35_12-Window.png

Step 4: Reference the shared resource in your controller

public class PickersController : Controller
{
    private readonly IStringLocalizer<Startup_localizaer;
    public PickersController(IStringLocalizer<Startup> localizaer)
    {
        _localizaer = localizaer;
       var someString =  _localizaer["Your Key from Resx"];
    }