我正在開發的 Xamarin.Forms 應用程式存在問題。我需要讀取.json檔案的內容,以顯示在CarouselView. 我正在使用Newtonsoft.Json庫來決議檔案。
但是,它不斷地給我一個錯誤,它找不到檔案。c#代碼如下:
using System.Net.Http;
using System.Collections.ObjectModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using PalazzoVecchioDemo.Models;
using Newtonsoft.Json;
using System.IO;
namespace PalazzoVecchioDemo.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class VisitChoice : ContentPage
{
private const string _json = "rooms.json";
public ObservableCollection<Room> Rooms { get; set; } = new ObservableCollection<Room>();
public VisitChoice()
{
InitializeComponent();
//necessary to do data binding to elements present in this page
//(in particular to the observable collection of rooms)
BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
string path = Directory.GetCurrentDirectory();
string previousFolder = Path.Combine(path, @"..\");
string jsonPath = Path.Combine(path, _json);
var rooms = JsonConvert.DeserializeObject<Room[]>(File.ReadAllText(_json));
Rooms.Clear();
foreach(var room in rooms)
{
Rooms.Add(room);
}
}
}
}
.xaml代碼如下(我只放了其中的重要部分:
!-- Carousel view with all the contents -->
<CarouselView Grid.Row="1"
ItemsSource="{Binding Rooms}"
Loop="False">
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame HasShadow="True"
BorderColor="DarkGray"
CornerRadius="5"
Margin="20"
HeightRequest="300"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand">
<StackLayout>
<Label Text="{Binding Name}"
FontAttributes="Bold"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="Center"
AutomationProperties.IsInAccessibleTree="True"/>
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
專案結構如下;我正在寫VisitChoice頁面(在Views檔案夾內),我想讀取的檔案是rooms.json.

任何人都可以請幫助我嗎?
uj5u.com熱心網友回復:
我建議以下幾點:
在 Visual Studio 中,可以在輸出檔案夾中復制檔案。單擊 JSON 檔案時,請檢查屬性視窗。也許它不會復制到運行您的 EXE 或代碼的 bin 檔案夾中。
在開始使用 DeserializeObject 之前,如果檔案確實存在,我會檢查路徑。檔案。存在()方法。
當您訪問該檔案時,請檢查您是否在正確的檔案夾中。也許您的檔案在另一個子檔案夾中,而您是上面的一個檔案夾。
對于訪問,使用總路徑來避免問題。
uj5u.com熱心網友回復:
最好避免以您正在做的方式嵌入檔案。對于 JSON,我建議您在應用程式運行時創建檔案,即使它只會創建一次。
為此,您可以:
例如,使用
App.xaml.cs來處理您的 JSON(它將是您的專案運行的第一個類)。您可以將 JSON(如果它是靜態的,看到您正在嘗試嵌入它)存盤在resx 檔案中以訪問字串。創建和更新 JSON 檔案:
public void SaveJSON(string jsonText)
{
File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "myJson.json"), jsonText);
}
- 獲取您的 JSON 檔案以使用它:
public string GetJSON()
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "myJson.json");
string json = (File.Exists(path)) ? File.ReadAllText(path) : "";
return json;
}
如果你不想創建resx file,你可以在你的 App.xaml.cs 中傳遞一次SaveJSON("")來創建你的 JSON 檔案。
uj5u.com熱心網友回復:
當您在 Xamarin.Forms 專案中添加 json 檔案時Embedded resource,您可以使用下面的代碼來讀取和反序列化。
1.獲取json資料的匹配類。
復制您的 json 字串。單擊編輯 > 選擇性粘貼 > 將 JSON 粘貼為類。在那之后,你可以得到你想要的課程。
2.使用下面的代碼讀取Embedded resourcejson檔案并反序列化。
string _json = "rooms.json";
Room ObjList = new Room();
var assembly = typeof(Page20).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{_json}");
using (var reader = new StreamReader(stream))
{
var jsonString = reader.ReadToEnd();
ObjList = JsonConvert.DeserializeObject<Room>(jsonString);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/432469.html
