Introducing C# Mark-up for Xamarin.Forms


Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

C# markup is a set of fletch method and class which is used to create interfaces in xamarin in C# or we can say that it makes it easy to work with. For that purpose xamarin form.markup namespace API is used.

Xamarin.form 4.6 was introduced in C# mark-up. C# markup helps the developer to write code separately it means that developer can separate their code for designing in a separate file and the code for business logic(.cs) file and 3 back and related coding can be stored in different files and you can also do binding between these two files and run the program. This structure reduces the work of a developer to handle all the code in a single file that is why it is known as one of the important features.

If you are using only one language hand working in this structure reduce the loading time of the project and also reduces markup scattering and it all adds up to run the project fast all the necessary files running in the background which doesn't disturb the user permit user to work in the UI.

By creating a separate file for each part of the project it almost become unnecessary to create converter, style, resources, dictionaries, behavior, Trigger, and markup extension files what we can say that we do not need them.

Some of the features which were introduced in C# mark-up:

  1. Data binding
  2. Layout
  3. Gesture
  4. Left to right and right to left
  5. Layout line conversion
  6. Grid row and columns
  7. Effect
  8. Logic integration
  9. Styles
  10. Platform - Specifics

Data binding

C# mark-up includes lower loaded bind extension method which binds view bindable property and special property. Bind method will bind most of the control with bindable property and leads to xamarin.form file. While using this property you cannot use the target property. But since you can add default bindable property for additional control which you can also register.

You can use the Bind Method with any bindable property by binding it.



Example:

using Xamarin.Forms.Markup;
// ...

new Button { Text = "Data not Found" }.Bind (Label.IsVisibleProperty, nameof(vm.Empty))

By default, the CommandParameter binds to the binding context. You can use command and CommandParameter to set binding path and source to perform binding.

Example:

using Xamarin.Forms.Markup;
// ...

new ListView { Text = "Touch me " }.BindCommand (nameof(vm.TapCommand), vm, nameof(Item.Id))

This example binding context is an item instance therefore to perform CommandParameter binding we do not require any specific source

If you want to bind then you can use the bind command method and passing null in the parameter path argument and you can use the bindable method

You can use Command and CommandParameter for additional control

Example
using Xamarin.Forms.Markup;
//...

DefaultBindableProperties.RegisterMyCommand(
    (CustomViewOneCommandProperty, CustomViewOne.CommandParameterProperty),
    (CustomViewTwo.CommandProperty, CustomViewTwo.CommandParameterProperty)
);

Inline converter code converts and converts back in which you can bind and pass in the method

Example:

using Xamarin.Forms.Markup;
//...

new ListView { Text = "Ten Millions Tree Node" }.Bind (Label.MarginProperty, nameof(TreeNode.TreeDepth),convert: (int dep) => new Thickness(depth * 20, 10, 10, 10))

It also supports the Typesafe converter parameter like this

Example

using Xamarin.Forms.Markup;
//...

new Entry { }
           .Bind (nameof(viewModel.Text),
                  convert: (string name, int repeatedtime) => string.Concat(Enumerable.Repeat(name, repeatedtime)))

funcConvert class can use clusterinfo object.

using Xamarin.Forms.Markup;
//...

cultureAwareConverter = new FuncConverter(
    (dates, daysToAdd, naturalnumbers) => dates.AddDays(daysToAdd).ToString(naturalnumbers)
);

Span object specified with Formatted text property can also perform data binding.

Example

 using Xamarin.Forms.Markup;
//...

new Label { } .FormattedText (
      new Span { TextColor = Color.Pink, TextDecorations = TextDecorations.Bold }
              .BindTapGesture (nameof(vm.ContinueToCSharpForMarkupCommand))
              .Bind (nameof(vm.Title))
)

These are some ways by which you can perform data-binding which were executed in C#.

Layout

C# markup includes a series of the layout with extension methods which helps the view to be in a proper position.

Type

Extension methods

FlexLayout

Alignself, Basis, Grow, Menu

Grid

Row, Column, RowSpan, ColumnSpan

Label

TextLeft, TextRight, TextBottom, TextTop, TextCenter

Layout

Padding, Paddings

Layoutoptions

Left, Right, Top, Bottom, Fill, Center, FillHorizontal, LeftExpand

View

Margin, Margins

VisualElement

Height, Width, MinHeight, MinWidth, Size, MinSize

Left-to-right and Right-to-left support

C# Markup is designed to support left to right and right to left direction of flow. An intuitive set provides different methods like left, right, top, and bottom.

This is used to point the design at the right direction which can be set either from right to left or to right. For that it provides 2 namespaces and you can use anyone you like. xamarin.forms.markup.lefttoright or xamarin.forms.markup.righttoleft.

For that you can use methods as shown below:

Type

Method name

Label

TextStart, TextEnd

Layout option

Start, End

Layout line convention

Following order row and column contains the view

  1. Alignment with row and column
  2. Margin around the view
  3. View size
  4. Padding within view
  5. Content alignment within the padding

Example:

