private static async Task OnStructureChanged(DependencyObject d) { var c = (Rating)d; if (c.EmptyImage == null) { c.EmptyImage = new Uri("ms-appx:///XamlBrewer.Uwp.Rating/Assets/defaultStar_empty.png"); } if (c.FilledImage == null) { c.FilledImage = new Uri("ms-appx:///XamlBrewer.Uwp.Rating/Assets/defaultStar_full.png"); } if ((c.StepFrequency <= 0) || (c.StepFrequency > 1)) { c.StepFrequency = 1; } var panel = c.GetTemplateChild(ItemsPartName) as StackPanel; if (panel != null) { // Load images. var root = panel.GetVisual(); var compositor = root.Compositor; var canvasDevice = new CanvasDevice(); var compositionDevice = CanvasComposition.CreateCompositionGraphicsDevice(compositor, canvasDevice); var rightPadding = c.ItemPadding; c.Clips.Clear(); for (int i = 0; i < c.Maximum; i++) { if (i == c.Maximum - 1) { rightPadding = 0; } // Create grid. var grid = new Grid { Height = c.ItemHeight, Width = c.ItemHeight, Margin = new Thickness(0, 0, rightPadding, 0) }; panel.Children.Add(grid); var gridRoot = grid.GetVisual(); // Empty image. var surface = await LoadFromUri(canvasDevice, compositionDevice, c.EmptyImage, new Size(c.ItemHeight, c.ItemHeight)); var emptyBrush = compositor.CreateSurfaceBrush(surface); var spriteVisual = compositor.CreateSpriteVisual(); spriteVisual.Size = new Vector2(c.ItemHeight, c.ItemHeight); gridRoot.Children.InsertAtTop(spriteVisual); spriteVisual.Brush = emptyBrush; // Filled image. surface = await LoadFromUri(canvasDevice, compositionDevice, c.FilledImage, new Size(c.ItemHeight, c.ItemHeight)); var fullBrush = compositor.CreateSurfaceBrush(surface); spriteVisual = compositor.CreateSpriteVisual(); spriteVisual.Size = new Vector2(c.ItemHeight, c.ItemHeight); var clip = compositor.CreateInsetClip(); c.Clips.Add(clip); spriteVisual.Clip = clip; gridRoot.Children.InsertAtTop(spriteVisual); spriteVisual.Brush = fullBrush; } compositionDevice.Dispose(); canvasDevice.Dispose(); } OnValueChanged(c); }
private static void OnStructureChanged(DependencyObject d) { Rating c = (Rating)d; if (c.EmptyImage == null) { c.EmptyImage = new Uri("ms-appx:///XamlBrewer.Uwp.RatingControl/Assets/defaultStar_empty.png"); } if (c.FilledImage == null) { c.FilledImage = new Uri("ms-appx:///XamlBrewer.Uwp.RatingControl/Assets/defaultStar_full.png"); } if ((c.StepFrequency <= 0) || (c.StepFrequency > 1)) { c.StepFrequency = 1; } var panel = c.GetTemplateChild(ItemsPartName) as StackPanel; if (panel != null) { // Load images. var root = panel.GetVisual(); var compositor = root.Compositor; var options = new CompositionImageOptions() { DecodeWidth = c.ItemHeight, DecodeHeight = c.ItemHeight }; var imageFactory = CompositionImageFactory.CreateCompositionImageFactory(compositor); var image = imageFactory.CreateImageFromUri(c.EmptyImage, options); var emptyBrush = compositor.CreateSurfaceBrush(image.Surface); image = imageFactory.CreateImageFromUri(c.FilledImage, options); var fullBrush = compositor.CreateSurfaceBrush(image.Surface); var rightPadding = c.ItemPadding; c.Clips.Clear(); for (int i = 0; i < c.Maximum; i++) { if (i == c.Maximum - 1) { rightPadding = 0; } // Create grid. var grid = new Grid { Height = c.ItemHeight, Width = c.ItemHeight, Margin = new Thickness(0, 0, rightPadding, 0) }; panel.Children.Add(grid); var gridRoot = grid.GetVisual(); // Empty image. var spriteVisual = compositor.CreateSpriteVisual(); spriteVisual.Size = new Vector2(c.ItemHeight, c.ItemHeight); gridRoot.Children.InsertAtTop(spriteVisual); spriteVisual.Brush = emptyBrush; // Filled image. spriteVisual = compositor.CreateSpriteVisual(); spriteVisual.Size = new Vector2(c.ItemHeight, c.ItemHeight); var clip = compositor.CreateInsetClip(); c.Clips.Add(clip); spriteVisual.Clip = clip; gridRoot.Children.InsertAtTop(spriteVisual); spriteVisual.Brush = fullBrush; } } OnValueChanged(c); }