alternetsoft / AlternetUI

MIT License
24 stars 2 forks source link

UIXml should be fully converted to c# code, not good to read Window props from Xml at runtime #26

Open generalloki opened 1 year ago

generalloki commented 1 year ago

This is an example from TabControlPage.g.cs which is generated from UiXml Section 1 and Section 2 generatyion should be rewitten

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public void InitializeComponent()
        {
            if (Alternet.UI.UixmlLoader.DisableComponentInitialization)
                return;            
            if (contentLoaded)
                return;            
            contentLoaded = true;
//SECTION 1
            var uixmlStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TabControlPage.uixml");
            if (uixmlStream == null)
                throw new InvalidOperationException();            
            new Alternet.UI.UixmlLoader().LoadExisting(uixmlStream, this);
//SECTION 2
            tabControl = (Alternet.UI.TabControl)FindElement("tabControl");
            modifyFirstPageTitleButton = (Alternet.UI.Button)FindElement("modifyFirstPageTitleButton");
            InsertLastPageSiblingButton = (Alternet.UI.Button)FindElement("InsertLastPageSiblingButton");
            removeSelectedPageButton = (Alternet.UI.Button)FindElement("removeSelectedPageButton");
            appendPageButton = (Alternet.UI.Button)FindElement("appendPageButton");
            clearPagesButton = (Alternet.UI.Button)FindElement("clearPagesButton");
            tabAlignmentComboBox = (Alternet.UI.ComboBox)FindElement("tabAlignmentComboBox");
            tabControl.SelectedPageChanged += TabControl_SelectedPageChanged;

Also example from WebBrowserPage.g.cs: Here we have assignment of events to controls with empty names

            ((Alternet.UI.Button)(ContentElements[0].ContentElements[1].ContentElements[5])).Click += GoButton_Click;
            ((Alternet.UI.Button)(ContentElements[0].ContentElements[2].ContentElements[1])).Click += FindButton_Click;

I beleive we need to have private fields with autogenerated control declaration and generate code like this:

Alternet.UI.Button contentElement_button1;
Alternet.UI.Button contentElement_button2;
            contentElement_button1.Click += GoButton_Click;
            contentElement_button2.Click += FindButton_Click;
generalloki commented 1 year ago

uixml is compiled to в c# ( g.cs) partially. Might need to create C# code with property assignments:

<TextBox Text="Test Button" 
Name="textTextBox" Margin="0,10,0,0"
TextChanged="TextTextBox_TextChanged"
/>

generated code:

TextBox textTextBox=new TextBox();
textTextBox.Text="Test Button";
textTextBox.Margin= new Thickness("0,10,0,0");
generalloki commented 1 year ago

UiXml Loader uses TryFindElement for all named elements. And it is slow recursively searches through all the controls. We probably need to rewrite this methods to use Dictionary

        public FrameworkElement? TryFindElement(string name)
        {
            if (name is null)
                throw new ArgumentNullException(nameof(name));

            if (Name == name)
                return this;

            foreach (var child in LogicalChildrenCollection)
            {
                var result = child.TryFindElement(name);
                if (result != null)
                    return result;
            }

            return null;
        }
generalloki commented 1 year ago

One more bug in UiXml reader:

Button without name with OnClick event in TabPage Shows error during UiXml Reading