我有一个xml模板文件。我需要将它“变成”代码。添加一些属性,然后以编程方式添加到父容器。NaiveScript:如何使用xml文件构建模块,而不是模板字符串
XML模板(这是在真正的应用程序要复杂得多,这只是举例):
<StackLayout id="{{ 'recipe_' + recipe_id }}">
<Label text="{{ title }}"/>
<Label text="{{ description }}"/>
<Button text="Choose" onTap="onRecipeBtnTap"/>
</StackLayout>
代码:
let recipeCard = builder.parse("~/templates/recipe.xml") //<-- problem here
let slide = new slide.Slide()
slide.addChild(recipeCard)
slidesContainer.add(slide)
的问题是,建造器模块需要XML字符串不一条路径。所以,我首先需要将xml文件转换为字符串。请告知这里最好的办法是什么。