xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.87k forks source link

Custom schema definition for xaml base class [Bug] #10946

Open ehsangfl opened 4 years ago

ehsangfl commented 4 years ago

Hi I defined base view

namespace StandardView.Views
{
    public abstract class BaseView : ContentView
    {
    }
}

and defined custom schema [assembly: XmlnsDefinition("http://schemas.apadanasystems.com/xaml/Views", "StandardView.Views")]

Then I created a view that implement from BaseView class and i named it "Notification". Xaml Code of "Notification" view is :

<Views:BaseView
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:Views="http://schemas.apadanasystems.com/xaml/Views"
             x:Class="StandardView.Views.Notification">

 <Views:BaseView.Content>
        <StackLayout BindingContext="{StaticResource ViewModel}">
            ........
        </StackLayout>
    </Views:BaseView.Content>
</Views:BaseView>

and in c# file

public partial class Notification : BaseView
    {
        public Notification()
        {
            InitializeComponent();
        }
    }

Unfortunately code generator for g.cs file could not find base class of xaml page. and namespace definition in xaml must be declared in explicit way as

<Views:BaseView
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:Views="clr-namespace:StandardView.Views;assembly=Infrastructure.View"
             x:Class="StandardView.Views.GLB_VW_Notification">

then code generator can found xaml base class and generate the g.cs file BR

StephaneDelcroix commented 4 years ago

It is known that the XamlGenerator can only lookup for XmlnsDefs in project references, and not in the current one. That feature is thus targeted at 3rd party control provider, more than directly to the users.

we plan to change this, but it will require rewriting XamlG over Roslyn, or Source Generators

ehsangfl commented 3 years ago

yes, but in sample description, base class is not defined in current assembly. base class is on Infrastructure assembly (Infrastructure,View,dll), and xmldef was added to it. and I want to use this baseview on other assembly (like Common.View.dll), with reference of Infrastructure asssembly (Infrastructure,View,dll). I think problem is on code generator of xaml. because if I replace codes handly, (implement g.cs file from baseview) , application run correctly. this means that xamldef define links correctly and code generator cannot generate .g.cs files.