我目前正在爲gravesite的訪問者信息站開發一個應用程序,其中一個功能從csv文件中提取數據並使用所述數據來填充列表視圖。下面的代碼是獲取數據並將其返回到列表類型嚴重程度的代碼。從ApplicationData.Current.LocalFolder切換到Knownfolders.DocumentsLibrary
public class Grave
{
public string plots { get; set; }
public string DOBS { get; set; }
public string lastNames { get; set; }
public string firstNames { get; set; }
public string companys { get; set; }
public string regts { get; set; }
public string unitTypes { get; set; }
public string states { get; set; }
public string ranks { get; set; }
public string sections { get; set; }
public string image { get; set; }
public string text { get; set; }
public string notenums { get; set; }
}
public class GraveManager
{
public static List<Grave> GetGrave()
{ //points to desired folder
StorageFolder folder = ApplicationData.Current.LocalFolder;
string csvPath = folder.Path + @"\PGexcel.csv";
//loads .csv file from folder
Csv csv = new Csv();
//property that tells csv parser to not treat the first row as data
csv.HasColumnNames = true;
bool success1;
success1 = csv.LoadFile(csvPath);
//name of columns
string plot = "Plot", DOB = "Date_of_Death", lastName = "Last_Name", firstName = "First_Name", company = "Company", regt = "Regt", state = "State", unitType = "Unit_Type", Rank = "Rank", Section = "Section", Notables= "Notables";
//initialize string of arrays for each column
string[] OCplots = new string[csv.NumRows];
string[] OCDOBS = new string[csv.NumRows];
string[] OClastNames = new string[csv.NumRows];
string[] OCfirstNames = new string[csv.NumRows];
string[] OCcompanys = new string[csv.NumRows];
string[] OCregts = new string[csv.NumRows];
string[] OCunitTypes = new string[csv.NumRows];
string[] OCstates = new string[csv.NumRows];
string[] OCranks = new string[csv.NumRows];
string[] OCsections = new string[csv.NumRows];
string[] OCimage = new string[csv.NumRows];
string[] OCtext = new string[csv.NumRows];
string[] OCnotenums = new string[csv.NumRows];
//populates the arrays with values from .csv file
for (int i = 0; i < csv.NumRows; i++)
{
OCplots[i] = csv.GetCellByName(i, plot);
OCDOBS[i] = csv.GetCellByName(i, DOB);
OClastNames[i] = csv.GetCellByName(i, lastName);
OCfirstNames[i] = csv.GetCellByName(i, firstName);
OCcompanys[i] = csv.GetCellByName(i, company);
OCregts[i] = csv.GetCellByName(i, regt);
OCunitTypes[i] = csv.GetCellByName(i, unitType);
OCranks[i] = csv.GetCellByName(i, Rank);
OCsections[i] = csv.GetCellByName(i, Section);
OCstates[i] = csv.GetCellByName(i, state);
OCnotenums[i] = csv.GetCellByName(i, Notables);
}
//concantenate arrays with .jpg .txt to call for corresponding files
for (int i = 0; i < csv.NumRows; i++)
{
//OCimage[i] = "C:/Users/POGR_ADMIN/AppData/Local/Packages/6b3614f6-6a5f-48fc-9687-80291e70b64d_phwtyg9y34v1t/LocalState/" + OCplots[i] + ".jpg";
OCimage[i] = folder.Path + @"\" + OCplots[i] + ".jpg";
OCtext[i] = folder.Path + @"\"+ OCplots[i] + ".txt";
}
var graves = new List<Grave>();
//attempt to populate List using the for loop
for (int i = 0; i < csv.NumRows; i++)
{
graves.Add(new Grave { plots= OCplots[i], DOBS = OCDOBS[i], lastNames = OClastNames[i], firstNames = OCfirstNames[i], companys = OCcompanys[i], regts = OCregts[i], states = OCstates[i], unitTypes = OCunitTypes[i],ranks = OCranks[i], sections = OCsections[i], image = OCimage[i], text = OCtext[i], notenums = OCnotenums[i] });
}
return graves;
}
}
我那麼另一頁上初始化列表類型的墳墓,並通過調用方法時調用構造函數填充它。這是顯示在這裏:
public List<Grave> Graves;
public FindaGrave()
{
this.InitializeComponent();
GraveInitializer();
}
public void GraveInitializer()
{
Graves = GraveManager.GetGrave();
}
此代碼工作完美和罰款,並完全填充listview我如何設想。但是,我注意到,csv文件中有許多錯誤,他們發現並計劃由於人爲錯誤而找到這些錯誤。據說,他們希望能夠對csv文件進行更改,並且在重新加載應用程序(沒有任何幫助)後顯示更改。由於當地國家的文件夾被包裝我認爲應該做的唯一方式,以便將改變 StorageFolder folder = ApplicationData.Current.LocalFolder;
爲StorageFolder folder = KnownFolders.DocumentsLibrary;
我覺得,如果我確信該文件是文檔庫中,增加了功能,在應用程序包的清單像<uap:Capability Name="documentsLibrary" />
這樣,並添加相應的文件類型關聯,它應該工作相同。但是,當我運行它時,列表不再顯示出來,並且在我稍微調試一下後,folder.Path
和string csvPath
的值就不再等於「\ PGexcel.csv」。我完全失去了,因爲我認爲會是一個快速解決方案最終成爲一件非常困難的事情,而且我是UWP的新手,所以沒有任何幫助。我查找了幾天,無法找到解決方案,對此問題的任何幫助將不勝感激。你會成爲一個拯救生命的人!
之前我們要潛入你的問題。您是否知道文件夾的功能受到限制,如果想要提交給商店,最好不要使用它?來源:https://msdn.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations –
我明白,最好不要使用它,但這個應用程序將被側載,並不會有去商店,所以它真的沒關係。@DaveSmits –