How to set up your own C# Markup project

In this tutorial, you will learn how to set up a new basic project using Uno Platform and C# Markup and using some basic resources and also make a comparison between C# Markup and XAML.

The purpose of this section of the tutorial is to show you how to create a basic project from scratch.

Set up a Markup project

You can use this tutorial to learn how to set up a Uno Platform project. We will use the comparison between two projects, one being C# Markup and the second XAML.

The tutorials below can teach you how to create both projects.

Comparing the structures

Open the two projects created and compare the structure of the two.

Comparing the C# Markup with the XAML project

  • Comparing projects structure

    C# Markup project structure
    • The project is separated into Solution Items, where general settings and properties files are located, and Source, which contains information about the Backend, Platforms and Shared Project.

    • Check out how your first Start Up Markup project will look like.

      Screenshot showing how to check your project's Markup initial structure using Uno Platform within Visual Studio.

    • Set as Startup Project the some Platform and Run the Project.

    • In the Shared Project open the file MainPage.cs and analyze the code that will look like this.

      Screenshot displaying MainPage using C# Markup in the generated project

Start creating some basic UI with C# Markup

Change the MainPage.cs to have a different content as the sample bellow.

Add elements and set attributes on the UI

  • Customizing the UI

    C# Markup
    • The code below shows how to create simple elements like TextBlock in the UI. Working as simple as that new TextBlock().

      //Add Some TextBlock
      new TextBlock()
          .Text("Hello Uno Platform!")
          .Padding(50)//Set some padding
          .Margin(50)//Set some Margin
          .HorizontalAlignment(HorizontalAlignment.Right)//Custom the Alignment
      )
      

Set the background in different ways

  • Changing Background

    C# Markup
    • The code below shows how Set the backgroun in 3 different ways.

    • First - accessing the Application Theme

      new Grid()
          //Set Background from Theme
          .Background(ThemeResource.Get<Brush>("ApplicationSecondaryForegroundThemeBrush"))
          .Children(
      
              new TextBlock()
                  .Text("Hello Uno Platform!")
                  .Padding(50)//Set some padding
                  .Margin(50)//Set some Margin
                  .HorizontalAlignment(HorizontalAlignment.Right)//Custom the Alignment
      
          ),
      
      • Second - using the Colors Helper
      
      new Grid()
          //Set Background from Colors
          .Background(new SolidColorBrush(Colors.Silver))
          .Children(
      
              new TextBlock()
                  .Text("Hello Uno Platform!")
                  .Padding(50)//Set some padding
                  .Margin(50)//Set some Margin
                  .HorizontalAlignment(HorizontalAlignment.Right)//Custom the Alignment
      
          )
      
      • Third - creating a new Brush through a manual ARGB color.
      new Grid()
          //Set Background from custom Brush
          .Background(new SolidColorBrush(Color.FromArgb(255, 233, 233, 233)))
          .Children(
      
              new TextBlock()
                  .Text("Hello Uno Platform!")
                  .Padding(50)//Set some padding
                  .Margin(50)//Set some Margin
                  .HorizontalAlignment(HorizontalAlignment.Right)//Custom the Alignment
      
          )
      

Work with the Grid in order to customize the columns and their children

  • Changing Grid, RowDefinitions and ColumnDefinitions

    C# Markup
    • The code below shows how to create simple Grid element and add the RowDefinitions and ColumnDefinitions.

      new Grid()
          //Custom the Row and Column Definitions
          .RowDefinitions<Grid>("Auto, *")
          .ColumnDefinitions<Grid>("2*, Auto, 3*")
      
      new Grid()
          //Custom the Row and Column Definitions
          .RowDefinitions<Grid>("Auto, *")
          .ColumnDefinitions<Grid>("2*, Auto, 3*")
      
          //Set Background from Theme
          .Children(
      
              new TextBlock()
                  .Padding(50)
                  .Grid(row: 0, column: 0)//Set and Attached Properties
                  .Text("Row 0"),
      
              new TextBlock()
                  .Margin(50)
                  .Grid(grid => grid.Row(0).Column(1).ColumnSpan(2))//Attached Properties using builder pattern
                  .Text("Row 0 with ColumnSpan and Attached Properties using builder pattern!"),
      
              new TextBlock()
                  .Margin(50)
                  .Grid(row: 1, column: 0)//Set and Attached Properties
                  .Text("Row 1 and Column 0"),
      
              new TextBlock()
                  .Margin(50)
                  .Grid(row: 1, column: 1)//Set and Attached Properties
                  .Text("Row 1 and Column 1"),
      
              new TextBlock()
                  .Margin(50)
                  .Grid(grid => grid.Row(1).Column(2))//Attached Properties using builder pattern
                  .Text("Row 1 and Column 2")
          )
      

Try it yourself

Now try to change your MainPage to have different layout and test other attributes and elements..

We continue in the next section to learn how to configure styles, work with Bindings, Templates and Template Selectors.

Next Steps

Learn more about: