Xamarin.Forms C# Code Generation

Autogenerated by MFractor v3.4.7

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:

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 .il8n.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?

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.ObjectModel.ObservableCollection 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

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

MyApp.iOS - Localize.cs: The iOS implementation of ILocalize to provide iOS's current culture.

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

Configurable Properties

Name Type Defaults To Description
LocalizationFolder System.String il8n What is the folder that the translation extension and localizer class should be placed into?
LocalizerName System.String ILocalize What is the name of the class that provides the culture information for translation extension?
ILocalizeSnippet Code Snippet

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

namespace
{
public interface ILocalize
{
CultureInfo GetCurrentCultureInfo();

void SetLocale();
}
}
The code snippet to use for the ILocalize 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.
  • localizer: The name of the localization 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;
}
}
}
AndroidLocalizeSnippet Code Snippet

Arguments:
  • namespace: The projects namespace
  • il8n_namespace: The namespace that the translate extension and ILocalize interface live within
  • localizer: The name of the localization 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(.Localize))]

namespace
{
public class Localize :
{
#region Public Methods

public CultureInfo GetCurrentCultureInfo()
{
var androidLocale = Locale.Default;

var language = androidLocale.ToString().Replace("_", "-");

var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
if (!cultures.Any(c => c.Name == language))
{
language = "en";
}

return new CultureInfo(language);
}

public void SetLocale()
{
var culture = GetCurrentCultureInfo();

Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}

#endregion
}
}
The code snippet to use for the Android implementation of the localization interface to provide the current culture.
iOSLocalizeSnippet Code Snippet

Arguments:
  • namespace: The projects namespace
  • il8n_namespace: The namespace that the translate extension and ILocalize interface live within
  • localizer: The name of the localization interface that provides the current culture.
using System;
using System.Globalization;
using System.Threading;
using Foundation;
using Xamarin.Forms;
using ;

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

namespace
{
public class Localize :
{
public CultureInfo GetCurrentCultureInfo()
{
var netLanguage = "en";
var prefLanguageOnly = "en";
if (NSLocale.PreferredLanguages.Length > 0)
{
var pref = NSLocale.PreferredLanguages[0];

// HACK: Apple treats portuguese fallbacks in a strange way
// https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html
// "For example, use pt as the language ID for Portuguese as it is used in Brazil and pt-PT as the language ID for Portuguese as it is used in Portugal"
prefLanguageOnly = pref.Substring(0, 2);
if (prefLanguageOnly == "pt")
if (pref == "pt")
pref =
"pt-BR"; // get the correct Brazilian language strings from the PCL RESX (note the local iOS folder is still "pt")
else
pref = "pt-PT"; // Portugal
netLanguage = pref.Replace("", "-");
}

// this gets called a lot - try/catch can be expensive so consider caching or something
CultureInfo ci = null;
try
{
ci = new CultureInfo(netLanguage);
}
catch
{
// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)
// fallback to first characters, in this case "en"
ci = new CultureInfo(prefLanguageOnly);
}

return ci;
}

public void SetLocale()
{
var iosLocaleAuto = NSLocale.AutoUpdatingCurrentLocale.LocaleIdentifier;
var netLocale = iosLocaleAuto.Replace("
", "-");
CultureInfo ci;
try
{
ci = new CultureInfo(netLocale);
}
catch
{
ci = GetCurrentCultureInfo();
}
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
}
}
The code snippet to use for the iOS implementation of the localization interface to provide the current culture.

Uses: