ToB企服应用市场:ToB评测及商务社交产业平台

标题: deepseek关于【Blazor Hybrid + MAUI + Web 的混合架构中,共享 Razor 组件 [打印本页]

作者: 民工心事    时间: 2025-2-12 21:04
标题: deepseek关于【Blazor Hybrid + MAUI + Web 的混合架构中,共享 Razor 组件
  提问词:【我想在一个dotnet core 8项目中使用 Blazor Hybric 技术,同时构建一个MAUI Project(A,打包得手机端),和一个Auto Render Model 的WebApp Project(W,发布到服务器),我想将两者都用到的Razor文件抽出来作为一个CommonPage中间Project(C,被A和W引用),W中既需要有webAssembly的渲染方式也需要SSR的渲染方式,因为A项目中用到的razor页面肯定是Client(WebAssembly)的渲染方式,我的问题是:C项目中每个Razor页面的RenderModel是否还需要区分为多种方式,还是说只需要设置成webAssembly的方式就可以了,ssr所需的页面组件单独在W里设置?】
  deepseek思索过程:
  嗯,用户的问题是关于在Blazor Hybrid项目中共享Razor组件,同时支持MAUI和WebApp项目,其中WebApp需要同时使用WebAssembly和SSR。他们想知道在公共项目C中,是否需要为每个Razor页面区分不同的渲染模式,还是只需要用WebAssembly模式,然后在Web项目W中单独处置处罚SSR的部分。
  最终它给了我七个策略设计方案,惊艳到我,故在此记录下来:
1. 公共组件库(Project C)的设计原则

 
 
  1. @* SharedComponent.razor *@
  2. @{
  3.     var isMobile =
  4.   #if MAUI
  5.         true;
  6.   #else
  7.         false;
  8.   #endif
  9. }
  10. @if (isMobile)
  11. {
  12.     <MobileOnlyFeature />
  13. }
复制代码
2. MAUI 项目(Project A)配置

  1. @rendermode RenderMode.InteractiveWebAssembly
复制代码
3. Web 项目(Project W)的渲染模式策略

  1. // Web/Components/SSRWrapper.razor
  2. @inherits SharedComponent
  3. @rendermode RenderMode.InteractiveServer
  4. // Web/Components/WASMWrapper.razor
  5. @inherits SharedComponent
  6. @rendermode RenderMode.InteractiveWebAssembly
复制代码
  1. @page "/dynamic-page"
  2. @if (isSSR)
  3. {
  4.     <SSRWrapper />
  5. }
  6. else
  7. {
  8.     <WASMWrapper />
  9. }
  10. @code {
  11.     private bool isSSR = /* 根据请求判断是否需要 SSR */;
  12. }
复制代码
4. 构建配置优化

在公共组件库的 .csproj 中配置多目标框架:
  1. <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst;net8.0</TargetFrameworks>
  2. <SupportedPlatform Condition="'$(TargetFramework)' == 'net8.0'">browser</SupportedPlatform>
复制代码
运行 HTML5. 服务注册策略

在共享库中定义接口,各平台实现:
  1. // Shared
  2. public interface IPlatformService
  3. {
  4.     Task<string> GetPlatformInfo();
  5. }
  6. // MAUI
  7. public class MauiService : IPlatformService { ... }
  8. // Web
  9. public class WebService : IPlatformService { ... }
复制代码
6. 性能优化手段

  1. [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(MobileOnlyComponent))]
  2. public class SharedComponent : ComponentBase
复制代码
7. 调试方案

在 .vscode/launch.json 中配置组合调试:
  1. {
  2.     "configurations": [
  3.         {
  4.             "name": "Debug MAUI + Web",
  5.             "type": "blazorwasm",
  6.             "request": "launch",
  7.             "browser": "edge",
  8.             "hotReload": true,
  9.             "projects": [
  10.                 "ProjectA.csproj",
  11.                 "ProjectW.csproj"
  12.             ]
  13.         }
  14.     ]
  15. }
复制代码
这种架构的关键点在于:
对于需要 SSR 和 WASM 同时存在的页面,发起在 Web 项目中创建路由署理组件:
  1. @page "/hybrid-page"
  2. @rendermode RenderMode.InteractiveAuto
  3. <SharedComponent @rendermode="currentMode" />
  4. @code {
  5.     private IComponentRenderMode currentMode =>
  6.         ShouldUseSSR() ? RenderMode.InteractiveServer : RenderMode.InteractiveWebAssembly;
  7. }
复制代码
这种方案比简单分离渲染模式更灵活,可以:


最后发起在解决方案中添加架构测试:
  1. [Test]
  2. public void SharedComponents_ShouldNotReferenceRenderingSpecificServices()
  3. {
  4.     var sharedAssembly = typeof(SharedComponent).Assembly;
  5.    
  6.     Assert.That(sharedAssembly.GetTypes()
  7.         .SelectMany(t => t.GetMethods())
  8.         .Where(m => m.GetParameters()
  9.             .Any(p => p.ParameterType == typeof(HttpContext))),
  10.         Is.Empty);
  11. }<br>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4