【问题标题】:In case of Multiple Array Lists, can I store them somewhere and call them, or need to create separate class for each AL如果是多个数组列表,我可以将它们存储在某处并调用它们,还是需要为每个 AL 创建单独的类
【发布时间】:2020-10-30 19:51:35
【问题描述】:

在我的应用程序中,我有多个数组列表(在我的情况下超过 20 个),试图找到一种方法将它们存储在某个地方并在可能的情况下将它们回调。就像 VB 模块中的旧方法一样。这是我的代码!

模型类代码

公开课联系人

public class Contacts
{
    [PrimaryKey][AutoIncrement]
    public int Contact_ID { get; set; }
    public string Contact_Name { get; set; }
    public string Contact_Address { get; set; }
    public string Contact_eMail { get; set; }
}

主页

`public partial class MainPage : ContentPage
{
    readonly SQLiteAsyncConnection _connection 
        = DependencyService.Get<ISQLite>().GetConnection();

    public MainPage()
    {
        InitializeComponent();
        this.BindingContext = this;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        // I have more than 20 This type of Array Lists, and some contains 
        more than 30 items 
        
        int[] intArray;
        intArray = new int[4];
        intArray[0] = 13;
        intArray[1] = 14;
        intArray[2] = 18;
        intArray[3] = 21;

        var list = _connection.Table<Contacts>().ToListAsync().Result;
        var myAL = new ArrayList();
        foreach (int rowList in intArray)
        {
            var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));
            myAL.Add(NewItem.FirstOrDefault());
        }

        listView.ItemsSource = myAL;
    }
}`

XAML 页面

`<StackLayout>
    <ListView x:Name="listView" HasUnevenRows="True"  IsVisible="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand">
                        <Frame BackgroundColor="LightYellow">
                            <StackLayout Orientation="Vertical" VerticalOptions="Center" >
                                <Label Text="{Binding Contact_Name}" HorizontalOptions="StartAndExpand" TextColor="Black" FontSize="Medium"></Label>
                                <Label Text="{Binding Contact_Address}" HorizontalOptions="StartAndExpand" TextColor="Black" FontSize="Medium"></Label>
                                <Label Text="{Binding Contact_eMail}" HorizontalOptions="StartAndExpand" TextColor="Black" FontSize="Medium"></Label>
                            </StackLayout>
                        </Frame>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>`

Screen shot

【问题讨论】:

  • 您可以将 ArrayLists 转换为 json 字符串,然后通过键值对Preferences 保存并获取它。
  • 感谢@Leo Zhu - MSFT 的回复,我会检查一下,会通知您

标签: xamarin arraylist xamarin.forms


【解决方案1】:

这是一个简单的示例:

为数组列表创建类MyArrayData(例如这里有两个列表):

public class MyArrayData
{
   public IList<string> StringArray { get; set; }
   public IList<int> IntArray { get; set; }
}

那么你可以像这样保存它:

MyArrayData dataArray = new MyArrayData() { IntArray = new List<int>() { 1,2, 3, 4 }, StringArray = new List<string>() { "A", "B", "C" } };
string jsonString = JsonConvert.SerializeObject(dataArray);
Preferences.Set("list", jsonString);

用钥匙搞定:

string dateJason = Preferences.Get("list", "");
MyArrayData myArrayData = JsonConvert.DeserializeObject<MyArrayData>(dateJason);
var intArray = myArrayData.IntArray;
var stringArray = myArrayData.StringArray;

【讨论】:

  • 谢谢@Leo Zhu - MSFT,详细的答案,我会检查并告诉你!
  • 感谢您宝贵的时间,但此保存部分不起作用,上传屏幕截图可能会有所帮助,谢谢!
  • @EmDe 您好,您需要在项目中使用using Xamarin.Essentials;,然后才能使用Preferences。而且你最好分享你问题中的屏幕截图,这样其他人会很方便地看到。
  • 嗨@JuniorJiang-MSFT,感谢您纠正我的屏幕截图,在使用导致错误的 Xamarin Preferences 后从回答端删除并添加到问题端!
  • @JuniorJiang-MSFT,它正在工作,我的问题在那里,如何像在 VB 中选择案例一样调用/选择它们,如果需要单独提问以获得答案,请建议我,谢谢为我完成这项工作付出了很多! (以及如何给你们投票)
猜你喜欢
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
相关资源
最近更新 更多