如何从页面向布局参数提供值?

huangapple go评论108阅读模式
英文:

How to provide value for layout's parameters from page?

问题

以下是翻译好的部分:

考虑下面的布局:

  1. @inherits LayoutComponentBase
  2. @layout ProductionsLayout
  3. @Body
  4. <div>
  5. @TrademarkMessage
  6. </div>
  7. @code {
  8. public string TrademarkMessage { get; set; } =
  9. "Doctor Who is a registered trademark of the BBC. " +
  10. "https://www.doctorwho.tv/";
  11. }

这里的TrademarkMessage的值直接在布局代码中提供。

我如何从页面中提供它?

  1. @page "/"
  2. ...
英文:

Consider the layout below:

  1. @inherits LayoutComponentBase
  2. @layout ProductionsLayout
  3. @Body
  4. &lt;div&gt;
  5. @TrademarkMessage
  6. &lt;/div&gt;
  7. @code {
  8. public string TrademarkMessage { get; set; } =
  9. &quot;Doctor Who is a registered trademark of the BBC. &quot; +
  10. &quot;https://www.doctorwho.tv/&quot;;
  11. }

Here the value of TrademarkMessage is provided right in the layout code.

How can I provide it from a page ?

  1. @page &quot;/&quot;
  2. ...

答案1

得分: 1

你需要使用通知模式的一个版本。

首先是一个数据类:

  1. public class LayoutData
  2. {
  3. public string TrademarkMessage { get; private set; } = "My TradeMark Message";
  4. public event EventHandler? DataChanged;
  5. public void SetTradeMarkMessage(string message)
  6. {
  7. this.TrademarkMessage = message;
  8. this.DataChanged?.Invoke(this, EventArgs.Empty);
  9. }
  10. }

布局部分:

  1. @using SO76837241.Data;
  2. @inherits LayoutComponentBase
  3. <PageTitle>SO76837241</PageTitle>
  4. <div class="page">
  5. <div class="sidebar">
  6. <NavMenu />
  7. </div>
  8. <main>
  9. <div class="top-row px-4">
  10. <span>@_layoutData.TrademarkMessage</span>
  11. <a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
  12. </div>
  13. <article class="content px-4">
  14. <CascadingValue Value="_layoutData" IsFixed>
  15. @Body
  16. </CascadingValue>
  17. </article>
  18. </main>
  19. </div>
  20. @code {
  21. private LayoutData _layoutData = new();
  22. protected override void OnInitialized()
  23. {
  24. // As this is internal to the component we don't need to de-register
  25. _layoutData.DataChanged += (object? sender, EventArgs e) => this.StateHasChanged();
  26. }
  27. }

和索引部分:

  1. @page "/"
  2. @using SO76837241.Data;
  3. <PageTitle>Index</PageTitle>
  4. <h1>Hello, world!</h1>
  5. Welcome to your new app.
  6. <SurveyPrompt Title="How is Blazor working for you?" />
  7. @code {
  8. [CascadingParameter] private LayoutData? _layoutData { get; set; }
  9. protected override void OnInitialized()
  10. {
  11. if (_layoutData is not null)
  12. _layoutData.SetTradeMarkMessage("Total Bull!");
  13. }
  14. }
英文:

You need to use a version of the notification pattern.

First a data class:

  1. public class LayoutData
  2. {
  3. public string TrademarkMessage { get; private set; } = &quot;My TradeMark Message&quot;;
  4. public event EventHandler? DataChanged;
  5. public void SetTradeMarkMessage(string message)
  6. {
  7. this.TrademarkMessage = message;
  8. this.DataChanged?.Invoke(this, EventArgs.Empty);
  9. }
  10. }

the Layout:

  1. @using SO76837241.Data;
  2. @inherits LayoutComponentBase
  3. &lt;PageTitle&gt;SO76837241&lt;/PageTitle&gt;
  4. &lt;div class=&quot;page&quot;&gt;
  5. &lt;div class=&quot;sidebar&quot;&gt;
  6. &lt;NavMenu /&gt;
  7. &lt;/div&gt;
  8. &lt;main&gt;
  9. &lt;div class=&quot;top-row px-4&quot;&gt;
  10. &lt;span&gt;@_layoutData.TrademarkMessage&lt;/span&gt;
  11. &lt;a href=&quot;https://docs.microsoft.com/aspnet/&quot; target=&quot;_blank&quot;&gt;About&lt;/a&gt;
  12. &lt;/div&gt;
  13. &lt;article class=&quot;content px-4&quot;&gt;
  14. &lt;CascadingValue Value=&quot;_layoutData&quot; IsFixed&gt;
  15. @Body
  16. &lt;/CascadingValue&gt;
  17. &lt;/article&gt;
  18. &lt;/main&gt;
  19. &lt;/div&gt;
  20. @code {
  21. private LayoutData _layoutData = new();
  22. protected override void OnInitialized()
  23. {
  24. // As this is internal to the component we don&#39;t need to de-register
  25. _layoutData.DataChanged += (object? sender, EventArgs e) =&gt; this.StateHasChanged();
  26. }
  27. }

And Index:

  1. @page &quot;/&quot;
  2. @using SO76837241.Data;
  3. &lt;PageTitle&gt;Index&lt;/PageTitle&gt;
  4. &lt;h1&gt;Hello, world!&lt;/h1&gt;
  5. Welcome to your new app.
  6. &lt;SurveyPrompt Title=&quot;How is Blazor working for you?&quot; /&gt;
  7. @code {
  8. [CascadingParameter] private LayoutData? _layoutData { get; set; }
  9. protected override void OnInitialized()
  10. {
  11. if (_layoutData is not null)
  12. _layoutData.SetTradeMarkMessage(&quot;Total Bull!&quot;);
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年8月4日 23:17:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定