我使用xamarin.forms,需要创建一长串我从XML文件读取的数据列表。 (XML作为“资产”包含在APK中)。该列表将被读取一次,并将在整个应用程序中使用(只读)。列表包含我正在处理的世界的基本数据。列表的每个节点都有6个元素。
我已选择将其代码放入MainActivity.cs中,其依据是全局可用的列表(这似乎是一个错误的假设)。该应用程序读取XML(每个列表项所有6个Data字段),并且数据恰好到达名为“ _ Proben”] >>的列表中。我的基本问题是,创建的列表在应用程序的其他页面中不可用。(尝试使用Singleton类方法对我来说不起作用。给出的建议通常不够具体,因为我需要知道我需要在Xamarin中的哪个位置嵌入代码,以及我需要在哪里“使用”什么。)抽象建议没有帮助我,没有占位符的代码最适合我。)然后,我尝试将代码从MainActivity.cs移到特定页面“ page1.xaml.cs”,目的是至少在该本地环境中创建列表(仍然不是全局环境,但至少可以在此处进行向下选择)。我无法运行page1.xaml.cs中的代码:它将不接受AssetManager并给我错误“ 找不到CS0246类型或名称空间“ AssetManager” ...“
我迷路了。
Q1)我需要做什么,才能运行page1.xaml.cs中的以下代码? (在MainActivity.cs中运行良好的代码)
Q2)甚至更好:如何使该列表(每个条目包含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
我使用xamarin.forms,需要创建一长串我从XML文件读取的数据列表。 (XML作为“资产”包含在APK中)。该列表将被读取一次,并将在整个...中使用(只读)...
根据您的描述,我想您想加载xml文件以获取数据,然后在ContentPage中使用此数据,对吗?