问题描述
我可以创建一个内联组件,例如
I can create an inline component like
<h1>@foo</h1>
@functions {
string foo = "foo";
}
但是,当我创建仅包含以下内容的Foo.razor
时:
However when I create Foo.razor
containing just:
<h1>@foo</h1>
和Foo.razor.cs
包含:
namespace MyApp.Client.Components {
public class Foo: ComponentBase {
public string foo;
}
}
我得到:
Error CS0101 The namespace 'MyApp.Client.Components' already contains a definition for 'Foo'
我正在使用最新的VS 2019和Blazor库.
I am using the latest VS 2019 and Blazor libraries.
我在做什么错了?
推荐答案
更新:现在可以: https://stackoverflow. com/a/59470941/1141089
目前,隐藏代码"是指和.razor视图不能共享相同的名称.
Currently, the "code-behind" and .razor view can't share the same name.
因此,当您具有Foo.razor.cs
和Foo.razor
时,它将被视为同一文件,从而导致冲突.
So when you have Foo.razor.cs
and Foo.razor
it is seen as the same file and thus causes a collision.
暂时的解决方法:将Foo.razor.cs
重命名为FooBase.cs
(或其他名称).
Workaround for now:Rename your Foo.razor.cs
to FooBase.cs
(or something else).
然后在您的Foo.razor
中添加@inherits FooBase
此处存在与此相关的GitHub问题: https://github.com/aspnet/AspNetCore/issues/5487
There is a GitHub issue regarding this here: https://github.com/aspnet/AspNetCore/issues/5487
这篇关于如何创建“后台代码"; VS 2019的Blazor组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!