diff --git a/src/Microsoft.AspNetCore.Blazor/Components/RazorToolingWorkaround.cs b/src/Microsoft.AspNetCore.Blazor/Components/RazorToolingWorkaround.cs index 53ea414131..1309be5be2 100644 --- a/src/Microsoft.AspNetCore.Blazor/Components/RazorToolingWorkaround.cs +++ b/src/Microsoft.AspNetCore.Blazor/Components/RazorToolingWorkaround.cs @@ -14,6 +14,7 @@ */ using Microsoft.AspNetCore.Blazor.Components; +using Microsoft.AspNetCore.Blazor.Layouts; using System; namespace Microsoft.AspNetCore.Mvc @@ -28,7 +29,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // This is temporary and exists only to support TemporaryLayoutPass. // It will be removed when we can add Blazor-specific directives. - public object Layout() where TLayout : IComponent + public object Layout() where TLayout : ILayoutComponent => throw new NotImplementedException(); // Similar temporary mechanism as above diff --git a/src/Microsoft.AspNetCore.Blazor/Layouts/ILayoutComponent.cs b/src/Microsoft.AspNetCore.Blazor/Layouts/ILayoutComponent.cs new file mode 100644 index 0000000000..24c2b5a27c --- /dev/null +++ b/src/Microsoft.AspNetCore.Blazor/Layouts/ILayoutComponent.cs @@ -0,0 +1,19 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.Blazor.Components; +using Microsoft.AspNetCore.Blazor.RenderTree; + +namespace Microsoft.AspNetCore.Blazor.Layouts +{ + /// + /// Indicates that the type represents a layout. + /// + public interface ILayoutComponent : IComponent + { + /// + /// Gets or sets the content to be rendered inside the layout. + /// + RenderFragment Body { get; set; } + } +} diff --git a/src/Microsoft.AspNetCore.Blazor/Layouts/LayoutAttribute.cs b/src/Microsoft.AspNetCore.Blazor/Layouts/LayoutAttribute.cs index d05798a866..3518ec22fe 100644 --- a/src/Microsoft.AspNetCore.Blazor/Layouts/LayoutAttribute.cs +++ b/src/Microsoft.AspNetCore.Blazor/Layouts/LayoutAttribute.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNetCore.Blazor.Components; using System; namespace Microsoft.AspNetCore.Blazor.Layouts @@ -13,22 +12,22 @@ namespace Microsoft.AspNetCore.Blazor.Layouts public class LayoutAttribute : Attribute { /// - /// The type of the layout. The type always implements . + /// The type of the layout. The type always implements . /// public Type LayoutType { get; private set; } /// /// Constructs an instance of . /// - /// The type of the layout. This must implement . + /// The type of the layout. This must implement . public LayoutAttribute(Type layoutType) { LayoutType = layoutType ?? throw new ArgumentNullException(nameof(layoutType)); - if (!typeof(IComponent).IsAssignableFrom(layoutType)) + if (!typeof(ILayoutComponent).IsAssignableFrom(layoutType)) { throw new ArgumentException($"Invalid layout type: {layoutType.FullName} " + - $"does not implement {typeof(IComponent).FullName}."); + $"does not implement {typeof(ILayoutComponent).FullName}."); } } } diff --git a/test/Microsoft.AspNetCore.Blazor.Build.Test/RazorCompilerTest.cs b/test/Microsoft.AspNetCore.Blazor.Build.Test/RazorCompilerTest.cs index 9ec5eb25d7..d978aa554c 100644 --- a/test/Microsoft.AspNetCore.Blazor.Build.Test/RazorCompilerTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.Build.Test/RazorCompilerTest.cs @@ -375,7 +375,7 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test public void SupportsLayoutDeclarationsViaTemporarySyntax() { // Arrange/Act - var testComponentTypeName = typeof(TestComponent).FullName.Replace('+', '.'); + var testComponentTypeName = typeof(TestLayout).FullName.Replace('+', '.'); var component = CompileToComponent( $"@(Layout<{testComponentTypeName}>())" + $"Hello"); @@ -384,7 +384,7 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test // Assert var layoutAttribute = component.GetType().GetCustomAttribute(); Assert.NotNull(layoutAttribute); - Assert.Equal(typeof(TestComponent), layoutAttribute.LayoutType); + Assert.Equal(typeof(TestLayout), layoutAttribute.LayoutType); Assert.Collection(frames, frame => AssertFrame.Text(frame, "Hello")); } @@ -540,6 +540,19 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test } } + public class TestLayout : ILayoutComponent + { + public RenderFragment Body { get; set; } + + public void Init(RenderHandle renderHandle) + { + } + + public void SetParameters(ParameterCollection parameters) + { + } + } + public interface ITestInterface { } } }