简体   繁体   English

Blazor:在 Razor 文件中定义命名空间

[英]Blazor: Define namespace in Razor File

Question: Is there a way to define a namespace in a.razor file in Blazor.问:有没有办法在 Blazor 中的 a.razor 文件中定义命名空间。

Problem Explanation: Razor files in Blazor do not have a namespace defined (only the code behind class has one and it should match the.razor file location in the project). Problem Explanation: Razor files in Blazor do not have a namespace defined (only the code behind class has one and it should match the.razor file location in the project). This resulted in a problem on GitHub and it restrict me from offering a Razor Component Library with few namespaces.这导致 GitHub 出现问题,它限制了我提供一个命名空间很少的 Razor 组件库。

Github problem: This stems from a Microsoft philosophy I do not subscribe to: filenames are not case sensitive. Github 问题:这源于我不认同的微软哲学:文件名不区分大小写。 I had a folder, lets say it was named 'FooBar'.我有一个文件夹,可以说它被命名为“FooBar”。 In my project I renamed it 'Foobar'.在我的项目中,我将其重命名为“Foobar”。 Either Windows did not rename it properly, or when checking in in Github it was not seen as a change. Windows 没有正确重命名它,或者在签入 Github 时,它没有被视为更改。 As a result, my code behind namespace 'Foobar' no longer matched the namespace of my /razor file 'FooBar', resulting in a build error on Github (it worked fine on my pc).结果,我的命名空间“Foobar”后面的代码不再匹配我的 /razor 文件“FooBar”的命名空间,导致 Github 上的构建错误(它在我的电脑上运行良好)。 If I had been able to specify the namespace in the.razor file this would not have happened.如果我能够在 .razor 文件中指定命名空间,这将不会发生。

Component library problem: I am making a component library I want to offer on NuGet.组件库问题:我正在制作一个想要在 NuGet 上提供的组件库。 I want to keep all my components in separate folders (let's say for Single responsibility sake).我想将所有组件保存在单独的文件夹中(假设为单一责任)。 When I now want to include this component library, the _imports file will have a @include for each folder that is part of the library, again because I am unable to separate working with folders from the namespace the components are in. I would like to say @include Foobar rather than @include Foobar.Checkbox , @include Foobar.Radiobutton etc, etc.当我现在想要包含此组件库时,_imports 文件将为库中的每个文件夹都有一个@include ,这也是因为我无法将文件夹的工作与组件所在的命名空间分开。我想说@include Foobar而不是@include Foobar.Checkbox@include Foobar.Radiobutton等等。

So is there any solution to this?那么有什么解决方案吗?

Ok I did not find this googling (It might be somewhere though) but I tried the following:好的,我没有找到这个谷歌搜索(虽然它可能在某个地方),但我尝试了以下方法:

In the code behind rename the namespace Foobar.Checkbox to Foobar in the razor file add @namespace Foobar在后面的代码中,在 razor 文件中将命名空间 Foobar.Checkbox 重命名为 Foobar 添加@namespace Foobar

Yes it is that simple facepalm是的,就是这么简单的手掌

By example.举个例子。 Your Blazor component name is Box, in folder Boxes.您的 Blazor 组件名称是 Box,在文件夹 Boxes 中。

You namespace should be Boxs - Right?你的命名空间应该是 Boxs - 对吧?

Do this: Add a class in Boxs folder with name Box.razor.cs.这样做:在 Boxs 文件夹中添加一个 class,名称为 Box.razor.cs。 This class is the C# code of Box, then add to this class:这个 class 是 Box 的 C# 代码,然后添加到这个 class 中:

using Microsoft.AspNetCore.Components;

namespace Boxes;

public partial class Box: ComponentBase
{
// @code ...
}

To make easy, add the Boxs namespace to _imports.razor.为方便起见,请将 Boxs 命名空间添加到 _imports.razor。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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