简体   繁体   中英

When creating a template is there any reason to have a back end C# initializer?

I have this example in my application:

<?xml version="1.0" encoding="utf-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:J;assembly=J" 
             x:Class="J.Templates.HelpGrid" 
             Padding="15"
             RowSpacing="10"
             ColumnSpacing="10" />

and in c#

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace J.Templates
{
    public partial class HelpGrid : Grid
    {
        public HelpGrid()
        {
            InitializeComponent();
        }
    }
}

From what I see the code works and I can use that template without or without my coding a back end c#.

Is there any reason why the developer might have chosen to add the back end C# code?

The backing class (c# file) is needed by the compiler to work, at this time you cant avoid it.

In theory, it is possible to eliminate the need of the backing class, but it's not an easy thing to do.

They could add a new task in the compile pipeline but this will slow the build a lot (more xaml, more slow).

At the end there is very little benefits and a lot of works, its not worth.

You can read the full explanation, made by the Xamarin staff, here: https://github.com/xamarin/Xamarin.Forms/issues/4327#issuecomment-436921856

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM