Skip to content

Commit 8ad712f

Browse files
authored
Update custom view item example to follow modern best practices
2 parents 9b721b9 + b92065f commit 8ad712f

File tree

12 files changed

+142
-124
lines changed

12 files changed

+142
-124
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ BenchmarkDotNet.Artifacts/
4949
project.lock.json
5050
project.fragment.lock.json
5151
artifacts/
52-
**/Properties/launchSettings.json
5352

5453
*_i.c
5554
*_p.c
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<MouseEventArgs> Click { get; set; }
10+
}
Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
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;
8+
using Microsoft.AspNetCore.Components.Web;
79

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

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

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-
}
25+
RenderFragment IComponentContentHolder.ComponentContent =>
26+
ComponentModelObserver.Create(componentModel, componentModel.GetComponentContent());
27+
void IComplexViewItem.Setup(IObjectSpace objectSpace, XafApplication application) {
28+
this.application = application;
29+
}
30+
31+
protected override object CreateControlCore() {
32+
componentModel = new ButtonModel
33+
{
34+
Text = "Click me!",
35+
Click = EventCallback.Factory.Create<MouseEventArgs>(this, ComponentModel_Click),
36+
};
37+
return componentModel;
38+
}
39+
private void ComponentModel_Click() {
40+
application.ShowViewStrategy.ShowMessage("Action is executed!");
4441
}
4542
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
using System;
2-
using DevExpress.ExpressApp.Blazor.Components.Models;
1+
using DevExpress.ExpressApp.Blazor.Components.Models;
2+
using Microsoft.AspNetCore.Components;
3+
using Microsoft.AspNetCore.Components.Web;
34

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;
5+
namespace CustomViewItem.Blazor.Server.Editors.ButtonViewItem;
6+
7+
public class ButtonModel : ComponentModelBase {
8+
public string Text {
9+
get => GetPropertyValue<string>();
10+
set => SetPropertyValue(value);
11+
}
12+
public EventCallback<MouseEventArgs> Click {
13+
get => GetPropertyValue<EventCallback<MouseEventArgs>>();
14+
set => SetPropertyValue(value);
1415
}
16+
17+
public override Type ComponentType => typeof(Button);
1518
}

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

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"CustomViewItem.Blazor.Server": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}

CS/XPO/CustomViewItemXPO/CustomViewItemXPO.Blazor.Server/CustomViewItemXPO.Blazor.Server.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
1818
</Content>
1919
</ItemGroup>
20-
<ItemGroup>
21-
<None Include="Editors\ButtonViewItem\ButtonRenderer.razor" />
22-
</ItemGroup>
2320
<ItemGroup>
2421
<PackageReference Include="DevExpress.ExpressApp.Blazor" Version="25.2.1-alpha-25302" />
2522
<PackageReference Include="DevExpress.ExpressApp.CodeAnalysis" Version="25.2.1-alpha-25302" />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@namespace CustomViewItemXPO.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<MouseEventArgs> Click { get; set; }
10+
}
Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
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;
8+
using Microsoft.AspNetCore.Components.Web;
79

8-
using DevExpress.ExpressApp.Blazor.Components;
10+
namespace CustomViewItemXPO.Blazor.Server.Editors.ButtonViewItem;
11+
12+
public interface IModelButtonDetailViewItemBlazor : IModelViewItem;
913

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

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-
}
25+
RenderFragment IComponentContentHolder.ComponentContent =>
26+
ComponentModelObserver.Create(componentModel, componentModel.GetComponentContent());
27+
void IComplexViewItem.Setup(IObjectSpace objectSpace, XafApplication application) {
28+
this.application = application;
29+
}
30+
31+
protected override object CreateControlCore() {
32+
componentModel = new ButtonModel
33+
{
34+
Text = "Click me!",
35+
Click = EventCallback.Factory.Create<MouseEventArgs>(this, ComponentModel_Click),
36+
};
37+
return componentModel;
38+
}
39+
private void ComponentModel_Click() {
40+
application.ShowViewStrategy.ShowMessage("Action is executed!");
4441
}
45-
}
42+
}
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
using System;
2-
using DevExpress.ExpressApp.Blazor.Components.Models;
1+
using DevExpress.ExpressApp.Blazor.Components.Models;
2+
using Microsoft.AspNetCore.Components;
3+
using Microsoft.AspNetCore.Components.Web;
34

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;
5+
namespace CustomViewItemXPO.Blazor.Server.Editors.ButtonViewItem;
6+
7+
public class ButtonModel : ComponentModelBase {
8+
public string Text {
9+
get => GetPropertyValue<string>();
10+
set => SetPropertyValue(value);
11+
}
12+
public EventCallback<MouseEventArgs> Click {
13+
get => GetPropertyValue<EventCallback<MouseEventArgs>>();
14+
set => SetPropertyValue(value);
1415
}
15-
}
16+
17+
public override Type ComponentType => typeof(Button);
18+
}

0 commit comments

Comments
 (0)