我正在開發的應用程序正在使用藍牙設備。 每個這些設備都有一個名稱和幾個服務。我試圖以編程方式向他們展示他們的服務。綁定到一個類中的列表
要做到這一點,我創建了一個包含服務列表的類。當嘗試綁定時,服務列表似乎完全是空的。
XAML:
public class BluetoothDevices
{
public string Name{ get; set;}
//public List<Guid> Services { get; set; }
public List<string> Services { get; set; }
}
XAML:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Width="150" />
<ListView ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Services}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C#:
adapter.ScanForBroadcasts(async (IBlePeripheral peripheral) =>
{
//List<Guid> services = new List<Guid>();
List<string> services = new List<string>();
string devicename = peripheral.Advertisement.DeviceName;
var _services = peripheral.Advertisement.Services;
var servicedata = peripheral.Advertisement.ServiceData;
//services = (_services.ToList());
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
services.Add("i am a string ");
BluetoothDevices.Add(new BluetoothDevices { Name = devicename, Services = services });
});
//BluetoothDevices = new ObservableCollection<BluetoothDevices>(BluetoothDevices.Distinct());
}, TimeSpan.FromSeconds(30));
使用ObservableCollection'的''而不是List'。當你向它添加項目時,List不會通知任何人,所以你不會看到UI創建後添加的任何東西。 'ObservableCollection'確實。如果這不能解決它,你可能會搞錯了。 BluetoothDevices是你的視角模型還是別的?它應該實現'INotifyPropertyChanged'。還要注意保加的正確建議。 –
我想這不會是問題,因爲我創建列表,然後將項目添加到observablecollection。 編輯: 即使嵌套observablecollections它不工作。 –
什麼是observablecollection? 「嵌套的observablecollections」是什麼意思?你是說'BluetoothDevices'是一個ObservableCollection嗎?順便說一下,假設至少有一個關於問題原因的假設是錯誤的 - 否則你不會在這裏尋求幫助。 –