Exemple #1
0
        public void PrintViewHierarchy(FrameworkElement c, StringBuilder sb, int level = 0)
        {
            var children = c.GetChildren().ToImmutableArray();

            for (int i = 0; i < children.Length; i++)
            {
                var v        = children[i];
                var vElement = (FrameworkElement)v;
                var desc     = string.Concat(Enumerable.Repeat("    |", level)) + $" [{i + 1}/{children.Length}] {v.GetType().Name}";
                if (vElement != null)
                {
                    desc += $" -- ActualHeight:{vElement.ActualHeight}, ActualWidth:{vElement.ActualWidth}, Height:{vElement.Height}, Width:{vElement.Width}, DataContext:{vElement.DataContext?.GetType().FullName}";
                    var vTextBlock = vElement as TextBlock;
                    if (vTextBlock != null)
                    {
                        desc += $", Text: {vTextBlock.Text}";
                    }
                }

                sb.AppendLine(desc);
                var childViewGroup = v as Control;
                if (childViewGroup != null)
                {
                    PrintViewHierarchy(childViewGroup, sb, level + 1);
                }
            }
        }
Exemple #2
0
        private static async Task GenerateBitmap(CancellationToken ct, string folderName, string fileName, FrameworkElement content)
        {
            var parent = content.Parent as FrameworkElement;

            if (parent != null)
            {
                parent.MinWidth  = 400;
                parent.MinHeight = 400;
            }

            content.MinWidth  = 400;
            content.MinHeight = 400;

            var border = content.FindFirstChild <Border>();

            if (border != null)
            {
                border.Background = new SolidColorBrush(Colors.White);
            }

            Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap bmp = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();

            await bmp.RenderAsync(content.Parent as FrameworkElement).AsTask(ct);

            content.DataContext = null;

            var pixels = await bmp.GetPixelsAsync().AsTask(ct);

            var folder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync(
                folderName,
                Windows.Storage.CreationCollisionOption.OpenIfExists
                ).AsTask(ct);

            if (folder == null)
            {
                folder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName).AsTask(ct);
            }

            var file = await folder.CreateFileAsync(
                fileName,
                Windows.Storage.CreationCollisionOption.ReplaceExisting
                ).AsTask(ct);

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

                encoder.SetPixelData(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Ignore,
                    (uint)bmp.PixelWidth,
                    (uint)bmp.PixelHeight,
                    DisplayInformation.GetForCurrentView().RawDpiX,
                    DisplayInformation.GetForCurrentView().RawDpiY,
                    pixels.ToArray()
                    );

                await encoder.FlushAsync().AsTask(ct);
            }
        }