我從 firebase 實時資料庫中獲取了一個 id 鍵,然后我將它作為 label.text 系結到 listview
我想要做的是將 label.text 內部的這個 idkey 分配為字串變數,并在將來在 XML.CS 頁面中使用這個字串變數。
在嘗試將其分配為字串時,它無法識別其 x:標簽名稱
XML 代碼
<StackLayout>
<ListView x:Name="xListView" ItemsSource="{Binding xlist}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="5">
<StackLayout HorizontalOptions="StartAndExpand">
<Label x:Name="entryvalue" Text="{Binding Id}" FontSize="Medium"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
XML CS 代碼
protected override async void OnAppearing()
{
var xlist = await GetAlles();
xListView.ItemsSource = xlist;
//Tries to assing value to string variable
string getentryvalue = entryvalue.Text;
}
從 XML CS 檔案中的資料庫函式獲取全部
public async Task<List<xModel>> GetAlles()
{
return (await firebaseClient.Child(nameof(xModel)).OnceAsync<xModel>()).Select(item => new xModel
{
Id = item.Key
}).ToList();
}
和班級
public class xModel
{
public string Id { get; set; }
}
uj5u.com熱心網友回復:
就像評論說的那樣,你把這件事復雜化了很多。
您的xlist變數已經包含所有 id。
要獲得特定的 id,您需要做的就是類似
var id = xlist[0].Id;
要更深入地了解對您不起作用的答案,您不能通過 x:name 在 ListView 或 CollectionView 中訪問 XAML 元素。由于每個專案都使用相同的模板,當您說 時,代碼應該如何知道您想要哪個模板entryvalue?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/524949.html
標籤:C#xamarin
上一篇:是否可以將滿足呼叫的次數作為引數傳遞給單元測驗類方法?
下一篇:將串列轉換為Int串列
