android - 在 Xamarin 表单中动态创建页面

标签 android ios xamarin xamarin.forms cross-platform

我在云端有一个这样的文本文件

<Entry Placeholder ="FirstName" />
<Entry Placeholder ="Last Name" />

我想加载这个文件并在运行时将这些代码注入(inject)到 Xamarin Form 的内容页面中

最佳答案

如果您的 xml 文件不复杂并且您不想使用 Nuget 包。您自己的实现应该相当简单。

假设你在云端的xml文件结构是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<Form>
    <Entry Placeholder ="First Name" />
    <Entry Placeholder ="Last Name" />
</Form>

您的动态页面模板 (YourForm.Xaml):

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Company.Project.Feature.Views.YourForm">
             <StackLayout Spacing="6" x:Name="EntryFieldsStack">
                <!--Currently this is empty. Children are added in the code behing after loading the xml file from the cloud-->
             </StackLayout>
</ContentPage> 

在后面的代码中:

namespace Company.Project.Feature.Views
{
    public partial class YourForm:ContentPage
    {
        InitializeComponent();
        LoadDynamicFields();
    }
    public void LoadDynamicFields()
    {
        //load the file as an XDocument from the url
        var doc = System.Xml.Linq.XDocument.Load("http://example.com/xml-file-url");
        foreach (var entry in doc.Descendants("Entry")
                .Select(e => new Entry {Placeholder = e.Attribute("PlaceHolder")?.Value}))
        {
            EntryFieldsStack.Children.Add(entry);
        }

    }
}

关于android - 在 Xamarin 表单中动态创建页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45866110/

相关文章:

ios - 条件为 "one time"

ios - 阻止 Chrome for iOS 打开 "Open in New Tab .. Copy Link URL"弹出窗口

android - 在 YUV 颜色空间中操纵亮度

java - 将两个 numberPicker 值小时和分钟组合成一个倒数计时器

ios - 使用 IOS 应用程序进行应用内购买

c# - 更改布局大小取决于屏幕大小

c# - 无法在 Xaml 的嵌套类上设置属性

c# - 更改文本选择颜色(强调色)Xamarin Android

android - 无法从 PendingIntent 触发地理围栏转换 IntentService

android - ActionBarSherlock:getSupportMenuInflator() 未定义