Thursday, 7 September 2017

iOS P12 Creation

When exporting a P12 from Key Chain Access, you should right click on the certificate and click Export and not export against the Private Key.

Tuesday, 9 May 2017

iOS App Package Creation

We had a requirement to create .app packages of our Xamarin Forms application for submission to a third party. The requirement was to create this for a iPhone 6 iOS 10.2 Simulator.

Initially I thought rebuilding the solution in Visual Studio 2015 would be enough as this would create what looked like the .app package however when using the following command line :-

xcrun simctl install booted /users/[username]/desktop/certificates/[username]/[packagename].app 

This resulted in a "Failed to chmod : No such file or directory" error.

 To resolve this issue I found that along with rebuilding the solution in Visual Studio, you need to deploy the application to the simulator, which inflates the .app package file by ~50MB, and on inspection of the before and after .app packages, the DLLs and EXE are included which allows the .app package file to be deployed to the simulator successfully, using the command line above.

Friday, 10 April 2015

StringToColorConverter for Xamarin.Forms

You may have a requirement to bind a string property to a Color property, such as TextColor. If this is the case then you will need to create a converter to perform the conversion from string to Color. The following is a sample of such a converter :-

Firstly, the converter :-

using System;
using System.Globalization;
using Xamarin.Forms;
namespace LabelTextColorSample
{
    public class StringToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string valueAsString = value.ToString();
            switch (valueAsString)
            {
                case (""):
                    {
                        return Color.Default;
                    }
                case ("Accent"):
                    {
                        return Color.Accent;
                    }
                default:
                    {
                        return Color.FromHex(value.ToString());
                    }
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}

Then the XAML which consumes it :-


    
        
            
        
    
    
Then the setting of the BindingContext :-

using Xamarin.Forms;
namespace LabelTextColorSample
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = MyViewModel.Instance;
        }
    }
}

And finally the ViewModel :-

namespace LabelTextColorSample
{
    public class MyViewModel
    {
        private static MyViewModel _instance;
        public static MyViewModel Instance
        {
            get { return _instance ?? (_instance = new MyViewModel()); }
        }
        public string MyTextColor
        {
            get { return "#00FF00"; }
        }
    }
}

Hope this is helpful.

Wednesday, 8 April 2015

Range Slider Renderer for Xamarin.Forms

Based on the Range Slider component in the Xamarin Components store, I have created a renderer so that the Range Slider can be used in Xamarin.Forms.

Firstly, I created a RangeSlider control in the Xamarin.Forms PCL, as follows :-
using Xamarin.Forms;

namespace RangeSliderSample
{
 public class RangeSlider : View
 {
  public static readonly BindableProperty LeftValueProperty =
   BindableProperty.Create(p => p.LeftValue, 0f);

  public float LeftValue
  {
   get { return (float) GetValue(LeftValueProperty); }
   set { SetValue(LeftValueProperty, value); }
  }

  public static readonly BindableProperty RightValueProperty =
   BindableProperty.Create(p => p.RightValue, 0f);

  public float RightValue
  {
   get { return (float) GetValue(RightValueProperty); }
   set { SetValue(RightValueProperty, value); }
  }

  public static readonly BindableProperty MaxValueProperty =
   BindableProperty.Create(p => p.MaxValue, 1f);

  public float MaxValue
  {
   get { return (float) GetValue(MaxValueProperty); }
   set { SetValue(MaxValueProperty, value); }
  }

  public static readonly BindableProperty MinValueProperty =
   BindableProperty.Create(p => p.MinValue, 0f);

  public float MinValue
  {
   get { return (float) GetValue(MinValueProperty); }
   set { SetValue(MinValueProperty, value); }
  }

  public static readonly BindableProperty StepProperty =
   BindableProperty.Create(p => p.Step, 0f);

  public float Step
  {
   get { return (float) GetValue(StepProperty); }
   set { SetValue(StepProperty, value); }
  }
 }
}

I then placed this control on a XAML page :-

 
  
  
  
 



I then created a Custom Renderer in the Android project :-
using RangeSlider;
using RangeSliderSample.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(RangeSliderSample.RangeSlider), typeof(RangeSliderRenderer))]
namespace RangeSliderSample.Droid
{
 public class RangeSliderRenderer : ViewRenderer
 {
  private RangeSliderView _slider;
  
  protected override void OnElementChanged(ElementChangedEventArgs e)
  {
   base.OnElementChanged(e);

   var rangeSlider = e.NewElement as RangeSlider;

   if (rangeSlider != null)
   {
    _slider = new RangeSliderView(Context, rangeSlider.MinValue, rangeSlider.MaxValue, rangeSlider.Step);

    _slider.LeftValueChanged += value =>
    {
     rangeSlider.LeftValue = _slider.LeftValue;
    };

    _slider.RightValueChanged += value =>
    {
     rangeSlider.RightValue = _slider.RightValue;
    };

    SetNativeControl(_slider);
   }
  }
 }
}

The complete solution is here :-
http://www.smartmobiledevice.co.uk/Samples/Xamarin/RangeSliderSample.zip

Xamarin Android Player and McAfee

I normally use a device for all my debugging in Xamarin however I thought I would try the Xamarin Android Player.

After installing and downloading an emulator image I tried starting the emulator however I was present with the following error :-

 OpenGL server is unreachable. Please check that Xamarin Android Player is allowed through your firewall on public networks.

 As I use McAfee, my firewall settings are handled by McAfee rather than the Windows Firewall. I viewed the Firewall settings for the Xamarin Android Player (AndroidPlayer) under "View firewall and anti-spam settings > Firewall > Internet Connections for Programs", and it was set to use "Designated Ports". Editing this entry and setting Incoming and Outgoing to "Open ports to Work and Home networks" now allows me to use the Xamarin Android Player.

Wednesday, 4 March 2015

Setting Color as HSLA using XAML Extension for Xamarin.Forms

The following shows how to set a Color to a HSLA value using a XAML Extension.

Firstly, define a new class called ColorAsHslaExtension.cs :-

[ContentProperty("ColorAsHsla")]
public class ColorAsHslaExtension : IMarkupExtension
{
 public string ColorAsHsla { get; set; }

 public object ProvideValue(IServiceProvider serviceProvider)
 {
  var elements = ColorAsHsla.Split(',');

  double h = double.Parse(elements[0]);
  double s = double.Parse(elements[1]);
  double l = double.Parse(elements[2]);
  double a = double.Parse(elements[3]);

  return Color.FromHsla(h, s, l, a);
 }
}

Next, define the XAML that you make use of the Extension :-


 

Hope this helps.

Update

After reading page 146 of the following :-

https://download.xamarin.com/developer/xamarin-forms-book/BookPreview2-Ch08-Rel0203.pdf

It seems there is a much simpler way of achieving this :-


    
       
          
             0.67
             1.0
             0.5
             1.0