[英]C# Xamarin How to use a existing List in a class?
I am struggling with a problem. 我正在为一个问题而苦苦挣扎。
In my MainActivity.cs I made a List like this: 在我的MainActivity.cs文件中,我做了一个这样的列表:
//Define
public List<products> mItems;
Here I created the List: 在这里,我创建了列表:
mItems = new List<products>();
And over here I fill the List: 在这里,我填写列表:
foreach (var property in Array)
{
//Convert every value in Array to string
var propertyList = JsonConvert.DeserializeObject<List<products>>(property.ToString());
//Add all strings to List
mItems.AddRange(propertyList);
}
Now I want to use this List in another class, so I use this code in the new class (Lets call it "Class2"): 现在,我想在另一个类中使用此List,因此在新类中使用此代码(将其称为“ Class2”):
public List<products> mItems;
But mItems is Null, what am I doing wrong. 但是mItems为Null,我在做什么错。
Again a little update with an Error of course. 再次更新当然有一个错误。
In the MainActivity, I made these changes: 在MainActivity中,我做了以下更改:
private List<products> mItems = new List<products>();
public List<products> ProductList
{
get { return mItems; }
}
And in Class2: 在Class2中:
List<products> mItems = MainActivity.ProductList;
I don't know if I am doing the good way, but my error is: "An object reference is required for the non-static field, method, or property " 我不知道自己是否做得很好,但是我的错误是:“非静态字段,方法或属性需要对象引用”
I have no idea what I have to do! 我不知道该怎么办! Any suggestions?
有什么建议么?
In your Class2, you have to get it from your Class1, just gave to a second list the same name isn't enough. 在Class2中,您必须从Class1中获取它,仅给第二个列表使用相同的名称是不够的。
Here is an example and you should revise the object programming ( https://msdn.microsoft.com/en-us/library/dd460654(v=vs.120).aspx#Properties ) 这是一个示例,您应该修改对象编程( https://msdn.microsoft.com/zh-cn/library/dd460654(v=vs.120).aspx#Properties )
public class Class1 {
public List<products> mItems;
public Class1() {
//Startup WebClient
WebClient client = new WebClient();
//Define URL to download
string link = @"http://websitelink.todownload.json.php";
//Download json website content
string json = new WebClient().DownloadString(link);
//Parse json content
var jObject = JObject.Parse(json);
//Create Array from everything inside Node:"Products"
var productPropery = jObject["Products"] as JArray;
mItems = new List<products>();
foreach (var property in Array)
{
//Convert every value in Array to string
var propertyList = JsonConvert.DeserializeObject<List<products>>(property.ToString());
//Add all strings to List
mItems.AddRange(propertyList);
}
}
}
public class Class2 {
public List<products> mItems;
public Class2() {
var class1Instance = new Class1();
mItems = class1Instance. mItems;
}
}
This answer is too broad to answer definitively, but I shall try help. 这个答案范围太广,无法确切地回答,但我将尽力提供帮助。
Example 1 if you want to pass a list from your main activity to an instance of a class you are creating in the main activity you can pass the list when declaring a new instance of it. 示例1,如果要将列表从主活动传递到要在主活动中创建的类的实例,则可以在声明列表的新实例时传递该列表。
MyListClass listClass = new MyListClass(productList);
then in the class you have something like 然后在课堂上,你会喜欢
public class MyListClass
{
private List<Products> _productList;
public MyListClass(List<Products> productList)
{
_products = productList
}
}
Example 2 If you are looking to pass the list from your main activity to lets say a fragment there are a number of ways to do so. 示例2如果您希望将主要活动中的列表传递给一个片段,则有多种方法可以执行此操作。 you can use the following.
您可以使用以下内容。
ListFragment listFragment = ListFragment.NewInstance(productList);
then in the fragment you have a static class NewInstance like so. 然后在片段中有一个静态类NewInstance,就像这样。
public class ListFragment :Fragment
{
private List<Products> _productList;
public static ListFragment NewInstance(List<Products> productList)
{
_productList = productList;
ListFragment fragment = new ListFragment();
return fragment;
}
}
Example 3 You can use a listener that your mainActivity implements and through the listener call a method that will return a list to you, the reason you use a listener is to decouple your fragment from any activity, an example of this would be 示例3您可以使用mainActivity实现的侦听器,并通过该侦听器调用将向您返回列表的方法,您使用侦听器的原因是将片段与任何活动分离,例如:
public class MainActivity : IListFragmentListener
{
//implement the interface below
public List<Products> GetProductsList()
{
return productsList;
}
}
then your IListFragmentListener.cs 然后你的IListFragmentListener.cs
public interface IListFragmentListener
{
List<Products> GetProductsList();
}
finally in your fragment 终于在你的片段中
public class ListFragment :Fragment
{
private List<Products> _productList;
private IListFragmentListener _listener;
public override void OnAttach(Context context)
{
base.OnAttach(context);
_listener = (IAddDateFragmentListner)context;
}
private void RandomFragmentMethod()
{
_productList = _listener.GetProductsList();
}
}
Activity to Activity Just a quick note on passing a List from one activity to another, as far as I know the cleanest way to do this is to make your list object serializable and serialize it in one activity and Deserialize it in the activity you wish to pass the list too. 从活动到活动只是将一个活动中的列表传递给另一个活动的快速提示,据我所知,最干净的方法是使列表对象可序列化,并在一个活动中将其序列化,然后在您想要的活动中反序列化也通过列表。 This can cause performance issues however
这可能会导致性能问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.