简体   繁体   English

以编程方式进行单向 [Parameter] 绑定

[英]One-way [Parameter] binding programatically

I want to set defaults for parameters on a third-party component.我想为第三方组件上的参数设置默认值。 Say I have this:说我有这个:

myBasePage.cs: myBasePage.cs:

public class MyBasePage : ComponentBase
{
  public IEnumerable MyData { get; set; }
}

myPage.razor: myPage.razor:

@inherits MyBasePage
<ThirdPartyComponent Data="@MyData" />

Since Data on ThirdPartyComponent is a [Parameter] with a DataHasChanged() virtual method, by rendering the blazor component like that, I'll get one-way binding, and if I change MyData on my page, programmatically, the component will update.由于ThirdPartyComponent上的Data是具有DataHasChanged()虚方法的[Parameter] ,因此通过像这样呈现blazor 组件,我将获得单向绑定,如果我以编程方式更改页面上的MyData ,该组件将更新。 This will work fine.这将正常工作。

Now, say I can't modify ThirdPartyComponent , but I want to make some defaults in it based on my base page... like so:现在,假设我不能修改ThirdPartyComponent ,但我想根据我的基本页面在其中设置一些默认值......像这样:

myPage.razor: myPage.razor:

@inherits MyBasePage
<MyDerivedComponent PageComponent="@this" />

myDerivedComponent.cs: myDerivedComponent.cs:

public class MyDerivedComponent : ThirdPartyComponent 
{
   [Parameter] public MyBasePage PageComponent { get; set; }
   public override void OnInitialized()
   {
      /* Set other parameter defaults */
      this.OtherParameter = 10;

      /* Bind to Data, as if I was passing it as a parameter in the Razor template */
      this.Data = PageComponent.MyData;
   }
}

This line:这一行:

this.Data = PageComponent.MyData;

Doesn't create a binding at all (and if I modify MyData , the blazor component doesn't get updated).根本不创建绑定(如果我修改MyData ,则 blazor 组件不会更新)。 Is there any way to programmatically create it?有没有办法以编程方式创建它?

Note : the real ThirdPartyComponent includes not only tons of parameters but also templates, etc. For many reasons, I'd like MyDerivedComponent to be of a derived type, and not a "parent component" with a child of ThirdPartyComponent , if that's possible at all).注意:真正的ThirdPartyComponent不仅包括大量参数,还包括模板等。出于多种原因,我希望MyDerivedComponent是派生类型,而不是具有ThirdPartyComponent子项的“父组件”,如果可能的话全部)。

This should work:这应该有效:

public class MyDerivedComponent : ThirdPartyComponent 
{
   [Parameter] public MyBasePage PageComponent { get; set; }
   public override void OnInitialized()
   {
      /* Set other parameter defaults */
      this.OtherParameter = 10;


      //this.Data = PageComponent.MyData;
   }
}

 protected override void OnParametersSet()   
 {
    Data = Data ?? PageComponent?.MyData ;  // determine priority
    base.OnParametersSet();
 }

OnParametersSet() notifies the Component that it has new Parameters. OnParametersSet()通知组件它有新的参数。 The most-derived class can intervene here.最派生的类可以在这里进行干预。

But there is no easy solution for wwhen this.MyData changes, that's out of sight for Blazor.但是当this.MyData发生变化时,没有简单的解决方案,这对 Blazor this.MyData是看不见的。

Is there any way to programmatically create it?有没有办法以编程方式创建它? Of course, but that doesn't mean that you should.当然,但这并不意味着你应该这样做。

Parameters should not be set or manipulated within the component code.不应在组件代码中设置或操作参数 Parameters are set externally whenever SetParametersAsync is called by the Renderer .每当Renderer调用SetParametersAsync时,都会在外部设置参数。 If you change them internally you create two versions of the truth.如果您在内部更改它们,则会创建两个版本的真相。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM