Exemple #1
0
        public static GeneratorLayoutElement Convert(ILayoutElement element)
        {
            GeneratorLayoutElement Result;

            if (element is Empty AsEmpty)
            {
                Result = new GeneratorEmpty(AsEmpty);
            }
            else if (element is Control AsControl)
            {
                Result = new GeneratorControl(AsControl);
            }
            else if (element is TextDecoration AsTextDecoration)
            {
                Result = new GeneratorTextDecoration(AsTextDecoration);
            }
            else if (element is Panel AsPanel)
            {
                Result = GeneratorPanel.Convert(AsPanel);
            }
            else
            {
                throw new InvalidOperationException();
            }

            GeneratorLayoutElementMap.Add(element, Result);

            return(Result);
        }
Exemple #2
0
        private void GenerateCSharp(IGeneratorDomain domain, string appNamespace, StreamWriter cSharpWriter)
        {
            cSharpWriter.WriteLine("using System.Collections.Generic;");
            cSharpWriter.WriteLine("using Windows.UI.Xaml;");
            cSharpWriter.WriteLine("using Windows.UI.Xaml.Controls;");
            cSharpWriter.WriteLine("using Windows.UI.Xaml.Controls.Primitives;");
            cSharpWriter.WriteLine();
            cSharpWriter.WriteLine($"namespace {appNamespace}");
            cSharpWriter.WriteLine("{");
            cSharpWriter.WriteLine($"    public partial class {XamlName} : Page, IPageBase");
            cSharpWriter.WriteLine("    {");
            cSharpWriter.WriteLine($"        public {XamlName}()");
            cSharpWriter.WriteLine("        {");
            cSharpWriter.WriteLine("            InitializeComponent();");
            cSharpWriter.WriteLine("            DataContext = this;");

            if (Dynamic.HasProperties)
            {
                cSharpWriter.WriteLine();
                cSharpWriter.WriteLine($"            Dynamic = new {XamlName}Dynamic(this);");
            }

            cSharpWriter.WriteLine("        }");
            cSharpWriter.WriteLine();
            cSharpWriter.WriteLine($"        public PageNames ThisPage {{ get {{ return PageNames.{XamlName}; }} }}");
            cSharpWriter.WriteLine("        public App GetApp { get { return (App)App.Current; } }");

            string ObjectLine = null;

            foreach (IGeneratorObject Object in domain.Objects)
            {
                if (Object.IsGlobal)
                {
                    if (ObjectLine == null)
                    {
                        cSharpWriter.WriteLine();
                    }

                    ObjectLine = $"        public I{Object.CSharpName} Get{Object.CSharpName} {{ get {{ return App.Get{Object.CSharpName}; }} }}";
                    cSharpWriter.WriteLine(ObjectLine);
                }
            }

            if (domain.Translation != null)
            {
                cSharpWriter.WriteLine("        public Translation GetTranslation { get { return App.GetTranslation; } }");
            }

            if (Dynamic.HasProperties)
            {
                cSharpWriter.WriteLine($"        public {XamlName}Dynamic Dynamic {{ get; private set; }}");
            }

            List <Tuple <IGeneratorPageNavigation, IGeneratorObject, IGeneratorObjectPropertyBoolean> > GoToList = new List <Tuple <IGeneratorPageNavigation, IGeneratorObject, IGeneratorObjectPropertyBoolean> >();

            Area.CollectGoTo(GoToList, this);

            foreach (Tuple <IGeneratorPageNavigation, IGeneratorObject, IGeneratorObjectPropertyBoolean> Item in GoToList)
            {
                IGeneratorPageNavigation        GoTo                     = Item.Item1;
                IGeneratorObject                ClosePopupObject         = Item.Item2;
                IGeneratorObjectPropertyBoolean ClosePopupObjectProperty = Item.Item3;
                string GoToCall = GoTo.IsExternal ? "GoToExternal" : "GoTo";

                cSharpWriter.WriteLine();
                cSharpWriter.WriteLine($"        public void {GoTo.EventName}(object sender, RoutedEventArgs e)");
                cSharpWriter.WriteLine("        {");

                if (GoTo.BeforeObject != null && GoTo.BeforeObjectEvent != null)
                {
                    cSharpWriter.WriteLine("            string Content = (sender as Button).Content as string;");

                    if (GoTo.GoToPage == GeneratorPage.AnyPage)
                    {
                        cSharpWriter.WriteLine("            PageNames DestinationPageName;");
                        cSharpWriter.WriteLine("            IObjectBase SenderContext = (IObjectBase)(sender as Button).DataContext;");

                        if (GoTo.BeforeObject.IsGlobal)
                        {
                            cSharpWriter.WriteLine($"            SenderContext.Get{GoTo.BeforeObject.CSharpName}.On_{GoTo.BeforeObjectEvent.CSharpName}(PageNames.{XamlName}, SenderContext, \"{GoTo.Source.Source.Name}\", Content, out DestinationPageName);");
                        }
                        else
                        {
                            cSharpWriter.WriteLine($"            (({GoTo.BeforeObject.CSharpName})SenderContext).On_{GoTo.BeforeObjectEvent.CSharpName}(PageNames.{XamlName}, SenderContext, \"{GoTo.Source.Source.Name}\", Content, out DestinationPageName);");
                        }

                        cSharpWriter.WriteLine($"            (App.Current as App).{GoToCall}(DestinationPageName);");
                    }
                    else
                    {
                        if (GoTo.BeforeObject.IsGlobal)
                        {
                            cSharpWriter.WriteLine("            IObjectBase SenderContext = (IObjectBase)(sender as Button).DataContext;");
                            cSharpWriter.WriteLine($"            SenderContext.Get{GoTo.BeforeObject.CSharpName}.On_{GoTo.BeforeObjectEvent.CSharpName}(PageNames.{XamlName}, \"{GoTo.Source.Source.Name}\", Content);");
                        }
                        else
                        {
                            cSharpWriter.WriteLine($"            {GoTo.BeforeObject.CSharpName} SenderContext = ({GoTo.BeforeObject.CSharpName})(sender as Button).DataContext;");
                            cSharpWriter.WriteLine($"            SenderContext.On_{GoTo.BeforeObjectEvent.CSharpName}(PageNames.{XamlName}, \"{GoTo.Source.Source.Name}\", Content);");
                        }

                        if (GoTo.GoToPage == GeneratorPage.PreviousPage)
                        {
                            cSharpWriter.WriteLine($"            (App.Current as App).{GoToCall}(PageNames.PreviousPage);");
                        }
                        else
                        {
                            cSharpWriter.WriteLine($"            (App.Current as App).{GoToCall}(PageNames.{GoTo.GoToPage.XamlName});");
                        }
                    }
                }
                else if (GoTo.GoToPage == GeneratorPage.PreviousPage)
                {
                    cSharpWriter.WriteLine($"            (App.Current as App).{GoToCall}(PageNames.PreviousPage);");
                }
                else if (GoTo.GoToPage != null)
                {
                    cSharpWriter.WriteLine($"            (App.Current as App).{GoToCall}(PageNames.{GoTo.GoToPage.XamlName});");
                }
                else
                {
                    if (GoTo.GoToObject.IsGlobal)
                    {
                        cSharpWriter.WriteLine($"            (App.Current as App).NavigateToExternal(Get{GoTo.GoToObject.CSharpName}.{GoTo.GoToObjectProperty.CSharpName});");
                    }
                    else
                    {
                        cSharpWriter.WriteLine($"            (App.Current as App).NavigateToExternal((({GoTo.GoToObject.CSharpName})(sender as Button).DataContext).{GoTo.GoToObjectProperty.CSharpName});");
                    }
                }

                if (ClosePopupObject != null && ClosePopupObjectProperty != null)
                {
                    if (ClosePopupObject.IsGlobal)
                    {
                        cSharpWriter.WriteLine($"            if (((IObjectBase)(sender as Button).DataContext).Get{ClosePopupObject.CSharpName}.{ClosePopupObjectProperty.CSharpName})");
                    }
                    else
                    {
                        cSharpWriter.WriteLine($"            if ((({ClosePopupObject.CSharpName})(sender as Button).DataContext).{ClosePopupObjectProperty.CSharpName})");
                    }
                    cSharpWriter.WriteLine("            {");
                    cSharpWriter.WriteLine($"                ClosePopups();");
                    if (ClosePopupObject.IsGlobal)
                    {
                        cSharpWriter.WriteLine($"                ((IObjectBase)(sender as Button).DataContext).Get{ClosePopupObject.CSharpName}.OnPopupClosed_{ClosePopupObjectProperty.CSharpName}();");
                    }
                    else
                    {
                        cSharpWriter.WriteLine($"                (({ClosePopupObject.CSharpName})(sender as Button).DataContext).OnPopupClosed_{ClosePopupObjectProperty.CSharpName}();");
                    }
                    cSharpWriter.WriteLine("            }");
                }

                cSharpWriter.WriteLine("        }");
            }

            if (Dynamic.HasProperties)
            {
                List <IGeneratorBindableComponent> BoundComponentList = new List <IGeneratorBindableComponent>();
                Area.CollectBoundComponents(BoundComponentList, this);

                foreach (IGeneratorBindableComponent Component in BoundComponentList)
                {
                    string ObjectName              = Component.BoundObject.CSharpName;
                    string ObjectPropertyName      = Component.BoundObjectProperty.CSharpName;
                    string HandlerName             = GeneratorComponent.GetLoadedHandlerName(Component.BoundObject, Component.BoundObjectProperty);
                    string HandlerArgumentTypeName = Component.HandlerArgumentTypeName;

                    if (Component is IGeneratorComponentSelector AsSelector)
                    {
                        cSharpWriter.WriteLine();
                        cSharpWriter.WriteLine($"        public void {HandlerName}(object sender, RoutedEventArgs e)");
                        cSharpWriter.WriteLine("        {");
                        cSharpWriter.WriteLine("            ListBox Ctrl = (ListBox)sender;");
                        cSharpWriter.WriteLine($"            {ObjectName} Item = ({ObjectName})Ctrl.DataContext;");
                        cSharpWriter.WriteLine($"            Item.NotifyPropertyChanged(nameof({ObjectName}.{ObjectPropertyName}));");
                        cSharpWriter.WriteLine("        }");
                    }

                    else if (Component is IGeneratorComponentPasswordEdit AsPasswordEdit)
                    {
                        cSharpWriter.WriteLine();
                        cSharpWriter.WriteLine($"        public void {HandlerName}(object sender, RoutedEventArgs e)");
                        cSharpWriter.WriteLine("        {");
                        cSharpWriter.WriteLine("            PasswordBox Ctrl = (PasswordBox)sender;");
                        cSharpWriter.WriteLine($"            string BindingName = $\"{{nameof({ObjectName})}}.{{nameof({ObjectName}.{ObjectPropertyName})}}\";");
                        cSharpWriter.WriteLine("            GetApp.AddPasswordControl(BindingName, Ctrl);");
                        cSharpWriter.WriteLine("        }");
                    }
                }

                foreach (IGeneratorBindableComponent Component in BoundComponentList)
                {
                    string ObjectName              = Component.BoundObject.CSharpName;
                    string ObjectPropertyName      = Component.BoundObjectProperty.CSharpName;
                    string HandlerName             = GeneratorComponent.GetChangedHandlerName(Component.BoundObject, Component.BoundObjectProperty);
                    string HandlerArgumentTypeName = Component.HandlerArgumentTypeName;

                    cSharpWriter.WriteLine();
                    cSharpWriter.WriteLine($"        public void {HandlerName}(object sender, {Component.HandlerArgumentTypeName} e)");
                    cSharpWriter.WriteLine("        {");

                    if (Component is IGeneratorComponentSelector AsSelector)
                    {
                        cSharpWriter.WriteLine("            ListBox Ctrl = (ListBox)sender;");
                        cSharpWriter.WriteLine($"            {ObjectName} Item = ({ObjectName})Ctrl.DataContext;");
                        cSharpWriter.WriteLine($"            if (Ctrl.SelectedIndex >= 0 && Item.{ObjectPropertyName} != Ctrl.SelectedIndex)");
                        cSharpWriter.WriteLine("            {");
                        cSharpWriter.WriteLine($"                Item.{ObjectPropertyName} = Ctrl.SelectedIndex;");
                        cSharpWriter.WriteLine($"                Item.NotifyPropertyChanged(nameof({ObjectName}.{ObjectPropertyName}));");
                        cSharpWriter.WriteLine("            }");
                        cSharpWriter.WriteLine();
                    }

                    string Notification = $"Dynamic.OnPropertyChanged($\"{{nameof({ObjectName})}}.{{nameof({ObjectName}.{ObjectPropertyName})}}\")";
                    if (Component.PostponeChangedNotification)
                    {
                        cSharpWriter.WriteLine($"            Dispatcher.BeginInvoke( () => {Notification} );");
                    }
                    else
                    {
                        cSharpWriter.WriteLine($"            {Notification};");
                    }

                    cSharpWriter.WriteLine("        }");
                }
            }

            Dictionary <IGeneratorPage, string> LinkedPageTable = new Dictionary <IGeneratorPage, string>();

            foreach (KeyValuePair <ILayoutElement, IGeneratorLayoutElement> Entry in GeneratorLayoutElement.GeneratorLayoutElementMap)
            {
                if (Entry.Value is IGeneratorTextDecoration AsTextDecoration)
                {
                    foreach (object LinkEntry in AsTextDecoration.LinkedPageList)
                    {
                        IGeneratorPage LinkedPage = (IGeneratorPage)LinkEntry;
                        if (!LinkedPageTable.ContainsKey(LinkedPage))
                        {
                            LinkedPageTable.Add(LinkedPage, GeneratorTextDecoration.ToEventHandlerName(LinkedPage));
                        }
                    }
                }
            }

            foreach (KeyValuePair <IGeneratorPage, string> LinkEntry in LinkedPageTable)
            {
                cSharpWriter.WriteLine();
                cSharpWriter.WriteLine($"        private void {LinkEntry.Value}(object sender, RoutedEventArgs e)");
                cSharpWriter.WriteLine("        {");
                cSharpWriter.WriteLine($"            (App.Current as App).GoTo(PageNames.{LinkEntry.Key.XamlName});");
                cSharpWriter.WriteLine("        }");
            }

            cSharpWriter.WriteLine();
            cSharpWriter.WriteLine("        private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)");
            cSharpWriter.WriteLine("        {");
            cSharpWriter.WriteLine("            (App.Current as App).OnIsEnabledChanged(sender, e);");
            cSharpWriter.WriteLine("        }");
            cSharpWriter.WriteLine();
            cSharpWriter.WriteLine("        private List<ToggleButton> ToggleList = new List<ToggleButton>();");
            cSharpWriter.WriteLine();
            cSharpWriter.WriteLine("        private void OnToggleLoaded(object sender, RoutedEventArgs e)");
            cSharpWriter.WriteLine("        {");
            cSharpWriter.WriteLine("            if ((sender is ToggleButton AsToggle) && !ToggleList.Contains(AsToggle))");
            cSharpWriter.WriteLine("                ToggleList.Add(AsToggle);");
            cSharpWriter.WriteLine("        }");
            cSharpWriter.WriteLine();
            cSharpWriter.WriteLine("        private void OnPointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)");
            cSharpWriter.WriteLine("        {");
            cSharpWriter.WriteLine("            ClosePopups();");
            cSharpWriter.WriteLine("        }");
            cSharpWriter.WriteLine();
            cSharpWriter.WriteLine("        private void ClosePopups()");
            cSharpWriter.WriteLine("        {");
            cSharpWriter.WriteLine("            foreach (ToggleButton Toggle in ToggleList)");
            cSharpWriter.WriteLine("                Toggle.IsChecked = false;");
            cSharpWriter.WriteLine("        }");
            cSharpWriter.WriteLine("    }");
            cSharpWriter.WriteLine("}");
        }