微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将数据从列表视图sqllite 数据库导出到 txt 文件?

如何解决如何将数据从列表视图sqllite 数据库导出到 txt 文件?

我有来自 sqlite 数据库的数据并将其显示在 Xamarin Forms 的列表视图中。现在我想将列表视图中的内容导出到特定位置的 txt 文件中。

我一直在互联网上搜索,但没有得到任何适当的解决方案。

这是我绑定的来源。

 <?xml version="1.0" encoding="utf-8" ?>
  <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:Xamarinsqlite"
         x:Class="Xamarinsqlite.MainPage">

<StackLayout>
    <StackLayout>
        <StackLayout HorizontalOptions="Center" VerticalOptions="Start">
            <Image x:Name="imgBanner" Source="banner.png" ></Image>
            <Image Margin="0,10" HeightRequest="100" Source="sqlite.png" ></Image>
            <Label Margin="0,10" Text="sqlite" FontAttributes="Bold" FontSize="Large" TextColor="Gray" HorizontalTextAlignment="Center" ></Label>
            <Entry x:Name="txtPersonId" Placeholder="PersonId Update and Delete"></Entry>
            <Entry x:Name="txtName" Placeholder="Enter Person Name"></Entry>
            <StackLayout  HorizontalOptions="CenterandExpand" Orientation="Horizontal">
                <Button x:Name="btnAdd" WidthRequest="200" Text="Add" Clicked="BtnAdd_Clicked" />
                <Button x:Name="btnRead" WidthRequest="200" Text="Read" Clicked="BtnRead_Clicked" />
            </StackLayout>
            <StackLayout HorizontalOptions="CenterandExpand" Orientation="Horizontal">
                <Button x:Name="btnUpdate" WidthRequest="200" Text="Update" Clicked="BtnUpdate_Clicked"/>
                <Button x:Name="btnDelete" WidthRequest="200" Text="Delete" Clicked="BtnDelete_Clicked" />
                <Button x:Name="btnExport" WidthRequest="200" Text="Export" Clicked="btnExport_Clicked" />
            </StackLayout>
            <ListView x:Name="lstPersons">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Name}" Detail="{Binding PersonID}"></TextCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </StackLayout>
    </StackLayout>
</StackLayout>

背后的代码

using System;
using Xamarin.Forms;

namespace Xamarinsqlite
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        protected async override void OnAppearing()
        {
            base.OnAppearing();
        
            //Get All Persons
            var personList = await App.sqliteDb.GetItemsAsync();
            if(personList!=null)
            {
                lstPersons.ItemsSource = personList;
            }
        }

        private async void BtnAdd_Clicked(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtName.Text))
            {
                Person person = new Person()
                {
                    Name = txtName.Text
                };

                //Add New Person
                await App.sqliteDb.SaveItemAsync(person);
                txtName.Text = string.Empty;
                await displayAlert("Success","Person added Successfully","OK");
                //Get All Persons
                var personList = await App.sqliteDb.GetItemsAsync();
                if (personList != null)
                {
                    lstPersons.ItemsSource = personList;
                }
            }
            else
            {
                await displayAlert("required","Please Enter name!","OK");
            }
        }

        private async void BtnRead_Clicked(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPersonId.Text))
            {
                //Get Person
                var person = await App.sqliteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
                if(person!=null)
                {
                    txtName.Text = person.Name;
                    await displayAlert("Success","Person Name: "+ person.Name,"OK");
                }
            }
            else
            {
                await displayAlert("required","Please Enter PersonID","OK");
            }
        }

        private async void BtnUpdate_Clicked(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPersonId.Text))
            {
                Person person = new Person()
                {
                    PersonID=Convert.ToInt32(txtPersonId.Text),Name = txtName.Text
                };

                //Update Person
                await App.sqliteDb.SaveItemAsync(person);

                txtPersonId.Text = string.Empty;
                txtName.Text = string.Empty;
                await displayAlert("Success","Person Updated Successfully","OK");
                //Get All Persons
                var personList = await App.sqliteDb.GetItemsAsync();
                if (personList != null)
                {
                    lstPersons.ItemsSource = personList;
                }

            }
            else
            {
                await displayAlert("required","OK");
            }
        }

        private async void BtnDelete_Clicked(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPersonId.Text))
            {
                //Get Person
                var person = await App.sqliteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
                if (person != null)
                {
                    //Delete Person
                    await App.sqliteDb.DeleteItemAsync(person);
                    txtPersonId.Text = string.Empty;
                    await displayAlert("Success","Person Deleted","OK");
                
                    //Get All Persons
                    var personList = await App.sqliteDb.GetItemsAsync();
                    if (personList != null)
                    {
                        lstPersons.ItemsSource = personList;
                    }
                }
            }
            else
            {
                await displayAlert("required","OK");
            }
        }

        private void btnExport_Clicked(object sender,EventArgs e)
        {
       //How to Export Data In Txt file
        }
    }
}

请帮助我 我是 Xamarin 的新手

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。