【发布时间】:2019-11-21 20:58:50
【问题描述】:
public class MyButton
{
public string Class { get; set; }
public string Name { get; set; }
public event EventHandler OnClickEvent;
public void EventLoad()
{
OnClickEvent?.Invoke(this,EventArgs.Empty);
}
}
public class ToolbarService
{
public List<MyButton> MyButtons { get; set; } = new List<MyButton>();
public ToolbarService()
{
MyButtons.Add(new MyButton
{
Name="Save",
Class="btn btn-primary",
});
MyButtons.Add(new MyButton
{
Name = "Update",
Class = "btn btn-success",
});
MyButtons.Add(new MyButton
{
Name = "Remove",
Class = "btn btn-danger",
});
}
}
public class MyBaseComponent : ComponentBase
{
[Inject] public ToolbarService ToolbarService { get; set; }
protected override void OnInitialized()
{
MyEventLoads();
}
public void MyEventLoads()
{
foreach (var item in ToolbarService.MyButtons)
{
item.OnClickEvent += ButtonItems;
}
}
private void ButtonItems(object sender, EventArgs e)
{
var btn = (MyButton)sender;
switch (btn.Name)
{
case "Save":
Save();
break;
case "Update":
Update();
break;
case "Remove":
Remove();
break;
}
}
protected virtual void Remove()
{
Console.WriteLine("Base Remove");
}
protected virtual void Update()
{
Console.WriteLine("Base Update");
}
protected virtual void Save()
{
Console.WriteLine("Base Save");
}
}
public class ToolbarMenuBase : MyBaseComponent
{
}
public class IndexBase : MyBaseComponent
{
protected override void Save()
{
Console.WriteLine("Save Override");
}
}
当工具栏覆盖索引页面中组件的保存方法时,它会执行两次该方法,如屏幕截图所示。
单击“保存”按钮时,它会将“基本单击”和“保存覆盖”消息写入控制台。通常,我必须将“保存覆盖”写入控制台,因为我覆盖了索引页面上的方法。
【问题讨论】:
-
我正在使用 Blazor Web 程序集。应用程序中不存在 _host 文件。
标签: c# asp.net .net blazor blazor-server-side