From 4f35288a7df303bf30b0a4f18293f7a57652f125 Mon Sep 17 00:00:00 2001 From: william19941994 <59403632+william19941994@users.noreply.github.com> Date: Fri, 29 Oct 2021 08:10:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E4=BB=A3=E7=A0=81=E6=AE=B5=E6=94=BE?= =?UTF-8?q?=E5=85=A5=E4=BA=86Block=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 具体修改: 1 ExampleExtensions.cs 的类增加了BlockIndex和缓存字典, GetCodeAsync函数增加了Block,增加了ResetCache函数。 服务注册为AddScoped。 2 Block增加了个Pre标签,用来显示代码--- TODO:界面暂时不怎么友好,待根据用户意见反馈优化 3 Block.razor.cs 增加了从爷爷控件传过来的RazorFileName 4 ComponentLayout增加了传递RazorFileName,以及OnParameterSet的时候清空ExampleService的BlockIndex。 --- .../Extensions/ExampleExtensions.cs | 74 ++++++++++++++++--- .../Pages/Components/Block.razor | 3 + .../Pages/Components/Block.razor.cs | 7 ++ .../Pages/Components/Pre.razor.cs | 9 ++- .../Shared/ComponentLayout.razor | 4 +- .../Shared/ComponentLayout.razor.cs | 7 +- 6 files changed, 91 insertions(+), 13 deletions(-) diff --git a/src/BootstrapBlazor.Shared/Extensions/ExampleExtensions.cs b/src/BootstrapBlazor.Shared/Extensions/ExampleExtensions.cs index 9628c2974..3ebed5bf7 100644 --- a/src/BootstrapBlazor.Shared/Extensions/ExampleExtensions.cs +++ b/src/BootstrapBlazor.Shared/Extensions/ExampleExtensions.cs @@ -5,6 +5,7 @@ using BootstrapBlazor.Shared; using Microsoft.Extensions.Options; using System; +using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; @@ -22,7 +23,7 @@ namespace Microsoft.Extensions.DependencyInjection /// public static IServiceCollection AddExampleService(this IServiceCollection services) { - services.AddTransient(); + services.AddScoped(); return services; } } @@ -35,6 +36,8 @@ namespace Microsoft.Extensions.DependencyInjection private HttpClient Client { get; set; } private string ServerUrl { get; set; } + static private Dictionary RazorFileCache { get; set; } = new Dictionary(); + private int BlockIndex = 0; //服务注册成scoped后,访问第二个Page不会清零,所以加了个ResetCache函数。TODO:多人不知道是否有bug /// /// 构造方法 @@ -48,25 +51,76 @@ namespace Microsoft.Extensions.DependencyInjection Client.BaseAddress = new Uri(options.Value.RepositoryUrl); ServerUrl = options.Value.ServerUrl; - } + //BlockIndex = 0;//TODO:测试一次请求是否从0开始, + } + public void ResetCache() + { + BlockIndex = 0; + //FileCacheName = string.Empty; + //FileCacheContent = string.Empty; + } /// - /// 获得组件版本号方法 + /// 获得组件代码段的方法 /// + /// *.razor文件名 + /// Block的标题,优先使用,但是有些是变量计算出来的就找不到了 /// - public async Task GetCodeAsync(string CodeFile) + public async Task GetCodeAsync(string CodeFile,string ? BlockTitle) { - var content = ""; + var content = string.Empty; + int myBlockIndex = 0; + if (!string.IsNullOrWhiteSpace(BlockTitle)) + { + myBlockIndex = BlockIndex++; //提前+1, 渲染整个文件的时候不会到这里。 + } try { - if (OperatingSystem.IsBrowser()) + if(CodeFile.EndsWith(".razor")) + { + //RazorCodeFileName = System.IO.Path.ChangeExtension(RazorCodeFileName, ".txt"); + if(RazorFileCache.ContainsKey(CodeFile)) + { + content = RazorFileCache[CodeFile]; + } + } + if(content==string.Empty) { - Client.BaseAddress = new Uri($"{ServerUrl}/api/"); - content = await Client.GetStringAsync($"Code?fileName={CodeFile}"); + if (OperatingSystem.IsBrowser()) + { + Client.BaseAddress = new Uri($"{ServerUrl}/api/"); + content = await Client.GetStringAsync($"Code?fileName={CodeFile}"); + } + else + { + content = await Client.GetStringAsync(CodeFile); + } + //if (RazorCodeFileName.EndsWith(".txt")) + if (CodeFile.EndsWith(".razor")) + { + //lock(this) + { + if (!RazorFileCache.ContainsKey(CodeFile)) + { + RazorFileCache[CodeFile] = content ; + //BlockIndex = 0;//BlockTitle==null?0:1; + } + } + } } - else + if(!string.IsNullOrWhiteSpace(BlockTitle)) { - content = await Client.GetStringAsync(CodeFile); + string[] segments = content.Split(new string[] { "" }, StringSplitOptions.None); + for(int i=1;i"; + if (codeSeg.Contains("Title=\"" + BlockTitle + "\"")) + return codeSeg; + } + if(BlockIndex*2-1"; + //按BlockIndex匹配失败,暂时先返回所有的内容。 + return CodeFile + "{" + myBlockIndex.ToString() + "}" + BlockTitle + "\r\n" + content; } } catch (HttpRequestException) { content = "网络错误"; } diff --git a/src/BootstrapBlazor.Shared/Pages/Components/Block.razor b/src/BootstrapBlazor.Shared/Pages/Components/Block.razor index 2ac6ade13..c7e3b121c 100644 --- a/src/BootstrapBlazor.Shared/Pages/Components/Block.razor +++ b/src/BootstrapBlazor.Shared/Pages/Components/Block.razor @@ -10,4 +10,7 @@
@ChildContent
+ diff --git a/src/BootstrapBlazor.Shared/Pages/Components/Block.razor.cs b/src/BootstrapBlazor.Shared/Pages/Components/Block.razor.cs index 5c7a89760..97367f56a 100644 --- a/src/BootstrapBlazor.Shared/Pages/Components/Block.razor.cs +++ b/src/BootstrapBlazor.Shared/Pages/Components/Block.razor.cs @@ -2,9 +2,11 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using BootstrapBlazor.Components; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; namespace BootstrapBlazor.Shared.Pages.Components { @@ -25,6 +27,11 @@ namespace BootstrapBlazor.Shared.Pages.Components ///
[Parameter] public string Introduction { get; set; } = "未设置"; + /// + /// 文件名 从ComponentLayout传递过来的razor文件名 + /// + [CascadingParameter(Name = "RazorFileName")] + public string? RazorFileName { get;set;} /// /// 获得/设置 组件内容 diff --git a/src/BootstrapBlazor.Shared/Pages/Components/Pre.razor.cs b/src/BootstrapBlazor.Shared/Pages/Components/Pre.razor.cs index 57b5588d5..e7805a3d1 100644 --- a/src/BootstrapBlazor.Shared/Pages/Components/Pre.razor.cs +++ b/src/BootstrapBlazor.Shared/Pages/Components/Pre.razor.cs @@ -61,6 +61,13 @@ namespace BootstrapBlazor.Shared.Pages.Components [Parameter] public string? CodeFile { get; set; } + + /// + /// 获得/设置 代码段的标题 + /// + [Parameter] + public string? BlockTitle { get; set; } = null; + /// /// OnInitializedAsync 方法 /// @@ -96,7 +103,7 @@ namespace BootstrapBlazor.Shared.Pages.Components { if (!string.IsNullOrEmpty(CodeFile)) { - var code = await Example.GetCodeAsync(CodeFile); + var code = await Example.GetCodeAsync(CodeFile, BlockTitle); if (!string.IsNullOrEmpty(code)) { ChildContent = builder => diff --git a/src/BootstrapBlazor.Shared/Shared/ComponentLayout.razor b/src/BootstrapBlazor.Shared/Shared/ComponentLayout.razor index f8bf696a0..d6c5bb1c5 100644 --- a/src/BootstrapBlazor.Shared/Shared/ComponentLayout.razor +++ b/src/BootstrapBlazor.Shared/Shared/ComponentLayout.razor @@ -17,7 +17,9 @@ - @Body + + @Body +

diff --git a/src/BootstrapBlazor.Shared/Shared/ComponentLayout.razor.cs b/src/BootstrapBlazor.Shared/Shared/ComponentLayout.razor.cs
index c3e32351e..b7c628aff 100644
--- a/src/BootstrapBlazor.Shared/Shared/ComponentLayout.razor.cs
+++ b/src/BootstrapBlazor.Shared/Shared/ComponentLayout.razor.cs
@@ -53,6 +53,11 @@ namespace BootstrapBlazor.Shared.Shared
         [NotNull]
         private IJSRuntime? JSRuntime { get; set; }
 
+
+        [Inject]
+        [NotNull]
+        private Microsoft.Extensions.DependencyInjection.ExampleService? ExampleSvc { get; set; }
+
         [NotNull]
         private Tab? TabSet { get; set; }
 
@@ -74,7 +79,7 @@ namespace BootstrapBlazor.Shared.Shared
         protected override void OnParametersSet()
         {
             base.OnParametersSet();
-
+            ExampleSvc.ResetCache();
             var page = Navigator.ToBaseRelativePath(Navigator.Uri);
             var comNameWithHash = page.Split("/").LastOrDefault() ?? string.Empty;
             var comName = comNameWithHash.Split("#").FirstOrDefault() ?? string.Empty;
-- 
Gitee