Skip to content

Commit 7ded048

Browse files
committed
doc: 增加二维码生成示例
1 parent 8be51f2 commit 7ded048

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
@namespace BootstrapBlazor.Server.Components.Samples.Tutorials
2+
@page "/tutorials/barcode"
3+
4+
<div class="bc-main">
5+
<div class="row g-3">
6+
<div class="col-12 col-sm-6">
7+
<h5>选择一个类型</h5>
8+
<div class="bc-type-list">
9+
@foreach (var item in _contents)
10+
{
11+
<span class="@GetItemClassString(item)" @onclick="@(() => OnActiveType(item))">@item</span>
12+
}
13+
</div>
14+
<div class="bc-content-main">
15+
@if (_activeContentType == "Text")
16+
{
17+
<ValidateForm Model="_textContent" OnValidSubmit="OnTextSubmit">
18+
<EditorForm TModel="TextContent" ItemsPerRow="1">
19+
<FieldItems>
20+
<EditorItem @bind-Field="@context.Content" Rows="3" Text="Text"></EditorItem>
21+
</FieldItems>
22+
</EditorForm>
23+
<div class="form-footer">
24+
<Button ButtonType="@ButtonType.Submit" Text="Submit" />
25+
</div>
26+
</ValidateForm>
27+
}
28+
else if (_activeContentType == "Url")
29+
{
30+
<ValidateForm Model="_urlContent" OnValidSubmit="OnUrlSubmit">
31+
<EditorForm TModel="TextContent" ItemsPerRow="1">
32+
<FieldItems>
33+
<EditorItem @bind-Field="@context.Content" PlaceHolder="https://" Text="Url"></EditorItem>
34+
</FieldItems>
35+
</EditorForm>
36+
<div class="form-footer">
37+
<Button ButtonType="@ButtonType.Submit" Text="Submit" />
38+
</div>
39+
</ValidateForm>
40+
}
41+
else if (_activeContentType == "Wi-Fi")
42+
{
43+
<ValidateForm Model="_wifiContent" OnValidSubmit="OnWiFiSubmit">
44+
<EditorForm TModel="WiFiContent" ItemsPerRow="1">
45+
<FieldItems>
46+
<EditorItem @bind-Field="@context.Password" ComponentType="typeof(BootstrapPassword)"></EditorItem>
47+
</FieldItems>
48+
</EditorForm>
49+
<div class="form-footer">
50+
<Button ButtonType="@ButtonType.Submit" Text="Submit" />
51+
</div>
52+
</ValidateForm>
53+
}
54+
else if (_activeContentType == "Email")
55+
{
56+
<ValidateForm Model="_emailContent" OnValidSubmit="OnEmailSubmit">
57+
<EditorForm TModel="EmailContent" ItemsPerRow="1"></EditorForm>
58+
<div class="form-footer">
59+
<Button ButtonType="@ButtonType.Submit" Text="Submit" />
60+
</div>
61+
</ValidateForm>
62+
}
63+
</div>
64+
</div>
65+
<div class="col-12 col-sm-6">
66+
<h5>预览</h5>
67+
<div class="bc-qr-content">
68+
@if (!string.IsNullOrEmpty(_content))
69+
{
70+
<QRCode Content="@_content"></QRCode>
71+
}
72+
</div>
73+
</div>
74+
</div>
75+
</div>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using Microsoft.AspNetCore.Components.Forms;
7+
8+
namespace BootstrapBlazor.Server.Components.Samples.Tutorials;
9+
10+
/// <summary>
11+
/// BarCodeGenerator 组件示例代码
12+
/// </summary>
13+
public partial class BarCodeGenerator
14+
{
15+
private string _activeContentType = "Text";
16+
17+
private readonly List<string> _contents = ["Text", "Url", "Wi-Fi", "Email"];
18+
19+
private readonly WiFiContent _wifiContent = new();
20+
21+
private readonly EmailContent _emailContent = new();
22+
23+
private readonly TextContent _textContent = new();
24+
25+
private readonly TextContent _urlContent = new();
26+
27+
private string? _content;
28+
29+
private string? GetItemClassString(string item) => CssBuilder.Default("bc-type-item")
30+
.AddClass("active", _activeContentType == item)
31+
.Build();
32+
33+
private void OnActiveType(string item)
34+
{
35+
_activeContentType = item;
36+
}
37+
38+
private Task OnTextSubmit(EditContext context)
39+
{
40+
_content = _textContent.ToString();
41+
StateHasChanged();
42+
return Task.CompletedTask;
43+
}
44+
45+
private Task OnUrlSubmit(EditContext context)
46+
{
47+
_content = _urlContent.ToString();
48+
StateHasChanged();
49+
return Task.CompletedTask;
50+
}
51+
52+
private Task OnWiFiSubmit(EditContext context)
53+
{
54+
_content = _wifiContent.ToString();
55+
StateHasChanged();
56+
return Task.CompletedTask;
57+
}
58+
59+
private Task OnEmailSubmit(EditContext context)
60+
{
61+
_content = _emailContent.ToString();
62+
StateHasChanged();
63+
return Task.CompletedTask;
64+
}
65+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.bc-type-list {
2+
display: flex;
3+
align-items: center;
4+
gap: 0.5rem 0.5rem;
5+
margin-bottom: 1rem;
6+
}
7+
8+
.bc-type-item {
9+
padding: .25rem 1rem;
10+
border: 2px solid var(--bb-primary-color);
11+
border-radius: 50px;
12+
line-height: 1.8em;
13+
cursor: pointer;
14+
}
15+
16+
.bc-type-item.active {
17+
background-color: var(--bb-primary-color);
18+
color: var(--bs-white);
19+
}

0 commit comments

Comments
 (0)