Beispiel #1
0
        public static void InitializeRenderer(AdaptiveHostConfig hostConfig)
        {
            try
            {
                _renderer = new AdaptiveCardRenderer();
                if (hostConfig != null)
                {
                    _renderer.HostConfig = hostConfig;
                }

                if (Settings.UseFixedDimensions)
                {
                    _renderer.SetFixedDimensions(320, 180);
                }

                // Custom resource resolvers
                _renderer.ResourceResolvers.Set("symbol", new MySymbolResourceResolver());
            }
            catch
            {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
                throw;
            }
        }
        public static void InitializeRenderer(AdaptiveHostConfig hostConfig)
        {
            _renderer = new AdaptiveCardRenderer();
            if (hostConfig != null)
            {
                _renderer.HostConfig = hostConfig;
            }

            // Add a feature representing this version of the visualizer. used for test cards.
            _renderer.FeatureRegistration.Set("acTest", "1.0");

            if (Settings.UseFixedDimensions)
            {
                _renderer.SetFixedDimensions(320, 180);
            }

            // Custom resource resolvers
            _renderer.ResourceResolvers.Set("symbol", new MySymbolResourceResolver());

            /*
             * Example on how to override the Action Positive and Destructive styles
             * Style positiveStyle = new Style(typeof(Button));
             * positiveStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.LawnGreen)));
             * Style destructiveStyle = new Style(typeof(Button));
             * destructiveStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Red)));
             * Style otherStyle = new Style(typeof(Button));
             * otherStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Yellow)));
             * otherStyle.Setters.Add(new Setter(Button.ForegroundProperty, new SolidColorBrush(Windows.UI.Colors.DarkRed)));
             *
             * _renderer.OverrideStyles = new ResourceDictionary();
             * _renderer.OverrideStyles.Add("Adaptive.Action.Positive", positiveStyle);
             * _renderer.OverrideStyles.Add("Adaptive.Action.Destructive", destructiveStyle);
             * _renderer.OverrideStyles.Add("Adaptive.Action.other", otherStyle);
             */
        }
        private async void TriggerUpdate()
        {
            if (_isUpdating)
            {
                return;
            }

            _isUpdating = true;

            try
            {
                if (_renderer == null)
                {
                    _renderer = new AdaptiveCardRenderer();
                    _renderer.SetFixedDimensions(320, 176);

                    var hostConfigsFolder = await Package.Current.InstalledLocation.GetFolderAsync("HostConfigs");

                    var hostConfigFile = await hostConfigsFolder.GetFileAsync("TimelineWindows.json");

                    _renderer.HostConfig = AdaptiveHostConfig.FromJsonString(await FileIO.ReadTextAsync(hostConfigFile)).HostConfig;
                }

                if (CardPayload == null)
                {
                    RenderedElement      = null;
                    RenderedSuccessfully = false;
                }
                else
                {
                    var renderResult = _renderer.RenderAdaptiveCardFromJsonString(CardPayload.ToString());
                    RenderedElement      = renderResult.FrameworkElement;
                    RenderedSuccessfully = RenderedElement != null;
                }
            }
            catch
            {
                RenderedElement      = null;
                RenderedSuccessfully = false;
            }
            finally
            {
                _isUpdating = false;
            }
        }
        public static void InitializeRenderer(AdaptiveHostConfig hostConfig)
        {
            try
            {
                _renderer = new AdaptiveCardRenderer();
                if (hostConfig != null)
                {
                    _renderer.HostConfig = hostConfig;
                }

                if (Settings.UseFixedDimensions)
                {
                    _renderer.SetFixedDimensions(320, 180);
                }

                // Custom resource resolvers
                _renderer.ResourceResolvers.Set("symbol", new MySymbolResourceResolver());

                /*
                 * Example on how to override the Action Positive and Destructive styles
                 * Style positiveStyle = new Style(typeof(Button));
                 * positiveStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.LawnGreen)));
                 * Style destructiveStyle = new Style(typeof(Button));
                 * destructiveStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Red)));
                 * Style otherStyle = new Style(typeof(Button));
                 * otherStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Yellow)));
                 * otherStyle.Setters.Add(new Setter(Button.ForegroundProperty, new SolidColorBrush(Windows.UI.Colors.DarkRed)));
                 *
                 * _renderer.OverrideStyles = new ResourceDictionary();
                 * _renderer.OverrideStyles.Add("Adaptive.Action.Positive", positiveStyle);
                 * _renderer.OverrideStyles.Add("Adaptive.Action.Destructive", destructiveStyle);
                 * _renderer.OverrideStyles.Add("Adaptive.Action.other", otherStyle);
                 */
            }
            catch
            {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
                throw;
            }
        }
