以Xamarin形式动态创建页面 [英] Create Page dynamically in Xamarin Form

查看:165
本文介绍了以Xamarin形式动态创建页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have a text file like this in the cloud

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

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

i want to load this file and inject these codes in my content page in Xamarin Form at runtime

推荐答案

如果您的xml文件并不复杂,并且您不想使用Nuget包.您自己的实现应该非常简单.

If your xml file is not complex and you don't wanna use the Nuget package. Your own implementation should be fairly simple.

假设您在云上的xml文件结构是这样的:

Assuming your xml file structure on cloud is like this:

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

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

Your Dynamic Page Template (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);
        }

    }
}

这篇关于以Xamarin形式动态创建页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