C# 通过代码添加多个标签

C# 通过代码添加多个标签,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我正在Visual Studio中使用Xamarin编写一个社交网络应用程序,这是一个带有Android和iOS项目的便携式项目 当MainPage类被导航到时,它会打开一个URL并读取页面并将其保存为字符串。我需要它在每个“”处拆分。我想我应该使用for循环,但我不知道下一步该怎么做。我希望每个区块都有自己的布局框,我还没有学会使用哪个布局以及每个布局最适合什么。在每个框中,它都有一个标签(内容),可能还有一个图像(如果文章附带了一个标签,那么它将是一个URL,因此它将打开URL并显示该图像)

我正在Visual Studio中使用Xamarin编写一个社交网络应用程序,这是一个带有Android和iOS项目的便携式项目

当MainPage类被导航到时,它会打开一个URL并读取页面并将其保存为字符串。我需要它在每个“
”处拆分。我想我应该使用for循环,但我不知道下一步该怎么做。我希望每个区块都有自己的布局框,我还没有学会使用哪个布局以及每个布局最适合什么。在每个框中,它都有一个标签(内容),可能还有一个图像(如果文章附带了一个标签,那么它将是一个URL,因此它将打开URL并显示该图像)

基本上每一个我想能够编辑以下内容

for(string chunk : data.split("<br/">){
    <Layout?>
    <label Text="*Content from the current chunk*"/>
    if(*contains image*){
    <image>*Open url and display the image*</image>
    }
    </Layout?>
}

for(字符串块:data.split(未显示块字符串解析)

MainPage.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"
             xmlns:local="clr-namespace:StackOverflow1"
             x:Class="StackOverflow1.MainPage"
             Appearing="ContentPage_Appearing">    
    <ScrollView>
        <StackLayout x:Name="mainStackLayout" />
    </ScrollView>
</ContentPage>

MainPage.xaml.cs:

    // ...
    public partial class MainPage : ContentPage
    {
        private List<Chunk> chunks = new List<Chunk>();

        // ...

        private void ContentPage_Appearing(object sender, EventArgs e)
        {
            StackLayout childLayout;

            foreach (Chunk chunk in chunks)
            {
                childLayout = new StackLayout();

                Image image = new Image() { Source = chunk.url, HorizontalOptions = LayoutOptions.Start };
                Label label = new Label() { Text = chunk.text, HorizontalOptions = LayoutOptions.Start };

                childLayout.Children.Add(image);
                childLayout.Children.Add(label);

                mainStackLayout.Children.Add(childLayout);
            }
        }
    }
    // ...
/。。。
公共部分类主页:ContentPage
{
私有列表块=新列表();
// ...
出现私有无效内容页_(对象发送方,事件参数e)
{
堆栈布局和子布局;
foreach(块中块)
{
childLayout=新的StackLayout();
Image Image=new Image(){Source=chunk.url,horizontalpoptions=LayoutOptions.Start};
Label Label=new Label(){Text=chunk.Text,horizontalpoptions=LayoutOptions.Start};
childLayout.Children.Add(图像);
childLayout.Children.Add(标签);
mainStackLayout.Children.Add(childLayout);
}
}
}
// ...