Esempio n. 1
0
        protected override void OnExecute(XenMessageContext ctx)
        {
            var request = ctx.Get <DeleteWidgetRequest>();

            if (request == null)
            {
                return;
            }

            var pair = Surface[request.WidgetId];

            if (pair == null)
            {
                return;
            }

            if (!pair.XenWidget.CanDelete)
            {
                return;
            }

            DesignThread.Invoke(() =>
            {
                Surface.Remove(request.WidgetId);
            });

            ctx.SetResponse <OkResponse>(r =>
            {
                r.Suggest <GetVisualTreeRequest>();
            });
        }
Esempio n. 2
0
        protected override void OnExecute(XenMessageContext ctx)
        {
            var request = ctx.Get <LoadProjectRequest>();

            if (request == null)
            {
                return;
            }

            try
            {
                Assembly assembly;
                var      data = Convert.FromBase64String(request.AssemblyData);
                Loader.Load(data, out assembly);
            }
            catch (Exception e)
            {
                DesignThread.Invoke(() =>
                {
                    App.ConnectionPage.ShowError(e);
                });
            }
        }
Esempio n. 3
0
        protected override void OnExecute(XenMessageContext ctx)
        {
            var request = ctx.Get <CreateWidgetRequest>();

            if (request == null)
            {
                return;
            }

            var viewType = TypeFinder.Find(request.TypeName);

            if (viewType == null)
            {
                DesignThread.Invoke(() =>
                {
                    App.ConnectionPage.ShowError($"Unable to create {request.TypeName}. Have you loaded the project assemblies?");
                });

                return;
            }

            var view = Activator.CreateInstance(viewType) as View;

            if (view == null)
            {
                DesignThread.Invoke(() =>
                {
                    App.ConnectionPage.ShowError($"Error instantiating {request.TypeName}.");
                });

                return;
            }

            var target = Surface[request.ParentId];

            if (target == null)
            {
                return;
            }

            var attached = false;

            DesignThread.Invoke(() =>
            {
                attached = Surface.SetParent(view, target);
            });

            if (!attached)
            {
                return;
            }

            var pair = Surface[view.Id];

            var xamlDefaults = string.Empty;

            try
            {
                var newView = Activator.CreateInstance(viewType) as View;
                xamlDefaults = XamlWriter.Save(newView);
            }
            catch (Exception)
            {
                // ignored
            }

            ctx.SetResponse <CreateWidgetResponse>(r =>
            {
                r.XamlDefaults = new[] { xamlDefaults };
                r.Widget       = pair.XenWidget;
                r.Parent       = target.XenWidget;
                r.Suggest <GetVisualTreeRequest>();
            });
        }
Esempio n. 4
0
        protected void SetPropertyValue(string rWidgetId, string[] rPath, object rValue, bool rIsBase64, bool rIsAttachedProperty)
        {
            var ignoreSet      = false;
            var targetPropName = XenProperty.GetLastPath(rPath);
            var pair           = Surface[rWidgetId];

            AttachedPropertyInfo  targetAttachedProp = null;
            XenReflectionProperty targetProp;

            if (rIsAttachedProperty)
            {
                var apAncestors = ElementHelper.GetParents(pair.XenWidget);
                var apInfos     = new List <AttachedPropertyInfo>();

                foreach (var ancestor in apAncestors)
                {
                    var aView = Surface[ancestor.Id];
                    if (aView == null)
                    {
                        continue;
                    }
                    apInfos.AddRange(ElementHelper.GetAttachedProperties(aView.VisualElement));
                }

                if (apInfos.Count == 0)
                {
                    return;
                }
                targetAttachedProp = apInfos.LastOrDefault(a => a.PropertyName == targetPropName);
                if (targetAttachedProp == null)
                {
                    return;
                }

                object apParent      = null;
                object apGrandParent = null;

                if (apAncestors.ElementAtOrDefault(0) != null)
                {
                    apParent = Surface[apAncestors[0].Id].VisualElement;
                }

                if (apAncestors.ElementAtOrDefault(1) != null)
                {
                    apGrandParent = Surface[apAncestors[1].Id].VisualElement;
                }

                targetProp = targetAttachedProp.Convert(apParent, apGrandParent);
            }
            else
            {
                targetProp = pair?
                             .VisualElement?
                             .GetXenRefProperties(rPath)
                             .FirstOrDefault();

                if (targetProp == null)
                {
                    return;
                }
            }

            if (targetPropName != null && targetPropName.Contains("Color"))
            {
                rValue = Color.FromHex(rValue.ToString());
            }

            // todo: make into a serializer
            if (targetPropName != null && targetPropName.Contains("Source"))
            {
                if (!rIsBase64)
                {
                    return;
                }
                var bytes = Convert.FromBase64String(rValue.ToString());

                var src = new XenImageSource(ImageSource.FromStream(() => new MemoryStream(bytes)))
                {
                    FileName = _req.Metadata?.ToString()
                };

                rValue = src;
            }

            // get enumeration value
            if (targetProp.IsTargetEnum)
            {
                var eval = ReflectionMethods.CreateEnum(targetProp.TargetType, rValue);

                if (eval == null)
                {
                    ignoreSet = true;
                }
                else
                {
                    rValue = eval;
                }
            }

            if (!ignoreSet)
            {
                DesignThread.Invoke(() =>
                {
                    // if target property is part of a structure and we're modifying its properties
                    // 1) make a copy of the current value
                    // 2) change the property value
                    // 3) reassign it
                    if (ReflectionMethods.IsValueType(targetProp.ParentObject))
                    {
                        var childProp = targetProp
                                        .ParentObject
                                        .GetXenRefProperties()
                                        .FirstOrDefault(p => p.TargetName == targetPropName);

                        childProp?.SetTargetObject(rValue);
                        var copy = childProp?.ParentObject;

                        var parentInfo = targetProp
                                         .GrandParentObject?
                                         .GetType()
                                         .GetProperty(targetProp.ParentName);

                        parentInfo?.SetValue(targetProp.GrandParentObject, copy);

                        // supporting View -> Struct -> Struct -> Target (valuetype or ref)
                        if (ReflectionMethods.IsValueType(targetProp.GrandParentObject))
                        {
                            if (rPath.Length == 3)
                            {
                                targetProp
                                .GrandParentObject?
                                .GetType()
                                .GetProperty(targetProp.ParentName)
                                .SetValue(targetProp.GrandParentObject, targetProp.ParentObject);

                                pair.VisualElement
                                .GetType()
                                .GetProperty(rPath[0])
                                .SetValue(pair.VisualElement, targetProp.GrandParentObject);
                            }
                        }
                    }
                    // this is assinging a field
                    else if (targetProp.IsTargetStruct && rValue is string)
                    {
                        var fieldName = rValue.ToString() ?? string.Empty;
                        var fieldInfo = targetProp.TargetType.GetStaticFields().FirstOrDefault(f => f.Name == fieldName);

                        if (fieldInfo == null)
                        {
                            if (rIsAttachedProperty)
                            {
                                SetAttachedProperty(rValue, targetAttachedProp, pair);
                            }
                            else
                            {
                                targetProp.SetTargetObject(rValue);
                            }
                        }
                        else
                        {
                            var copy = fieldInfo.GetValue(null);

                            if (rIsAttachedProperty)
                            {
                                SetAttachedProperty(copy, targetAttachedProp, pair);
                            }
                            else
                            {
                                targetProp.SetTargetObject(copy);
                            }
                        }
                    }
                    else
                    {
                        // one last attempt
                        if (rIsAttachedProperty)
                        {
                            SetAttachedProperty(rValue, targetAttachedProp, pair);
                        }
                        else
                        {
                            targetProp.SetTargetObject(rValue);
                        }
                    }
                });
            }
        }