new Entry { }
           .Row (BodyRow.Prompt) .ColumnSpan (All()) .FillExpandHorizontal () .CenterVertical () .Margin (fieldNameMargin) .TextCenterHorizontal () // Layout line

The advantage of doing this is that you can easily read the code and you can also take a guess by seeing the UI code that what type of UI will be created, that is why C# markup is important and helpful.


One Stop Solution for Xamarin Mobile App Development ?

Your Search ends here.


Grid Rows and Columns

nstead of using numbers, you can define grid row and column with the help of enumeration. The main advantage of using grid row and column is that you don't have to do renumbering for add and remove and for that, you can use the following workspace: Using static xamrain.forms.markup.grid.rowcolumns.

Example:

using Xamarin.Forms.Markup;
using Xamarin.Forms.Markup.LeftToRight;
using static Xamarin.Forms.Markup.GridRowsColumns;

View Build() => new Grid
{
    RowDefinitions = Rows.Define(
        (BodyRow.Prompt    , 60 ),
        (BodyRow.CodeHeader, 25  ),
        (BodyRow.CodeEntry , Star),
        (BodyRow.Button    , Star)
    ),

    ColumnDefinitions = Columns.Define(
        (BodyCol.FieldLabel     , 135 ),
        (BodyCol.FieldValidation, Auto)
    ),    
};

Platform-specific

The invoke extension method is used to execute the platform-specific. To remove the error, use the xamrain.forms.platformconfiguratons.iosspecific namespace. Use or make new aliases for the namespace and use them for working on a particular platform.

Trigger-in-Xamarin

Reference: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/windows/

Till now we saw all the examples and that is a little example created in which UI and coding have their codes in different files and we have also used a grid, label, and entry

Example

Grid gr = new Grid();

gr.Children.Add(lbl, 0, 1);

Entry entr = new Entry
{
    Placeholder = "Enter Your number",
    Keyboard = Keyboard.Defult,
    BackgroundColor = Color.NavyBlue,
    TextColor = Color.Yellow,
    FontSize = 20,
    HeightRequest = 24,
    Margin = fieldMargin
};
gr.Children.Add(entr, 0, 4);
Grid.SetColumnSpan(entr, 5);
entr.SetBinding(Entry.TextProperty, new Binding("RegistrationCodeEnter"));

Content = gr;

Trigger-in-Xamarin

Reference: https://docs.microsoft.com

Concussion

In this blog, we have seen that C# markup use xamarin very easily. It allows us to separate our business logic and designing view and we have also discussed many features of it.

Introducing C# Mark-up for Xamarin.Forms

C# markup is a set of fletch method and class which is used to create interfaces in xamarin in C# or we can say that it makes it easy to work with. For that purpose xamarin form.markup namespace API is used.

Xamarin.form 4.6 was introduced in C# mark-up. C# markup helps the developer to write code separately it means that developer can separate their code for designing in a separate file and the code for business logic(.cs) file and 3 back and related coding can be stored in different files and you can also do binding between these two files and run the program. This structure reduces the work of a developer to handle all the code in a single file that is why it is known as one of the important features.

If you are using only one language hand working in this structure reduce the loading time of the project and also reduces markup scattering and it all adds up to run the project fast all the necessary files running in the background which doesn't disturb the user permit user to work in the UI.

By creating a separate file for each part of the project it almost become unnecessary to create converter, style, resources, dictionaries, behavior, Trigger, and markup extension files what we can say that we do not need them.

Some of the features which were introduced in C# mark-up:

  1. Data binding
  2. Layout
  3. Gesture
  4. Left to right and right to left
  5. Layout line conversion
  6. Grid row and columns
  7. Effect
  8. Logic integration
  9. Styles
  10. Platform - Specifics

Data binding

C# mark-up includes lower loaded bind extension method which binds view bindable property and special property. Bind method will bind most of the control with bindable property and leads to xamarin.form file. While using this property you cannot use the target property. But since you can add default bindable property for additional control which you can also register.

You can use the Bind Method with any bindable property by binding it.



Example:

using Xamarin.Forms.Markup;
// ...

new Button { Text = "Data not Found" }.Bind (Label.IsVisibleProperty, nameof(vm.Empty))

By default, the CommandParameter binds to the binding context. You can use command and CommandParameter to set binding path and source to perform binding.

Example:

using Xamarin.Forms.Markup;
// ...

new ListView { Text = "Touch me " }.BindCommand (nameof(vm.TapCommand), vm, nameof(Item.Id))

This example binding context is an item instance therefore to perform CommandParameter binding we do not require any specific source

If you want to bind then you can use the bind command method and passing null in the parameter path argument and you can use the bindable method

You can use Command and CommandParameter for additional control

Example
using Xamarin.Forms.Markup;
//...

DefaultBindableProperties.RegisterMyCommand(
    (CustomViewOneCommandProperty, CustomViewOne.CommandParameterProperty),
    (CustomViewTwo.CommandProperty, CustomViewTwo.CommandParameterProperty)
);

Inline converter code converts and converts back in which you can bind and pass in the method

