C# 如何获得一个;列表“;在其他页面上提供复杂节点

C# 如何获得一个;列表“;在其他页面上提供复杂节点,c#,list,xamarin.forms,global,xmlnode,C#,List,Xamarin.forms,Global,Xmlnode,我使用xamarin.forms,需要创建一长串从XML文件读取的数据。(XML作为“资产”包含在APK中)。该列表读取一次,并应在整个应用程序中使用(只读)。该列表包含我工作的世界的基础数据。列表的每个节点都有6个元素 我选择将代码放在MainActivity.cs中,并给出我可以得到的基本原理 该列表全局可用(这似乎是一个错误的假设)。该应用程序读取XML(每个列表项都有6个数据字段),数据正好到达一个名为“\u Proben”的列表中。 我的基本问题是,创建的列表在应用程序的其他页面中不可

我使用xamarin.forms,需要创建一长串从XML文件读取的数据。(XML作为“资产”包含在APK中)。该列表读取一次,并应在整个应用程序中使用(只读)。该列表包含我工作的世界的基础数据。列表的每个节点都有6个元素

我选择将代码放在MainActivity.cs中,并给出我可以得到的基本原理 该列表全局可用(这似乎是一个错误的假设)。该应用程序读取XML(每个列表项都有6个数据字段),数据正好到达一个名为“\u Proben”的列表中。 我的基本问题是,创建的列表在应用程序的其他页面中不可用。 (尝试应用单例类方法对我不起作用。给出的建议通常不够具体,因为我需要知道在Xamarin中我需要在哪里嵌入代码,以及我需要在哪里“使用”什么。抽象建议对我没有帮助,没有占位符的代码最适合我。) 然后我尝试将代码从MainActivity.cs转移到特定的页面“page1.xaml.cs”,目的是至少在本地环境中创建列表(仍然不是全局的,但至少我可以在那里向下选择)。 我无法让它运行page1.xaml.cs中的代码:它将不接受AssetManager,并给出错误“CS0246找不到类型或命名空间”AssetManager

我迷路了

Q1)要在page1.xaml.cs中运行下面的代码,我需要做什么?(在MainActivity.cs中运行良好的代码)

问题2)更好的是:我如何使该列表(每个条目包含6个元素)可以全局读取

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    // Liste erstellen aller Proben
    public static List<ProbenKlasse> _Proben = new List<ProbenKlasse>();protected override     
    void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());

        Int16 i = 0;       
        // open the file from the asset folder
        AssetManager assets = this.Assets;
        StreamReader sr = new StreamReader(assets.Open("ProbenDB2.xml"));
        XmlDocument doc = new XmlDocument();
        doc.Load(sr);
        // now initializing the reader 
        XmlNodeReader nodeRead = new XmlNodeReader(doc);
        XmlNodeList xnList = doc.SelectNodes("/ProbenDS/Probe");
        // Beginn reading the XML
        i = 0; // start counting how many data sets were in the XML
        String text1;  // just data containers for the 6 data fields of each entry in the list
        String text2;  // just data containers for the 6 data fields of each entry in the list
        Int16 c1; // just data containers for the 6 data fields of each entry in the list
        Int16 c2; // just data containers for the 6 data fields of each entry in the list
        Int16 c3; // just data containers for the 6 data fields of each entry in the list
        Int16 c4; // just data containers for the 6 data fields of each entry in the list
        foreach (XmlNode xn in xnList)
        {
            ProbenKlasse aktprob = new ProbenKlasse(); 
            text1 =              xn["Titel"].InnerText;       aktprob.Titel = text1;
            c1 = Convert.ToInt16(xn["Probentyp"].InnerText);  aktprob.Probentyp = c1;
            c2 = Convert.ToInt16(xn["Wurf1"].InnerText);      aktprob.Wurf1 = c2;
            c3 = Convert.ToInt16(xn["Wurf2"].InnerText);      aktprob.Wurf2 = c3;
            c4 = Convert.ToInt16(xn["Wurf3"].InnerText);      aktprob.Wurf3 = c4;
            text2 =              xn["Beschreibung"].InnerText; aktprob.Beschreibung = text2;
            _Proben.Add(aktprob); // adding the newly read data set to the list
            i = ++i;
        }
        GlobalVariables.AnzProben = i;  // letting the app know how many data sets we have
public class main活动:global::Xamarin.Forms.Platform.Android.formsappactivity
{
//李斯特·埃斯泰伦·阿勒·普罗本
公共静态列表_Proben=new List();受保护的覆盖
创建时无效(Bundle savedInstanceState)
{
TabLayoutResource=Resource.Layout.Tabbar;
ToolbarResource=Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(这个,savedInstanceState);
全局::Xamarin.Forms.Forms.Init(这个,savedInstanceState);
加载应用程序(新应用程序());
int16i=0;
//从资产文件夹中打开文件
AssetManager资产=此.assets;
StreamReader sr=新的StreamReader(assets.Open(“ProbenDB2.xml”);
XmlDocument doc=新的XmlDocument();
文件加载(sr);
//现在初始化读取器
XmlNodeReader nodeRead=新的XmlNodeReader(doc);
XmlNodeList xnList=doc.SelectNodes(“/ProbenDS/Probe”);
//Beginn阅读XML
i=0;//开始计算XML中有多少数据集
String text1;//只是列表中每个条目的6个数据字段的数据容器
String text2;//只是列表中每个条目的6个数据字段的数据容器
Int16 c1;//仅用于列表中每个条目的6个数据字段的数据容器
Int16 c2;//仅用于列表中每个条目的6个数据字段的数据容器
Int16 c3;//仅用于列表中每个条目的6个数据字段的数据容器
Int16 c4;//仅为列表中每个条目的6个数据字段提供数据容器
foreach(xnList中的XmlNode xn)
{
ProbenKlasse aktprob=新的ProbenKlasse();
text1=xn[“Titel”].InnerText;aktprob.Titel=text1;
c1=Convert.ToInt16(xn[“Probentyp”].InnerText);aktprob.Probentyp=c1;
c2=Convert.ToInt16(xn[“Wurf1”].InnerText);aktprob.Wurf1=c2;
c3=Convert.ToInt16(xn[“Wurf2”].InnerText);aktprob.Wurf2=c3;
c4=Convert.ToInt16(xn[“Wurf3”].InnerText);aktprob.Wurf3=c4;
text2=xn[“Beschreibung”].InnerText;aktprob.Beschreibung=text2;
_Proben.Add(aktprob);//将新读取的数据集添加到列表中
i=++i;
}
GlobalVariables.AnzProben=i;//让应用程序知道我们有多少数据集

根据您的描述,我猜您希望加载xml文件以获取数据,然后在ContentPage中使用此数据,对吗

如果是,我建议您可以在Xamarin.forms中使用DependencyService

这是DependencyService,您可以查看:

我做了一个关于使用DependencyService从Android获取xml文件到表单的示例,您可以看看:

首先,在表单中创建接口:

public interface IClick
{

    string GetContent();
}
内容页:

private void Button_Clicked(object sender, EventArgs e)
    {
        string content = DependencyService.Get<IClick>().GetContent();
    }
如果我的回复对您有帮助,请记住将我的回复标记为答案,谢谢

   [assembly: Xamarin.Forms.Dependency(typeof(MainActivity))]
 namespace App3.Droid
 {
   [Activity(Label = "App3", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,IClick
{
    public string GetContent()
    {
        string content="";
        AssetManager asset = Application.Context.Assets;
        using (StreamReader sr = new StreamReader(asset.Open("note.xml")))
        {
            content = sr.ReadToEnd();
        }
        return content;
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
  }
 }