Xamarin.Forms C# Code Generation

Autogenerated by MFractor v3.5.1

Generate Bindable Property

Configuration Id: com.mfractor.code_gen.forms.csharp.bindable_property

Generates a Xamarin.Forms bindable property implementation and a proxy property that calls the BindableProperty implementation.

Configurable Properties

Name Type Defaults To Description
Snippet Code Snippet

Arguments:
  • name: The name of the new bindable property.
  • type: The type of the new bindable property.
  • control_type: The control type that the new bindable property has been created inside.
public static readonly Xamarin.Forms.BindableProperty Property = Xamarin.Forms.BindableProperty.Create(nameof(), typeof(), typeof(), default());
public
{
get
{
return ()GetValue(Property);
}

set
{
SetValue(Property, value);
}
}
When creating the new bindable property, what is the default code snippet MFractor should use?

Generate Custom Renderer For Xamarin.Forms Control

Configuration Id: com.mfractor.code_gen.forms.generate_control_native_renderer

For a given Xamarin.Forms user control in C#, the Generate Custom Renderer For Xamarin.Forms Control code action enables you to generate a custom renderer for that control in each iOS and Android project in your solution.

Uses:

Generate ICommand Implementation

Configuration Id: com.mfractor.code_gen.forms.csharp.command_implementation

Generates a boilerplate implementation of ICommand using an inline Xamarin.Forms.Command.

Configurable Properties

Name Type Defaults To Description
Snippet Code Snippet

Arguments:
  • name: The name of the new command.
  • type: The fully qualified type of the new command
public System.Windows.Input.ICommand
{
get
{
return new (() =>
{
throw new System.NotImplementedException();
});
}
}
What is the code snippet to use when creating the command stub? If not set, this code generator will default to generating a stub Xamarin.Forms.Command implementation
CommandType System.String Xamarin.Forms.Command The fully qualified type to use as the command implementation. For example, using Xamarin.Forms.Command would cause the new expression to be new Xamarin.Forms.Command

Generate IValueConverter Implementation

Configuration Id: com.mfractor.code_gen.forms.csharp.value_converter

Generates an implementation of a value converter.

Configurable Properties

Name Type Defaults To Description
CreateMissingValueConversionAttribute Boolean True If an implementation of the ValueConversionAttribute class cannot be resolved within the project compilation, should MFractor automatically create the implementation?
Snippet Code Snippet

Arguments:
  • name: The name of the new value converter.
  • namespace: The namespace the new converter will be placed inside.
  • value_conversion_attribute: The fully qualified type of the value conversion attribute (if available).
  • input_type: The fully qualified type that the value converter should accept as it's input type. If unavailable, defaults to System.Object.
  • output_type: The fully qualified type that the value converter should accept as it's output type. If unavailable, defaults to System.Object.
  • parameter_type: The fully qualified type that the value converter should accept as it's parameter type. If unavailable, defaults to System.Object.
using System;
using System.Globalization;
using Xamarin.Forms;

namespace
{
[(typeof(), typeof())]
public class : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is == false)
{
return default();
}

var input = ()value;

// TODO: Put your value conversion logic here.

return default();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
What is the code snippet to use when generating the value converter class file?

Uses:

Generate Platform Specific Effect

Configuration Id: com.mfractor.code_gen.forms.csharp.platform_effect

Generates a new effect, creating the effect inside the common project as well as linked Android and iOS projects.

Configurable Properties

Name Type Defaults To Description
iOSEffectsFolder System.String Effects What is the folder that iOS platform-specific effects should be placed into?
AndroidEffectsFolder System.String Effects What is the folder that Android platform-specific effects should be placed into?
EffectsFolder System.String Effects What is the folder that the effects declaration be placed into?
ResolutionGroupName System.String Effects What is the name of the resolution group?
iOSEffectSnippet Code Snippet

Arguments:
  • name: The name of the new effect.
  • group_name: The resolution group name of the new effect.
  • namespace: The iOS projects default namespace.
  • platform: If you wish to shared the platform-specific effect template, this is the name of the effects platform.
using .Effects;
using Xamarin.Forms;
using Xamarin.Forms.Platform.;

[assembly: ResolutionGroupName("")]
[assembly: ExportEffect(typeof(), "")]
namespace .Effects
{
public class : PlatformEffect
{
protected override void OnAttached()
{
// TODO: Apply your effect.
}

protected override void OnDetached()
{
// TODO: Remove your effect.
}
}
}
What is the code snippet for the iOS platform-specific effect?
AndroidEffectSnippet Code Snippet

