Example #1
0
        internal static MenuItem CreateAddFromTemplateButton(Type context, Action <object> add)
        {
            MenuItem b = new MenuItem()
            {
                Header = LocalizationManager.Current.Interface["Control_AddFromTemplate"]
            };

            b.Click += new RoutedEventHandler((sender, e) =>
            {
                OpenFileDialog ofd = new OpenFileDialog()
                {
                    Filter      = $"{LocalizationManager.Current.General["Project_TemplateFilter"]}|*.npctemplate",
                    Multiselect = false
                };
                if (ofd.ShowDialog() == true)
                {
                    try
                    {
                        var template = TemplateManager.LoadTemplate(ofd.FileName);

                        if (template != null)
                        {
                            if (TemplateManager.IsCorrectContext(template, context))
                            {
                                TemplateManager.PrepareTemplate(template);

                                if (template.Inputs.Count > 0)
                                {
                                    TemplateManager.AskForInput(template);
                                }

                                var result = TemplateManager.ApplyTemplate(template);

                                add.Invoke(result);
                            }
                            else
                            {
                                App.NotificationManager.Notify(LocalizationManager.Current.Notification.Translate("Template_InvalidContext"));
                            }
                        }
                        else
                        {
                            App.NotificationManager.Notify(LocalizationManager.Current.Notification.Translate("Template_InvalidFile"));
                        }
                    }
                    catch (Exception ex)
                    {
                        App.NotificationManager.Notify(LocalizationManager.Current.Notification.Translate("Template_Error"));
                        App.Logger.LogException("Could not add item from template", ex: ex);
                    }
                }
            });
            b.Icon = new PackIconMaterial()
            {
                Kind = PackIconMaterialKind.FolderPlusOutline
            };
            (b.Icon as PackIconMaterial).SetResourceReference(PackIconMaterial.ForegroundProperty, "AccentColor");
            return(b);
        }