Search code examples
c#xamlxamarin.formsresourcedictionary

Xamarin forms - Convert XAML to c#


I have a ResourceDictionary defined in XAML and I would like to convert it to c#.

I have trouble to convert the OnPlatform property and the Converters property.

How can I convert this?

<OnPlatform x:Key="VolosIconFont" x:TypeArguments="x:String">
    <On Platform="iOS" Value="VolosIconFont" />
    <On Platform="Android" Value="Fonts/VolosIconFont.ttf#VolosIconFont" />
    <On Platform="UWP, WinPhone" Value="Assets/Fonts/VolosIconFont.ttf#VolosIconFont" />
</OnPlatform>

And this?

<core:SelectedItemChangedEventArgsConverter x:Key="SelectedItemChangedEventArgsConverter" />

Solution

  • For the onplatform you can use this snippet whenever the VolosIconFont it's used:

    switch(Device.RuntimePlatform)
    {
        case Device.iOS:
        //reference your xaml item and apply your changes.
            break;
        case Device.Android:
            break;
        case Device.UWP:   
            break;
        case Device.macOS:
            break;
    }
    

    Looking at the converter, that line which you've posted it's just a reference of the C# Converter class. You don't need to map it into C#. It is there already. You just need to set the binding programmatically whenever you want to use the converter: e.g.

    myLabel.SetBinding(myLabelProperty, new Binding("MyC#Property", null, new SelectedItemChangedEventArgsConverter(), null, null));
    

    Don't know for sure why would you like to do this, but to me it seems to be the wrong approach. Try to stick with xaml. it's easier!