Esempio n. 5
0
        protected override void OnExecute(XenMessageContext ctx)
        {
            var req = ctx.Get <CreateGridRequest>();

            if (req == null)
            {
                return;
            }

            var target = Surface[req.ParentId];

            if (target == null)
            {
                return;
            }

            var attached      = false;
            var rowCollection = new RowDefinitionCollection();
            var colCollection = new ColumnDefinitionCollection();

            for (var i = 0; i < req.Rows; i++)
            {
                var row = new RowDefinition();
                rowCollection.Add(row);
            }

            for (var j = 0; j < req.Columns; j++)
            {
                var col = new ColumnDefinition();
                colCollection.Add(col);
            }

            var view = new Grid
            {
                RowDefinitions    = rowCollection,
                ColumnDefinitions = colCollection,
                ColumnSpacing     = req.ColumnSpacing,
                RowSpacing        = req.RowSpacing
            };

            DesignThread.Invoke(() =>
            {
                attached = Surface.SetParent(view, target);
            });

            if (!attached)
            {
                return;
            }

            var pair = Surface[view.Id];

            var xamlDefaults = string.Empty;

            try
            {
                var newGrid = new Grid();
                xamlDefaults = XamlWriter.Save(newGrid);
            }
            catch (Exception)
            {
                // ignored
            }

            ctx.SetResponse <CreateWidgetResponse>(res =>
            {
                res.XamlDefaults = new [] { xamlDefaults };
                res.Widget       = pair.XenWidget;
                res.Parent       = target.XenWidget;
                res.Suggest <GetVisualTreeRequest>();
            });
        }
Esempio n. 6
0
        protected override void OnExecute(XenMessageContext ctx)
        {
            var request = ctx.Get <CreateStackLayoutRequest>();

            if (request == null)
            {
                return;
            }

            var orientation = StackOrientation.Vertical;

            if (string.IsNullOrWhiteSpace(request.Orientation) || request.Orientation.ToLower() == "vertical")
            {
                orientation = StackOrientation.Vertical;
            }
            else if (request.Orientation.ToLower() == "horizontal")
            {
                orientation = StackOrientation.Horizontal;
            }

            var view = new StackLayout
            {
                Orientation = orientation,
                Spacing     = request.Spacing
            };

            var target = Surface[request.ParentId];

            if (target == null)
            {
                return;
            }

            var attached = false;

            DesignThread.Invoke(() =>
            {
                attached = Surface.SetParent(view, target);
            });

            if (!attached)
            {
                return;
            }

            var pair = Surface[view.Id];

            var xamlDefaults = string.Empty;

            try
            {
                xamlDefaults = XamlWriter.Save(new StackLayout());
            }
            catch (Exception)
            {
                // ignored
            }

            ctx.SetResponse <CreateWidgetResponse>(r =>
            {
                r.XamlDefaults = new[] { xamlDefaults };
                r.Widget       = pair.XenWidget;
                r.Parent       = target.XenWidget;
                r.Suggest <GetVisualTreeRequest>();
            });
        }