Example:

using Xamarin.Forms.Markup;
//...

new ListView { Text = "Ten Millions Tree Node" }.Bind (Label.MarginProperty, nameof(TreeNode.TreeDepth),convert: (int dep) => new Thickness(depth * 20, 10, 10, 10))

It also supports the Typesafe converter parameter like this

Example

using Xamarin.Forms.Markup;
//...

new Entry { }
           .Bind (nameof(viewModel.Text),
                  convert: (string name, int repeatedtime) => string.Concat(Enumerable.Repeat(name, repeatedtime)))

funcConvert class can use clusterinfo object.

using Xamarin.Forms.Markup;
//...

cultureAwareConverter = new FuncConverter(
    (dates, daysToAdd, naturalnumbers) => dates.AddDays(daysToAdd).ToString(naturalnumbers)
);

Span object specified with Formatted text property can also perform data binding.

Example

 using Xamarin.Forms.Markup;
//...

new Label { } .FormattedText (
      new Span { TextColor = Color.Pink, TextDecorations = TextDecorations.Bold }
              .BindTapGesture (nameof(vm.ContinueToCSharpForMarkupCommand))
              .Bind (nameof(vm.Title))
)

These are some ways by which you can perform data-binding which were executed in C#.

Layout

C# markup includes a series of the layout with extension methods which helps the view to be in a proper position.

Type

Extension methods

FlexLayout

Alignself, Basis, Grow, Menu

Grid

Row, Column, RowSpan, ColumnSpan

Label

TextLeft, TextRight, TextBottom, TextTop, TextCenter

Layout

Padding, Paddings

Layoutoptions

Left, Right, Top, Bottom, Fill, Center, FillHorizontal, LeftExpand

View

Margin, Margins

VisualElement

Height, Width, MinHeight, MinWidth, Size, MinSize

Left-to-right and Right-to-left support

C# Markup is designed to support left to right and right to left direction of flow. An intuitive set provides different methods like left, right, top, and bottom.

This is used to point the design at the right direction which can be set either from right to left or to right. For that it provides 2 namespaces and you can use anyone you like. xamarin.forms.markup.lefttoright or xamarin.forms.markup.righttoleft.

For that you can use methods as shown below:

Type

Method name

Label

TextStart, TextEnd

Layout option

Start, End

Layout line convention

Following order row and column contains the view

  1. Alignment with row and column
  2. Margin around the view
  3. View size
  4. Padding within view
  5. Content alignment within the padding

Example:

new Entry { }
           .Row (BodyRow.Prompt) .ColumnSpan (All()) .FillExpandHorizontal () .CenterVertical () .Margin (fieldNameMargin) .TextCenterHorizontal () // Layout line

The advantage of doing this is that you can easily read the code and you can also take a guess by seeing the UI code that what type of UI will be created, that is why C# markup is important and helpful.


One Stop Solution for Xamarin Mobile App Development ?

Your Search ends here.


Grid Rows and Columns

nstead of using numbers, you can define grid row and column with the help of enumeration. The main advantage of using grid row and column is that you don't have to do renumbering for add and remove and for that, you can use the following workspace: Using static xamrain.forms.markup.grid.rowcolumns.

Example:

using Xamarin.Forms.Markup;
using Xamarin.Forms.Markup.LeftToRight;
using static Xamarin.Forms.Markup.GridRowsColumns;

View Build() => new Grid
{
    RowDefinitions = Rows.Define(
        (BodyRow.Prompt    , 60 ),
        (BodyRow.CodeHeader, 25  ),
        (BodyRow.CodeEntry , Star),
        (BodyRow.Button    , Star)
    ),

    ColumnDefinitions = Columns.Define(
        (BodyCol.FieldLabel     , 135 ),
        (BodyCol.FieldValidation, Auto)
    ),    
};

Platform-specific

The invoke extension method is used to execute the platform-specific. To remove the error, use the xamrain.forms.platformconfiguratons.iosspecific namespace. Use or make new aliases for the namespace and use them for working on a particular platform.

Trigger-in-Xamarin

Reference: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/windows/

Till now we saw all the examples and that is a little example created in which UI and coding have their codes in different files and we have also used a grid, label, and entry

Example

Grid gr = new Grid();

gr.Children.Add(lbl, 0, 1);

Entry entr = new Entry
{
    Placeholder = "Enter Your number",
    Keyboard = Keyboard.Defult,
    BackgroundColor = Color.NavyBlue,
    TextColor = Color.Yellow,
    FontSize = 20,
    HeightRequest = 24,
    Margin = fieldMargin
};
gr.Children.Add(entr, 0, 4);
Grid.SetColumnSpan(entr, 5);
entr.SetBinding(Entry.TextProperty, new Binding("RegistrationCodeEnter"));

Content = gr;

Trigger-in-Xamarin

Reference: https://docs.microsoft.com

Concussion

In this blog, we have seen that C# markup use xamarin very easily. It allows us to separate our business logic and designing view and we have also discussed many features of it.

  • Pin It
TOP