Begin ability to have .cshtml files in Blazor apps

This commit is contained in:
Steve Sanderson 2018-01-11 11:11:47 +00:00
parent 8d4491d4b8
commit 3f522ab216
4 changed files with 84 additions and 1 deletions

View File

@ -0,0 +1,3 @@
@inherits Microsoft.Blazor.Components.BlazorComponent
<h1>Hello, world!</h1>
Hello from the Razor component.

View File

@ -4,7 +4,6 @@
using Microsoft.Blazor.Browser.Rendering;
using Microsoft.Blazor.Components;
using Microsoft.Blazor.RenderTree;
using System;
namespace StandaloneApp
{

View File

@ -0,0 +1,38 @@
// 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.Blazor.RenderTree;
using System;
using System.Threading.Tasks;
namespace Microsoft.Blazor.Components
{
/// <summary>
/// Optional base class for Blazor components. Alternatively, Blazor components may
/// implement <see cref="IComponent"/> directly.
/// </summary>
public abstract class BlazorComponent : IComponent
{
/// <inheritdoc />
public virtual void BuildRenderTree(RenderTreeBuilder builder)
{
// This is virtual rather than abstract so that 'code behind' classes don't have to
// be marked abstract.
// Developers can either override this method in derived classes, or can use Razor
// syntax to define a derived class and have the compiler generate the method.
}
// At present, if you have a .cshtml file in a project with <Project Sdk="Microsoft.NET.Sdk.Web">,
// Visual Studio will run design-time builds for it, codegenning a class that attempts to override
// this method. Therefore the virtual method must be defined, even though it won't be used at runtime,
// because otherwise VS will display a design-time error in its 'Error List' pane.
// TODO: Track down what triggers the design-time build for .cshtml files and how to stop it, then
// this method can be removed.
/// <summary>
/// Not used. Do not invoke this method.
/// </summary>
/// <returns>Always throws an exception.</returns>
public virtual Task ExecuteAsync()
=> throw new NotImplementedException($"Blazor components do not implement {nameof(ExecuteAsync)}.");
}
}

View File

@ -0,0 +1,43 @@
// 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.
/*
* Currently if you have a .cshtml file in a project with <Project Sdk="Microsoft.NET.Sdk.Web">,
* Visual Studio will run design-time builds for the .cshtml file that assume certain ASP.NET MVC
* APIs exist. Since those namespaces and types wouldn't normally exist for Blazor client apps,
* this leads to spurious errors in the Errors List pane, even though there aren't actually any
* errors on build. As a workaround, we define here a minimal set of namespaces/types that satisfy
* the design-time build.
*
* TODO: Track down what is triggering the unwanted design-time build and find out how to disable it.
* Then this file can be removed entirely.
*/
using System;
namespace Microsoft.AspNetCore.Mvc
{
public interface IUrlHelper { }
public interface IViewComponentHelper { }
}
namespace Microsoft.AspNetCore.Mvc.Razor
{
public class RazorPage<T> { }
namespace Internal
{
public class RazorInjectAttributeAttribute : Attribute { }
}
}
namespace Microsoft.AspNetCore.Mvc.Rendering
{
public interface IJsonHelper { }
public interface IHtmlHelper<T> { }
}
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
public interface IModelExpressionProvider { }
}