Arguments:
  • name: The name of the new effect.
  • group_name: The resolution group name of the new effect.
  • namespace: The Android projects default namespace.
  • platform: If you wish to shared the platform-specific effect template, this is the name of the effects platform.
using .Effects;
using Xamarin.Forms;
using Xamarin.Forms.Platform.;

[assembly: ResolutionGroupName("")]
[assembly: ExportEffect(typeof(), "")]
namespace .Effects
{
public class : PlatformEffect
{
protected override void OnAttached()
{
// TODO: Apply your effect.
}

protected override void OnDetached()
{
// TODO: Remove your effect.
}
}
}
What is the code snippet for the Android platform-specific effect?
EffectSnippet Code Snippet

Arguments:
  • name: The name of the new effect.
  • group_name: The resolution group name of the new effect.
  • namespace: The common projects default namespace.

using System;
using Xamarin.Forms;

namespace
{
public class : RoutingEffect
{
public () : base(".")
{
}
}
}
What is the code snippet for the effect?

Generate Value Conversion Attribute

Configuration Id: com.mfractor.code_gen.forms.csharp.value_conversion_attribute

Generates an implementation of the ValueConversionAttribute; the attribute used to hint type-flow in a value converter for design time tools like MFractor.

Configurable Properties

Name Type Defaults To Description
Snippet Code Snippet

Arguments:
  • namespace: The namespace that the new value conversion attribute should be placed within.
using System;
namespace
{
///
/// When annotated onto an IValueConverter implementation, enables third party tools to analyse the type-flow of binding expressions.
///

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ValueConversionAttribute : Attribute
{
public ValueConversionAttribute(Type input, Type output)
{
}

public Type ParameterType
{
get;
set;
}
}
}
What is the default code snippet to use when creating the value conversion attribute implementation?

Uses:

Generate View With XAML and Code-Behind Class

Configuration Id: com.mfractor.code_gen.forms.xaml_view_with_code_behind

Creates a new view/control using a XAML to define the UI and a code-behind class to encapsulate backing logic.

Configurable Properties

Name Type Defaults To Description
ViewsFolder System.String null What is the folder that new XAML views be placed into?
CodeBehindSnippet Code Snippet

Arguments:
  • name: The name of the new class.
  • namespace: The namespace that the new class resides inside.
  • type: The base type of the new class.
using System;

namespace
{
public partial class :
{
public ()
{
InitializeComponent();
}
}
}
What is the default implementation of the new XAML control's code behind class?

Uses:

Generates Xamarin.Forms Cascading Style Sheet

Configuration Id: com.mfractor.code_gen.forms.generate_style_sheet

Generates a new Xamarin.Forms cascading style sheet that can be used to style XAML

Configurable Properties

Name Type Defaults To Description
Snippet Code Snippet

Arguments:
  • control: The name of the control that this style sheet is for. EG: stackLayout, entry, view etc
{
margin:10;
}
The default cascading style sheet to
StyleFolder System.String Styles What is the folder to place new style sheets into?
DefaultControl System.String view What is the default control to use when generating new style sheets?

String Localisation Generator

Configuration Id: com.mfractor.code_gen.forms.replace_with_localisation_lookup

The string localisation generator converts literal string value inside a XAML document to a localised value lookup. This replaces the text with a localistaion lookup extension and generates a resx entry for the old value.

Configurable Properties

Name Type Defaults To Description
TranslateExtensionSymbol System.String .i18n.TranslateExtension What is the full sybol name (namespace + class name) of the translate markup extension? Prepend the symbol name with a dot to make it relative to the projects default namespace. For example, using .Localisation.TranslateExtension when the default namespace is MyApp would cause the full symbol name to become MyApp.Localisation.TranslateExtension.
TranslationXamlNamespace System.String i18n When importing a XAML namespace reference to the translation extension, what is the default namespace name that should be used?
XamlLocalisationExpression Code Snippet

Arguments:
  • namespace: The xml namespace for the translate extension symbol when used in XAML.
  • translate_extension: The name of the translation extension that's used to lookup a localised value for a given key.
  • key: The key that's used to lookup a localised value in this applications resources.
  • resx_name: The name of the RESX file to look into for the given key, excluding the .resx file extension.
{: } When importing a XAML namespace reference to the translation extension, what is the default namespace name that should be used?

