Skip to content

Commit b8936ad

Browse files
committed
Update custom view item example to follow modern best practices
Simplified the ButtonDetailViewItemBlazor implementation: - ViewItem now implements IComponentContentHolder directly (no wrapper class needed) - Component model is created with all parameters in CreateControlCore This approach reduces boilerplate code and better demonstrates the recommended pattern for creating custom view items in XAF Blazor applications.
1 parent 9b721b9 commit b8936ad

File tree

4 files changed

+54
-59
lines changed

4 files changed

+54
-59
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@namespace CustomViewItem.Blazor.Server.Editors.ButtonViewItem
2+
3+
<DxButton Text=@Text Click=@Click />
4+
5+
@code {
6+
[Parameter]
7+
public string Text { get; set; }
8+
[Parameter]
9+
public EventCallback Click { get; set; }
10+
}
Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
1-
using System;
2-
using DevExpress.ExpressApp;
1+
using DevExpress.ExpressApp;
32
using DevExpress.ExpressApp.Blazor;
3+
using DevExpress.ExpressApp.Blazor.Components;
4+
using DevExpress.ExpressApp.Blazor.Components.Models;
45
using DevExpress.ExpressApp.Editors;
56
using DevExpress.ExpressApp.Model;
67
using Microsoft.AspNetCore.Components;
78

8-
using DevExpress.ExpressApp.Blazor.Components;
9+
namespace CustomViewItem.Blazor.Server.Editors.ButtonViewItem;
10+
11+
public interface IModelButtonDetailViewItemBlazor : IModelViewItem;
912

10-
namespace MySolution.Module.Blazor {
11-
public interface IModelButtonDetailViewItemBlazor : IModelViewItem { }
13+
[ViewItem(typeof(IModelButtonDetailViewItemBlazor))]
14+
public class ButtonDetailViewItemBlazor(IModelViewItem model, Type objectType) :
15+
ViewItem(objectType, model.Id),
16+
IComponentContentHolder,
17+
IComplexViewItem
18+
{
19+
private ButtonModel componentModel;
20+
private XafApplication application;
1221

13-
[ViewItem(typeof(IModelButtonDetailViewItemBlazor))]
14-
public class ButtonDetailViewItemBlazor : ViewItem, IComplexViewItem {
15-
public class ButtonHolder : IComponentContentHolder {
16-
public ButtonHolder(ButtonModel componentModel) {
17-
ComponentModel = componentModel;
18-
}
19-
public ButtonModel ComponentModel { get; }
20-
RenderFragment IComponentContentHolder.ComponentContent => ComponentModelObserver.Create(ComponentModel, ButtonRenderer.Create(ComponentModel));
21-
}
22-
private XafApplication application;
23-
public ButtonDetailViewItemBlazor(IModelViewItem model, Type objectType) : base(objectType, model.Id) { }
24-
void IComplexViewItem.Setup(IObjectSpace objectSpace, XafApplication application) {
25-
this.application = application;
26-
}
27-
protected override object CreateControlCore() => new ButtonHolder(new ButtonModel());
28-
protected override void OnControlCreated() {
29-
if (Control is ButtonHolder holder) {
30-
holder.ComponentModel.Text = "Click me!";
31-
holder.ComponentModel.Click += ComponentModel_Click;
32-
}
33-
base.OnControlCreated();
34-
}
35-
public override void BreakLinksToControl(bool unwireEventsOnly) {
36-
if (Control is ButtonHolder holder) {
37-
holder.ComponentModel.Click -= ComponentModel_Click;
38-
}
39-
base.BreakLinksToControl(unwireEventsOnly);
40-
}
41-
private void ComponentModel_Click(object sender, EventArgs e) {
42-
application.ShowViewStrategy.ShowMessage("Action is executed!");
43-
}
22+
RenderFragment IComponentContentHolder.ComponentContent =>
23+
ComponentModelObserver.Create(componentModel, componentModel.GetComponentContent());
24+
void IComplexViewItem.Setup(IObjectSpace objectSpace, XafApplication application) {
25+
this.application = application;
26+
}
27+
28+
protected override object CreateControlCore() {
29+
componentModel = new ButtonModel
30+
{
31+
Text = "Click me!",
32+
Click = EventCallback.Factory.Create(this, ComponentModel_Click),
33+
};
34+
return componentModel;
35+
}
36+
private void ComponentModel_Click() {
37+
application.ShowViewStrategy.ShowMessage("Action is executed!");
4438
}
4539
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
using System;
2-
using DevExpress.ExpressApp.Blazor.Components.Models;
1+
using DevExpress.ExpressApp.Blazor.Components.Models;
2+
using Microsoft.AspNetCore.Components;
33

4-
namespace MySolution.Module.Blazor {
5-
public class ButtonModel : ComponentModelBase {
6-
public string Text {
7-
get => GetPropertyValue<string>();
8-
set => SetPropertyValue(value);
9-
}
10-
public void ClickFromUI() {
11-
Click?.Invoke(this, EventArgs.Empty);
12-
}
13-
public event EventHandler Click;
4+
namespace CustomViewItem.Blazor.Server.Editors.ButtonViewItem;
5+
6+
public class ButtonModel : ComponentModelBase {
7+
public string Text {
8+
get => GetPropertyValue<string>();
9+
set => SetPropertyValue(value);
10+
}
11+
public EventCallback Click {
12+
get => GetPropertyValue<EventCallback>();
13+
set => SetPropertyValue(value);
1414
}
15+
16+
public override Type ComponentType => typeof(Button);
1517
}

CS/EFCore/CustomViewItem/CustomViewItem.Blazor.Server/Editors/ButtonViewItem/ButtonRenderer.razor

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)