-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Component Generator - generate ElementTemplates and non-generic DataT…
…emplates (#51)
- Loading branch information
1 parent
382fcdc
commit 60c59c6
Showing
21 changed files
with
351 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
samples/ControlGallery/Views/SetValues/RadioButtonTemplated.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<RadioButton Value="Value" @bind-IsChecked="isChecked"> | ||
<ControlTemplate> | ||
<Label TextColor="@(isChecked ? Colors.Green : Colors.Red)" Text="@Value" /> | ||
</ControlTemplate> | ||
</RadioButton> | ||
|
||
@code { | ||
[Parameter] public string Value { get; set; } | ||
|
||
bool isChecked; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/BlazorBindings.Maui/Elements/DataTemplates/ControlTemplateItemsComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
using System.Collections.Generic; | ||
using MC = Microsoft.Maui.Controls; | ||
|
||
namespace BlazorBindings.Maui.Elements.DataTemplates | ||
{ | ||
#pragma warning disable CA1812 // Avoid uninstantiated internal classes. Class is used as generic parameter. | ||
internal class ControlTemplateItemsComponent : ComponentBase | ||
#pragma warning restore CA1812 // Avoid uninstantiated internal classes | ||
{ | ||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
builder.OpenElement(0, ElementName); | ||
|
||
foreach (var itemRoot in _itemRoots) | ||
{ | ||
builder.OpenComponent<InitializedVerticalStackLayout>(1); | ||
|
||
builder.AddAttribute(2, nameof(InitializedVerticalStackLayout.NativeControl), itemRoot); | ||
builder.AddAttribute(3, "ChildContent", (RenderFragment)(builder => | ||
{ | ||
Template.Invoke(builder); | ||
})); | ||
|
||
builder.CloseComponent(); | ||
} | ||
|
||
builder.CloseElement(); | ||
} | ||
|
||
// ElementName is parametrized so that component would be handled by appropriate handler. | ||
[Parameter] public string ElementName { get; set; } | ||
[Parameter] public RenderFragment Template { get; set; } | ||
|
||
private readonly List<MC.VerticalStackLayout> _itemRoots = new(); | ||
|
||
public MC.View AddTemplateRoot() | ||
{ | ||
var templateRoot = new MC.VerticalStackLayout(); | ||
_itemRoots.Add(templateRoot); | ||
StateHasChanged(); | ||
|
||
return templateRoot; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/BlazorBindings.Maui/Elements/DataTemplates/MbbControlTemplate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.Maui.Controls; | ||
|
||
namespace BlazorBindings.Maui.Elements.DataTemplates | ||
{ | ||
internal class MbbControlTemplate : ControlTemplate | ||
{ | ||
public MbbControlTemplate(ControlTemplateItemsComponent controlTemplateItemsContainer) | ||
: base(controlTemplateItemsContainer.AddTemplateRoot) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/BlazorBindings.Maui/Elements/Handlers/ControlTemplatePropertyHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using BlazorBindings.Core; | ||
using BlazorBindings.Maui.Elements.DataTemplates; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.Maui.Controls; | ||
using System; | ||
using MC = Microsoft.Maui.Controls; | ||
|
||
namespace BlazorBindings.Maui.Elements.Handlers | ||
{ | ||
public class ControlTemplatePropertyHandler<TElementType> : IMauiContainerElementHandler, INonChildContainerElement | ||
{ | ||
private readonly ControlTemplateItemsComponent _controlTemplateItemsComponent; | ||
private readonly Action<TElementType, ControlTemplate> _setPropertyAction; | ||
|
||
public ControlTemplatePropertyHandler(IComponent controlTemplateItemsComponent, Action<TElementType, ControlTemplate> setPropertyAction) | ||
{ | ||
_controlTemplateItemsComponent = (ControlTemplateItemsComponent)controlTemplateItemsComponent; | ||
_setPropertyAction = setPropertyAction; | ||
} | ||
|
||
public void SetParent(object parentElement) | ||
{ | ||
var parent = (TElementType)parentElement; | ||
var controlTemplate = new MbbControlTemplate(_controlTemplateItemsComponent); | ||
_setPropertyAction(parent, controlTemplate); | ||
} | ||
|
||
public void Remove() | ||
{ | ||
// Because this Handler is used internally only, this method is no-op. | ||
} | ||
|
||
// Because this is a 'fake' element, all matters related to physical trees | ||
// should be no-ops. | ||
|
||
void IMauiContainerElementHandler.AddChild(MC.Element child, int physicalSiblingIndex) { } | ||
|
||
void IMauiContainerElementHandler.RemoveChild(MC.Element child) { } | ||
|
||
int IMauiContainerElementHandler.GetChildIndex(MC.Element child) => -1; | ||
|
||
object IElementHandler.TargetElement => null; | ||
void IElementHandler.ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName) { } | ||
|
||
MC.Element IMauiElementHandler.ElementControl => null; | ||
bool IMauiElementHandler.IsParented() => false; | ||
|
||
void IMauiElementHandler.SetParent(MC.Element parent) | ||
{ | ||
// This should never get called. Instead, INonChildContainerElement.SetParent() implemented | ||
// in this class should get called. | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.