Uses:

View Model Property Generator

Configuration Id: com.mfractor.code_gen.forms.csharp.view_model_property

Generates a property declaration for a Xamarin.Forms ViewModel. By default, this code generator routes to the standard PropertyGenerator. However, specifying a code snippet will cause the code generator to use that instead.

Configurable Properties

Name Type Defaults To Description
Snippet Code Snippet

Arguments:
  • type: The fully qualified type of the new property.
  • name: The name of the new property.
public
{
get;
set;
}
What is the code snippet to use when creating the property declaration?
ValueSnippet Code Snippet

Arguments:
  • type: The fully qualified type of the new property.
  • name: The name of the new property.
  • value: The value to assign to the new property.
public
{
get;
set;
} = ;
What is the code snippet to use when creating a property declaration with a default value?
DefaultEnumerableType System.String System.Collections.Generic.List When the new view model property is an IEnumerable, what is the default type that should be used instead of IEnumerable? To use the provided IEnumerable type, set this to an empty string.

XAML Localisation Infrastructure Generation

Configuration Id: com.mfractor.code_gen.forms.xaml_localisation_infrastructure

Builds the necessary infrustructure needed for XAML localisation using the best practice outlined in Localizing Xamarin.Forms Apps with RESX Resource Files.

This code generator will create the following:

MyApp.Core

  • i18n/TranslateExtension.cs:
  • i18n/ILocalise.cs: The interface that provides the current culture on each platform.

MyApp.iOS - Localise.cs: The iOS implementation of ILocalise to provide iOS's current culture.

MyApp.Android - Localise.cs: The Android implementation of ILocalise to provide iOS's current culture.

Configurable Properties

Name Type Defaults To Description
LocalisationFolder System.String i18n What is the folder that the translation extension and localiser class should be placed into?
LocaliserName System.String ILocalise What is the name of the class that provides the culture information for translation extension?
ILocaliseSnippet Code Snippet

Arguments:
  • namespace: The projects namespace
  • name: The name of the new localiser interface. Defaults to ILocalise
using System;
using System.Globalization;

namespace
{
public interface ILocalise
{
CultureInfo GetCurrentCultureInfo();

void SetLocale();
}
}
The code snippet to use for the ILocalise interface that each platform project should implement.
TranslateExtensionSnippet Code Snippet

Arguments:
  • namespace: The projects namespace
  • name: The name of the translation extension class. This will be derived from the string localisation generations TranslateExtensionSymbol property.
  • resource: The fully qualified type of the resource file that the translate extension should use when looking for localisation values. This will be provided by the apps default resource file config.
  • localiser: The name of the localisation interface that provides the current culture.
using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace
{
// You exclude the 'Extension' suffix when using in Xaml markup
[ContentProperty ("Text")]
public class : IMarkupExtension
{
readonly CultureInfo ci;
const string ResourceId = "";

private static readonly Lazy ResMgr = new Lazy(()=> new ResourceManager(ResourceId, typeof().GetTypeInfo().Assembly));

public ()
{
if (Device.RuntimePlatform == Device.iOS
Device.RuntimePlatform == Device.Android)
{
ci = DependencyService.Get<>().GetCurrentCultureInfo();
}
}

public string Text { get; set; }

public object ProvideValue (IServiceProvider serviceProvider)
{
if (Text == null)
return "";

var translation = ResMgr.Value.GetString(Text, ci);

if (translation == null)
{
#if DEBUG
throw new ArgumentException(
String.Format("Key '{0}' was not found in resources '{1}' for culture '{2}'.", Text, ResourceId, ci.Name),
"Text");
#else
translation = Text; // returns the key, which GETS DISPLAYED TO THE USER
#endif
}
return translation;
}
}
}
AndroidLocaliseSnippet Code Snippet

Arguments:
  • namespace: The projects namespace
  • i18n_namespace: The namespace that the translate extension and ILocalise interface live within
  • localiser: The name of the localisation interface that provides the current culture.
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using Java.Util;
using Xamarin.Forms;
using ;

[assembly: Dependency(typeof(.Localise))]

