我Xamarin.Forms正在json使用CarouselView. 但是,我做不到:我正在正確決議檔案,但它沒有顯示在 UI 中。
xaml代碼(僅部分CarouselView)如下:
<!-- Carousel view with all the contents -->
<CarouselView Grid.Row="1"
Loop="False"
x:Name="visitChoicheCarousel"
ItemsSource="{Binding Rooms}">
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame HasShadow="True"
BorderColor="DarkGray"
CornerRadius="5"
Margin="20"
HeightRequest="300"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand">
<StackLayout>
<Label Text="{Binding Contents.Name}"
FontAttributes="Bold"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="Center"
AutomationProperties.IsInAccessibleTree="True"/>
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
決議檔案的c#腳本如下:
using System.Net.Http;
using System.Collections.ObjectModel;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using PalazzoVecchioDemo.Models;
using Newtonsoft.Json;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using System.Reflection;
namespace PalazzoVecchioDemo.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class VisitChoice : ContentPage
{
private const string _json = "rooms.json";
public Room Rooms { get; set; } = new Room();
public VisitChoice()
{
InitializeComponent();
BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
var assembly = typeof(VisitChoice).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{_json}");
using (var reader = new StreamReader(stream))
{
var jsonString = reader.ReadToEnd();
Rooms = JsonConvert.DeserializeObject<Room>(jsonString);
}
}
}
}
包含反序列化檔案的類如下:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace PalazzoVecchioDemo.Models
{
public class Rootobject
{
public Room[] Rooms { get; set; }
}
public class Room
{
[JsonProperty("ID")]
public string ID { get; set; }
public Content[] Contents { get; set; }
}
public class Content
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("ImageURI")]
public string ImageURI { get; set; }
}
}
有人可以幫我嗎?
uj5u.com熱心網友回復:
首先,你ItemsSource需要成為一個IEnumerable
這是錯誤的,Room是單個Room物件
ItemsSource="{Binding Rooms}"
你可能想這樣做,因為Contents是一個陣列
ItemsSource="{Binding Rooms.Contents}"
然后也改變這個
Text="{Binding Contents.Name}"
對此
Text="{Binding Name}"
最后,在加載資料BindingContext 后分配,或使用INotifyPropertyChanged表示Rooms已更新
uj5u.com熱心網友回復:
public partial class VisitChoice : ContentPage, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private const string FileName = "Room.json";
private List<Room> _rooms;
public List<Room> Rooms
{
get => _rooms;
set
{
_rooms = value;
NotifyPropertyChanged();
}
}
public VisitChoice()
{
InitializeComponent();
BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(VisitChoice)).Assembly;
var stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{FileName}");
using (var reader = new StreamReader(stream))
{
var jsonString = reader.ReadToEnd();
Rooms = JsonConvert.DeserializeObject<List<Room>>(jsonString);
}
}
}
uj5u.com熱心網友回復:
你可以參考下面的代碼。
xml:
<CarouselView Grid.Row="1"
Loop="False"
x:Name="visitChoicheCarousel"
ItemsSource="{Binding Rooms}">
<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>
代碼:
public partial class Page20 : ContentPage
{
public ObservableCollection<Contact> Rooms { get; set; }
public Page20()
{
InitializeComponent();
string _json = "rooms.json";
Room ObjContactList = 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();
//Converting JSON Array Objects into generic list
ObjContactList = JsonConvert.DeserializeObject<Room>(jsonString);
}
Rooms = new ObservableCollection<Contact>();
foreach (var item in ObjContactList.contacts.ToList())
{
Rooms.Add(item);
}
this.BindingContext = this;
}
}
public class Room
{
public Contact[] contacts { get; set; }
}
public class Contact
{
public string name { get; set; }
public string email { get; set; }
public string phoneNumber { get; set; }
}
json資料:
{
"contacts": [
{
"name": "JOE",
"email": "name@handle",
"phoneNumber": "123-456-7890"
},
{
"name": "JYM",
"email": "name@handle",
"phoneNumber": "123-456-7890"
},
{
"name": "JYM",
"email": "name@handle",
"phoneNumber": "123-456-7890"
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435957.html
標籤:C# json xamarin xamarin.forms