Beispiel #5
0
        public static async Task <RenderedTestResult> RenderCard(FileViewModel cardFile, FileViewModel hostConfigFile, Dictionary <string, IAdaptiveCardResourceResolver> resourceResolvers)
        {
            string           error = null;
            string           roundTrippedJsonString = null;
            FrameworkElement xaml        = null;
            double           cardWidth   = 400;
            WeakReference    weakRefCard = null;

            try
            {
                AdaptiveHostConfig hostConfig = AdaptiveHostConfig.FromJsonString(hostConfigFile.Contents).HostConfig;

                if (hostConfig == null)
                {
                    error = "Parsing hostConfig failed";
                }

                else
                {
                    AdaptiveCard card = AdaptiveCard.FromJsonString(cardFile.Contents).AdaptiveCard;

                    if (card == null)
                    {
                        error = "Parsing card failed";
                    }

                    else
                    {
                        roundTrippedJsonString = card.ToJson().ToString();
                        card = AdaptiveCard.FromJsonString(roundTrippedJsonString).AdaptiveCard;

                        AdaptiveFeatureRegistration featureRegistration = new AdaptiveFeatureRegistration();
                        featureRegistration.Set("acTest", "1.0");

                        var renderer = new AdaptiveCardRenderer()
                        {
                            HostConfig          = hostConfig,
                            FeatureRegistration = featureRegistration
                        };

                        foreach (var resourceResolver in resourceResolvers)
                        {
                            renderer.ResourceResolvers.Set(resourceResolver.Key, resourceResolver.Value);
                        }

                        if (hostConfigFile.Name.Contains("windows-timeline"))
                        {
                            renderer.SetFixedDimensions(320, 180);
                            cardWidth = 320;
                        }
                        else if (hostConfigFile.Name.Contains("windows-live-tile"))
                        {
                            renderer.SetFixedDimensions(310, 310);
                            cardWidth = 310;
                        }

                        RenderedAdaptiveCard renderedCard = renderer.RenderAdaptiveCard(card);
                        weakRefCard = new WeakReference(renderedCard);

                        xaml = renderedCard.FrameworkElement as FrameworkElement;

                        if (xaml == null)
                        {
                            error = "Rendering card failed";
                        }

                        else
                        {
                            xaml = new Border()
                            {
                                Background       = new SolidColorBrush(Colors.White),
                                Child            = xaml,
                                IsHitTestVisible = false // Disable HitTest so that mouse pointer can't accidently hover over a button
                            };

                            // The theme is important to set since it'll ensure buttons/inputs appear correctly
                            if (hostConfigFile.Name.Contains("windows-notification"))
                            {
                                xaml.RequestedTheme = ElementTheme.Dark;
                            }
                            else
                            {
                                xaml.RequestedTheme = ElementTheme.Light;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }

            return(new RenderedTestResult
            {
                Error = error,
                RoundTrippedJSON = roundTrippedJsonString,
                Tree = xaml,
                CardWidth = cardWidth,
                WeakCard = weakRefCard
            });
        }
Beispiel #6
0
        private async Task <Tuple <string, StorageFile, StorageFile, UIElement> > RenderCard(FileViewModel cardFile, FileViewModel hostConfigFile)
        {
            string             error = null;
            RenderTargetBitmap rtb   = null;
            string             roundTrippedJsonString = null;

            try
            {
                AdaptiveHostConfig hostConfig = AdaptiveHostConfig.FromJsonString(hostConfigFile.Contents).HostConfig;

                if (hostConfig == null)
                {
                    error = "Parsing hostConfig failed";
                }

                else
                {
                    AdaptiveCard card = AdaptiveCard.FromJsonString(cardFile.Contents).AdaptiveCard;

                    if (card == null)
                    {
                        error = "Parsing card failed";
                    }

                    else
                    {
                        roundTrippedJsonString = card.ToJson().ToString();
                        card = AdaptiveCard.FromJsonString(roundTrippedJsonString).AdaptiveCard;

                        var renderer = new AdaptiveCardRenderer()
                        {
                            HostConfig = hostConfig
                        };

                        if (hostConfigFile.Name.Contains("windows-timeline"))
                        {
                            renderer.SetFixedDimensions(320, 180);
                            CurrentCardVisualWidth = 320;
                        }
                        else if (hostConfigFile.Name.Contains("windows-live-tile"))
                        {
                            renderer.SetFixedDimensions(310, 310);
                            CurrentCardVisualWidth = 310;
                        }
                        else
                        {
                            CurrentCardVisualWidth = 400;
                        }

                        FrameworkElement xaml = renderer.RenderAdaptiveCard(card).FrameworkElement as FrameworkElement;

                        if (xaml == null)
                        {
                            error = "Rendering card failed";
                        }

                        else
                        {
                            xaml = new Border()
                            {
                                Background       = new SolidColorBrush(Colors.White),
                                Child            = xaml,
                                IsHitTestVisible = false // Disable HitTest so that mouse pointer can't accidently hover over a button
                            };

                            // The theme is important to set since it'll ensure buttons/inputs appear correctly
                            if (hostConfigFile.Name.Contains("windows-notification"))
                            {
                                xaml.RequestedTheme = ElementTheme.Dark;
                            }
                            else
                            {
                                xaml.RequestedTheme = ElementTheme.Light;
                            }

                            CurrentCardVisual = xaml;

                            ExpandShowCards(xaml);
                            NormalizeTimePickers(xaml);
                            await WaitOnAllImagesAsync(xaml);

                            rtb = new RenderTargetBitmap();
                            await rtb.RenderAsync(CurrentCardVisual);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }

            StorageFile file  = null;
            StorageFile file2 = null;

            if (error == null)
            {
                file = await _tempResultsFolder.CreateFileAsync("Result.png", CreationCollisionOption.GenerateUniqueName);

                file2 = await _tempResultsFolder.CreateFileAsync("Result.json", CreationCollisionOption.GenerateUniqueName);

                // https://basquang.wordpress.com/2013/09/26/windows-store-8-1-save-visual-element-to-bitmap-image-file/
                var buffer = await rtb.GetPixelsAsync();

                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, 96, 96, buffer.ToArray());

                    // Set the size of the card so that it can be rendered just like the bitmap
                    if (CurrentCardVisual is FrameworkElement fe)
                    {
                        fe.Width  = rtb.PixelWidth;
                        fe.Height = rtb.PixelHeight;
                    }

                    await encoder.FlushAsync();
                }

                if (roundTrippedJsonString != null)
                {
                    await Windows.Storage.FileIO.WriteTextAsync(file2, roundTrippedJsonString);
                }
            }

            return(new Tuple <string, StorageFile, StorageFile, UIElement>(error, file, file2, CurrentCardVisual));
        }
        public static async Task <Tuple <string, string, UIElement, double> > RenderCard(FileViewModel cardFile, FileViewModel hostConfigFile)
        {
            string           error = null;
            string           roundTrippedJsonString = null;
            FrameworkElement xaml      = null;
            double           cardWidth = 400;

            try
            {
                AdaptiveHostConfig hostConfig = AdaptiveHostConfig.FromJsonString(hostConfigFile.Contents).HostConfig;

                if (hostConfig == null)
                {
                    error = "Parsing hostConfig failed";
                }

                else
                {
                    AdaptiveCard card = AdaptiveCard.FromJsonString(cardFile.Contents).AdaptiveCard;

                    if (card == null)
                    {
                        error = "Parsing card failed";
                    }

                    else
                    {
                        roundTrippedJsonString = card.ToJson().ToString();
                        card = AdaptiveCard.FromJsonString(roundTrippedJsonString).AdaptiveCard;

                        var renderer = new AdaptiveCardRenderer()
                        {
                            HostConfig = hostConfig
                        };

                        if (hostConfigFile.Name.Contains("windows-timeline"))
                        {
                            renderer.SetFixedDimensions(320, 180);
                            cardWidth = 320;
                        }
                        else if (hostConfigFile.Name.Contains("windows-live-tile"))
                        {
                            renderer.SetFixedDimensions(310, 310);
                            cardWidth = 310;
                        }

                        xaml = renderer.RenderAdaptiveCard(card).FrameworkElement as FrameworkElement;

                        if (xaml == null)
                        {
                            error = "Rendering card failed";
                        }

                        else
                        {
                            xaml = new Border()
                            {
                                Background       = new SolidColorBrush(Colors.White),
                                Child            = xaml,
                                IsHitTestVisible = false // Disable HitTest so that mouse pointer can't accidently hover over a button
                            };

                            // The theme is important to set since it'll ensure buttons/inputs appear correctly
                            if (hostConfigFile.Name.Contains("windows-notification"))
                            {
                                xaml.RequestedTheme = ElementTheme.Dark;
                            }
                            else
                            {
                                xaml.RequestedTheme = ElementTheme.Light;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }

            return(new Tuple <string, string, UIElement, double>(error, roundTrippedJsonString, xaml, cardWidth));
        }
Beispiel #8
0
        public static async Task <RenderedTestResult> RenderCard(FileViewModel cardFile, FileViewModel hostConfigFile)
        {
            string           error = null;
            string           roundTrippedJsonString = null;
            FrameworkElement xaml        = null;
            double           cardWidth   = 400;
            WeakReference    weakRefCard = null;

            try
            {
                AdaptiveHostConfig hostConfig = null;
                if (hostConfigFile.Contents != null)
                {
                    hostConfig = AdaptiveHostConfig.FromJsonString(hostConfigFile.Contents).HostConfig;

                    if (hostConfig == null)
                    {
                        error = "Parsing hostConfig failed";
                    }
                }

                if (error == null)
                {
                    AdaptiveCard card = AdaptiveCard.FromJsonString(cardFile.Contents).AdaptiveCard;

                    if (card == null)
                    {
                        error = "Parsing card failed";
                    }

                    else
                    {
                        roundTrippedJsonString = card.ToJson().ToString();
                        card = AdaptiveCard.FromJsonString(roundTrippedJsonString).AdaptiveCard;

                        AdaptiveFeatureRegistration featureRegistration = new AdaptiveFeatureRegistration();
                        featureRegistration.Set("acTest", "1.0");

                        var renderer = new AdaptiveCardRenderer()
                        {
                            FeatureRegistration = featureRegistration
                        };

                        if (hostConfig != null)
                        {
                            renderer.HostConfig = hostConfig;
                        }

                        renderer.ResourceResolvers.Set("symbol", new SampleResourceResolver());

                        if (hostConfigFile.Name.Contains(FileLoadHelpers.fixedNonInteractiveName))
                        {
                            renderer.SetFixedDimensions(320, 180);
                            cardWidth = 320;

                            renderer.HostConfig.SupportsInteractivity = false;
                        }

                        RenderedAdaptiveCard renderedCard = renderer.RenderAdaptiveCard(card);
                        weakRefCard = new WeakReference(renderedCard);

                        xaml = renderedCard.FrameworkElement as FrameworkElement;

                        if (xaml == null)
                        {
                            error = "Rendering card failed";
                        }

                        else
                        {
                            xaml = new Border()
                            {
                                Background       = new SolidColorBrush(Colors.White),
                                Child            = xaml,
                                IsHitTestVisible = false // Disable HitTest so that mouse pointer can't accidently hover over a button
                            };

                            // The theme is important to set since it'll ensure buttons/inputs appear correctly
                            if (hostConfigFile.Name.Contains(FileLoadHelpers.testVarientHostConfigName))
                            {
                                xaml.RequestedTheme = ElementTheme.Dark;
                            }
                            else
                            {
                                xaml.RequestedTheme = ElementTheme.Light;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }

            return(new RenderedTestResult
            {
                Error = error,
                RoundTrippedJSON = roundTrippedJsonString,
                Tree = xaml,
                CardWidth = cardWidth,
                WeakCard = weakRefCard
            });
        }