Пример #1
0
        public static ITemplateAction ParseTemplateAction(dynamic dynamicObject)
        {
            var type = (TemplateActionType)System.Enum.Parse(typeof(TemplateActionType), (string)dynamicObject?.type, true);

            return(type switch
            {
                TemplateActionType.Message => MessageTemplateAction.CreateFrom(dynamicObject),
                TemplateActionType.Uri => UriTemplateAction.CreateFrom(dynamicObject),
                TemplateActionType.Postback => PostbackTemplateAction.CreateFrom(dynamicObject),
                TemplateActionType.Datetimepicker => DateTimePickerTemplateAction.CreateFrom(dynamicObject),
                TemplateActionType.Camera => CameraTemplateAction.CreateFrom(dynamicObject),
                TemplateActionType.CameraRoll => CameraRollTemplateAction.CreateFrom(dynamicObject),
                TemplateActionType.Location => LocationTemplateAction.CreateFrom(dynamicObject),
                _ => null,
            });
        public static ITemplateAction ParseTemplateAction(dynamic dynamicObject)
        {
            var type = (TemplateActionType)System.Enum.Parse(typeof(TemplateActionType), (string)dynamicObject?.type, true);

            switch (type)
            {
            case TemplateActionType.Message:
                return(MessageTemplateAction.CreateFrom(dynamicObject));

            case TemplateActionType.Uri:
                return(UriTemplateAction.CreateFrom(dynamicObject));

            case TemplateActionType.Postback:
                return(PostbackTemplateAction.CreateFrom(dynamicObject));

            case TemplateActionType.Datetimepicker:
                return(DateTimePickerTemplateAction.CreateFrom(dynamicObject));

            default:
                return(null);
            }
        }
        public (RichMenu newRichMenu, RenderTargetBitmap bitmap) RenderingRichMenu()
        {
            var body = this.FindName("menu_body") as UIElement;

            if (body == null)
            {
                return(null, null);
            }

            var areas = new List <ActionArea>();

            foreach (var area in Enumerable.Range(1, 20).Select(n => "area_" + n))
            {
                var areaObj = this.FindName(area) as UIElement;
                if (areaObj == null)
                {
                    continue;
                }

                var             menuAction = RichMenuProperties.GetAction(areaObj);
                ITemplateAction action     = null;
                switch (menuAction.Type)
                {
                case TemplateActionType.Message:
                    action = new MessageTemplateAction(menuAction.Label, menuAction.Text);
                    break;

                case TemplateActionType.Postback:
                    action = new PostbackTemplateAction(menuAction.Label, menuAction.Data, menuAction.Text);
                    break;

                case TemplateActionType.Uri:
                    action = new UriTemplateAction(menuAction.Label, menuAction.Uri);
                    break;

                case TemplateActionType.Datetimepicker:
                    action = new DateTimePickerTemplateAction(menuAction.Label, menuAction.Data, menuAction.DateTimeMode,
                                                              menuAction.initialDateTime, menuAction.minDateTime, menuAction.maxDateTime);
                    break;
                }
                if (action == null)
                {
                    continue;
                }
                ;

                var topLeft = areaObj.TransformToVisual(body).Transform(new Point(0, 0));
                areas.Add(new ActionArea()
                {
                    Bounds = new ImageArea(
                        (int)topLeft.X,
                        (int)topLeft.Y,
                        (int)areaObj.RenderSize.Width,
                        (int)areaObj.RenderSize.Height),
                    Action = action
                });
            }

            var details = RichMenuProperties.GetSettings(body);
            var menu    = new RichMenu()
            {
                Name        = details.Name,
                ChatBarText = details.ChatBarText,
                Selected    = details.Selected,
                Size        = new ImageSize((int)body.RenderSize.Width, (int)body.RenderSize.Height),
                Areas       = areas
            };

            var bmp = new RenderTargetBitmap(menu.Size.Width, menu.Size.Height, 96, 96, PixelFormats.Pbgra32);

            bmp.Render(body);

            return(menu, bmp);
        }