【问题标题】:Is it possible to replace a part of a string with a dynamic 2 way binding?是否可以用动态 2 路绑定替换字符串的一部分?
【发布时间】:2020-08-18 04:16:23
【问题描述】:

目标

提供了一个标签列表(以及一个小胡子或其他样式的字符串模板),我想动态创建一些输入框,然后用输入框中内容的 2 路绑定替换模板中存在的任何参数.我能够创建输入框,但不知道如何将它们的值替换到模板中(或者这在 Blazor 中是否可行)。

示例

    public class TemplateService
    {
        private List<NameValuePairs> _labels = new List<NameValuePairs>
        {
            new NameValuePairs(){Name="Name", Value="John"},
            new NameValuePairs(){Name="Age", Value="27"},
            new NameValuePairs(){Name="Height", Value=""},
            new NameValuePairs(){Name="Hair Colour", Value=""},
            
        };

        public Task<Template> GetTemplateByIdAsync(int id)
        {
            Template t = new Template() { Labels = _labels, Text = @"Hi, my name is {{Name}}. I am {{Age}} years old" };
            return Task.FromResult(t);
        }
    }
@inject TemplateService TemplateService

<h3>Text Templates</h3>

@if (template == null)
{
    <p><em>Loading...</em></p>
}
else
{
    @foreach (var label in template.Labels)
    {
        <label>
            @label.Name :
            <input @bind-value="label.Value" @bind-value:event="oninput" />
        </label>
    }

    <h4>Model</h4>
    @foreach (var l in template.Labels)
    {
        <p>@l.Value</p>
    }

    <h4>Interpolated</h4>
    //HERE I WANT TO DISPLAY A RESULT OF template.text WHEREBY {{Name}} WOULD BE SUBSTITUTED WITH JOHN IN THIS CASE, AND WOULD CHANGE AS THE INPUTBOX FOR NAME IS TYPED IN 
    
}

@code {
    private Template template;

    protected override async Task OnInitializedAsync()
    {
        template = await TemplateService.GetTemplateByIdAsync(1);
    }
}

这是我坚持的视图中的插值位。我可以控制该服务,因此考虑将 {{Age}} 替换为 @FindValueForName("Age") 之类的函数,但不确定是否可以将其作为可执行代码而不是文字字符串插入。有什么想法可以通过任何方式实现吗?

【问题讨论】:

    标签: c# razor .net-core blazor


    【解决方案1】:

    如果你可以重新组织一下,你可以试试这个

    public class Template
    {
     public List<NameValuePairs> Labels { get; set; }
     public Func<string> Text { get; set; }
    }
    
    public class TemplateService
    {
     private List<NameValuePairs> _labels = new List<NameValuePairs>
     {
      new NameValuePairs(){Name="Name", Value="John"},
      new NameValuePairs(){Name="Age", Value="27"},
      new NameValuePairs(){Name="Height", Value=""},
      new NameValuePairs(){Name="Hair Colour", Value=""},
     };
    
     public string GetListValue(string Name) => _labels.Find(lbl=>lbl.Name==Name).Value;
     public Task<Template> GetTemplateByIdAsync(int id)
     {
      Template t = new Template() 
       { 
        Labels = _labels, 
        Text = () => $@"Hi, my name is {GetListValue("Name")}. I am {GetListValue("Age")} years old" };
        return Task.FromResult(t);
     }
    }
    
    ...
    <h4>@template.text()</h4>
    

    【讨论】:

    • 太棒了,这让我走上了正确的道路(完全忘记了美元语法)。如果文本是硬编码的,效果很好。但是,如果文本来自数据库,即作为字符串,该怎么办。有没有办法让字符串执行插值? ... //From the DB I get a string and then maybe convert it to be in the following format string s = "Hi, my name is {GetListValue(\"Name\")}. I am {GetListValue(\"Age\")} years old"; Template t = new Template() { Labels = _labels, //Not sure Text = () =&gt; $"{ s }" }; ...
    猜你喜欢
    • 2017-12-30
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 2015-05-25
    相关资源
    最近更新 更多