IView CreateSampleGrid() { var layout = new Microsoft.Maui.Controls.Layout2.GridLayout() { ColumnSpacing = 0, RowSpacing = 0 }; layout.AddRowDefinition(new RowDefinition() { Height = new GridLength(40) }); layout.AddRowDefinition(new RowDefinition() { Height = GridLength.Auto }); layout.AddColumnDefinition(new ColumnDefinition() { Width = new GridLength(100) }); layout.AddColumnDefinition(new ColumnDefinition() { Width = new GridLength(100) }); var topLeft = new Label { Text = "Top Left", BackgroundColor = Colors.LightBlue }; layout.Add(topLeft); var bottomLeft = new Label { Text = "Bottom Left", BackgroundColor = Colors.Lavender }; layout.Add(bottomLeft); layout.SetRow(bottomLeft, 1); var topRight = new Label { Text = "Top Right", BackgroundColor = Colors.Orange }; layout.Add(topRight); layout.SetColumn(topRight, 1); var bottomRight = new Label { Text = "Bottom Right", BackgroundColor = Colors.MediumPurple }; layout.Add(bottomRight); layout.SetRow(bottomRight, 1); layout.SetColumn(bottomRight, 1); layout.BackgroundColor = Colors.Chartreuse; return(layout); }
IView CreateImagesGrid() { var layout = new Microsoft.Maui.Controls.Layout2.GridLayout { ColumnSpacing = 10, RowSpacing = 10, Margin = 10 }; layout.AddRowDefinition(new RowDefinition { Height = GridLength.Auto }); layout.AddRowDefinition(new RowDefinition { Height = new GridLength(120) }); layout.AddRowDefinition(new RowDefinition { Height = GridLength.Auto }); layout.AddRowDefinition(new RowDefinition { Height = new GridLength(120) }); layout.AddRowDefinition(new RowDefinition { Height = GridLength.Auto }); layout.AddRowDefinition(new RowDefinition { Height = new GridLength(120) }); layout.AddRowDefinition(new RowDefinition { Height = GridLength.Auto }); layout.AddRowDefinition(new RowDefinition { Height = new GridLength(120) }); layout.AddRowDefinition(new RowDefinition { Height = GridLength.Auto }); layout.AddRowDefinition(new RowDefinition { Height = new GridLength(120) }); layout.AddColumnDefinition(new ColumnDefinition { Width = new GridLength(120) }); layout.AddColumnDefinition(new ColumnDefinition { Width = new GridLength(120) }); var row = -1; Add(new Label { Text = "App Bundle", WidthRequest = 150 }, row: (row += 2) - 1, col: 0, colSpan: 2); Add(new Image { Source = "dotnet_bot.png" }, row: row, col: 0); Add(new Image { Source = "animated_heart.gif", IsAnimationPlaying = true }, row: row, col: 1); Add(new Label { Text = "File", WidthRequest = 150 }, row: (row += 2) - 1, col: 0, colSpan: 2); Add(new Image { Source = CopyLocal("dotnet_bot.png") }, row: row, col: 0); Add(new Image { Source = CopyLocal("animated_heart.gif"), IsAnimationPlaying = true }, row: row, col: 1); Add(new Label { Text = "Font", WidthRequest = 150 }, row: (row += 2) - 1, col: 0, colSpan: 2); Add(new Image { Source = new FontImageSource { FontFamily = "Ionicons", Glyph = "\uf2fe" }, BackgroundColor = Color.FromUint(0xFF512BD4), Aspect = Aspect.Center }, row: row, col: 0); Add(new Image { Source = new FontImageSource { FontFamily = "Dokdo", Glyph = "M" }, BackgroundColor = Color.FromUint(0xFF512BD4), Aspect = Aspect.Center }, row: row, col: 1); Add(new Label { Text = "URI", WidthRequest = 150 }, row: (row += 2) - 1, col: 0, colSpan: 2); Add(new Image { Source = "https://raw.githubusercontent.com/dotnet-foundation/swag/05cc70d33fa8c310147b9bd70ae9e103a072cae0/dotnet-bot/dotnet-bot-pot.png" }, row: row, col: 0); Add(new Image { Source = "https://raw.githubusercontent.com/mono/SkiaSharp/6753bfad91dce1894c69084555dab6494efa90eb/samples/Gallery/Shared/Media/animated-heart.gif", IsAnimationPlaying = true }, row: row, col: 1); Add(new Label { Text = "Stream", WidthRequest = 150 }, row: (row += 2) - 1, col: 0, colSpan: 2); Add(new Image { Source = ImageSource.FromStream(() => GetEmbedded("dotnet_bot.png")) }, row: row, col: 0); Add(new Image { Source = ImageSource.FromStream(() => GetEmbedded("animated_heart.gif")), IsAnimationPlaying = true }, row: row, col: 1); return(layout); void Add(IView view, int row = 0, int col = 0, int rowSpan = 1, int colSpan = 1) { layout.Add(view); layout.SetRow(view, row); layout.SetRowSpan(view, rowSpan); layout.SetColumn(view, col); layout.SetColumnSpan(view, colSpan); } string CopyLocal(string embeddedPath) { var path = Path.Combine(FileSystem.CacheDirectory, Guid.NewGuid().ToString("N")); using var stream = GetEmbedded(embeddedPath); using var file = File.Create(path); stream.CopyTo(file); return(path); } Stream GetEmbedded(string embeddedPath) { var assembly = GetType().Assembly; var name = assembly .GetManifestResourceNames() .First(n => n.EndsWith(embeddedPath, StringComparison.InvariantCultureIgnoreCase)); return(assembly.GetManifestResourceStream(name)); } }