首页 > 解决方案 > 如何从解决方案中的文件加载 XML 文件,使用嵌入式资源构建?项目 > 跨平台 > Xamarin.Forms

问题描述

如何从解决方案中的文件加载 XML 文件,使用嵌入式资源构建?跨平台 (Xamarin.Forms) - VS

XDocument xDoc = XDocument.Load("Data.xml");

这是我的解决方案的概要: 这是我的解决方案的概要。图像

整页代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using System.Xml.Linq;
using Xamarin.Forms.Xaml;

namespace App14
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ListMain : ContentPage
    {

        public List<string> Students; 

        public ListMain ()
        {

            InitializeComponent ();
            Students = new List<string>(); 
            XDocument xDoc = XDocument.Load("Data.xml");
            foreach (XElement xe in xDoc.Element("students").Elements("mainStudent"))
            {
                Students.Add(xe.Element("student").Value); 
            }

            foreach (string student in Students)
            {
               stackLayout.Children.Add(new Label { Text = student, FontSize = 20, HorizontalOptions = LayoutOptions.StartAndExpand });
               stackLayout.Children.Add(new Label { Text = "DEL", FontSize = 20, HorizontalOptions = LayoutOptions.End });
            }


        }


    }
}

标签: c#xmlvisual-studioxamarincross-platform

解决方案


如果你想要一个交叉嵌入的资源,你应该在一个共享或 PCL 项目中放置一个文件。

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=vswin#embedded-images


推荐阅读