namespace
{
public class Localise :
{
public void SetLocale(CultureInfo ci)
{
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}

public CultureInfo GetCurrentCultureInfo()
{
var netLanguage = "en";
var androidLocale = Java.Util.Locale.Default;
netLanguage = AndroidToDotnetLanguage(androidLocale.ToString().Replace("_", "-"));
// this gets called a lot - try/catch can be expensive so consider caching or something
System.Globalization.CultureInfo ci = null;
try
{
ci = new System.Globalization.CultureInfo(netLanguage);
}
catch (CultureNotFoundException e1)
{
// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)
// fallback to first characters, in this case "en"
try
{
var fallback = ToDotnetFallbackLanguage(new PlatformCulture(netLanguage));
ci = new System.Globalization.CultureInfo(fallback);
}
catch (CultureNotFoundException e2)
{
// iOS language not valid .NET culture, falling back to English
ci = new System.Globalization.CultureInfo("en");
}
}
return ci;
}
string AndroidToDotnetLanguage(string androidLanguage)
{
var netLanguage = androidLanguage;
//certain languages need to be converted to CultureInfo equivalent
switch (androidLanguage)
{
case "ms-BN": // "Malaysian (Brunei)" not supported .NET culture
case "ms-MY": // "Malaysian (Malaysia)" not supported .NET culture
case "ms-SG": // "Malaysian (Singapore)" not supported .NET culture
netLanguage = "ms"; // closest supported
break;
case "in-ID": // "Indonesian (Indonesia)" has different code in .NET
netLanguage = "id-ID"; // correct code for .NET
break;
case "gsw-CH": // "Schwiizertüütsch (Swiss German)" not supported .NET culture
netLanguage = "de-CH"; // closest supported
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
}
return netLanguage;
}
string ToDotnetFallbackLanguage(PlatformCulture platCulture)
{
var netLanguage = platCulture.LanguageCode; // use the first part of the identifier (two chars, usually);
switch (platCulture.LanguageCode)
{
case "gsw":
netLanguage = "de-CH"; // equivalent to German (Switzerland) for this app
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
}
return netLanguage;
}
}
}
}
The code snippet to use for the Android implementation of the localization interface to provide the current culture.
iOSLocaliseSnippet Code Snippet

Arguments:
  • namespace: The projects namespace
  • i18n_namespace: The namespace that the translate extension and ILocalise interface live within
  • localiser: The name of the localisation interface that provides the current culture.
using System;
using System.Globalization;
using System.Threading;
using Foundation;
using Xamarin.Forms;
using ;

[assembly: Dependency(typeof(.Localise))]

namespace
{
public class Localise :
{
public void SetLocale (CultureInfo ci)
{
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}

public CultureInfo GetCurrentCultureInfo ()
{
var netLanguage = "en";
if (NSLocale.PreferredLanguages.Length > 0)
{
var pref = NSLocale.PreferredLanguages [0];
netLanguage = iOSToDotnetLanguage(pref);
}
// this gets called a lot - try/catch can be expensive so consider caching or something
System.Globalization.CultureInfo ci = null;
try
{
ci = new System.Globalization.CultureInfo(netLanguage);
}
catch (CultureNotFoundException e1)
{
// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)
// fallback to first characters, in this case "en"
try
{
var fallback = ToDotnetFallbackLanguage(new PlatformCulture(netLanguage));
ci = new System.Globalization.CultureInfo(fallback);
}
catch (CultureNotFoundException e2)
{
// iOS language not valid .NET culture, falling back to English
ci = new System.Globalization.CultureInfo("en");
}
}
return ci;
}

string iOSToDotnetLanguage(string iOSLanguage)
{
var netLanguage = iOSLanguage;
//certain languages need to be converted to CultureInfo equivalent
switch (iOSLanguage)
{
case "ms-MY": // "Malaysian (Malaysia)" not supported .NET culture
case "ms-SG": // "Malaysian (Singapore)" not supported .NET culture
netLanguage = "ms"; // closest supported
break;
case "gsw-CH": // "Schwiizertüütsch (Swiss German)" not supported .NET culture
netLanguage = "de-CH"; // closest supported
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
}
return netLanguage;
}

string ToDotnetFallbackLanguage (PlatformCulture platCulture)
{
var netLanguage = platCulture.LanguageCode; // use the first part of the identifier (two chars, usually);
switch (platCulture.LanguageCode)
{
case "pt":
netLanguage = "pt-PT"; // fallback to Portuguese (Portugal)
break;
case "gsw":
netLanguage = "de-CH"; // equivalent to German (Switzerland) for this app
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
}
return netLanguage;
}
}
}
The code snippet to use for the iOS implementation of the localization interface to provide the current culture.

Uses: