简体   繁体   中英

How do I merge my code behind file in the solution explorer?

I am trying to create a code-behind file for my blazor application (Using .NET Core 3.1, Blazor-server-side), while it also shows up in the solution explorer as the razor file you can click open, and then see the razor.cs file in the tree structure.

I managed to get the funcionality to work, but it still doesn't show up correctly in the solution explorer.

As far as I know I am doing everything correctly, I even tried to use an item template, and that didn't work either. Whatever I try, they always show up as seperate files.

The file names are the same, except for the extension, the class inherits from ComponentBase, I have tried calling the class in the file FetchData or FetchDataBase, neither works.

My class file is called: 'FetchData.razor.cs' My razor file is called 'FetchData.razor'

Am I missing something? I am on VS2019 V16.5.2, do I need to be on the preview version for this?

Code snippets: class:

public partial class FetchDataBase : ComponentBase
{
    [Inject]
    private CoordinatesService CoordinatesService { get; set; }

    protected IEnumerable<ICoordinate> coordinates;

    protected override async Task OnInitializedAsync()
    {
        coordinates = await CoordinatesService.GetCoordinates();
    }
}

razor page:

@page "/fetchdata"
@inherits FetchDataBase

<h1>Coordinates</h1>

<p>This component demonstrates fetching data from a service.</p>

@if (coordinates == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>Test</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var coordinate in coordinates)
            {
                <tr>
                    <td>@coordinate.Test</td>
                </tr>
            }
        </tbody>
    </table>
}

Image of solution explorer:

解决方案浏览器示例

Thanks!

At first glance, your file names do look correct, so make sure that File Nesting is turned on in the Solution Explorer:

在此处输入图片说明

Edit: Your file names look correct. Have you tried closing Visual Studio and reopening? What are the little locks by your files, is that source control?

As I change a file name, the files switch in solution explorer to become embedded in the file.

Name your file:

FileName.razor

在此处输入图片说明 FileName.razor.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM