我有一個程式,用戶必須根據串列視圖項(章節)在協議中單擊的章節從 json 獲取文本
我該如何解決?
用戶在此處選擇協議中的所選章節
//for chosen step inside protocol
public void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
//change title based on clicked step
var tappedStep = (Content)MyListView.SelectedItem;
var nextTitle = Title " - " tappedStep.ChapterTitle;
Navigation.PushAsync(new StepView(_protocol, Title, tappedStep.ChapterTitle));
//Deselect item
((ListView)sender).SelectedItem = null;
}
接下來出錯的是標簽文本總是第一章的第一個文本這是因為我在下一個類中使用 FirstOrDefaultStepView.cs這里的邏輯必須是這樣的,它需要屬于相應章節的文本!而不是協議中的第一個。
public partial class StepView : ContentPage
{
//get step
private Protocol _protocol;
//go to selected step
public StepView(Protocol protocol, string title, string chapterTitle)
{
_protocol = protocol;
InitializeComponent();
Title = title " - " chapterTitle;
// get label text
lblText.Text = protocol.Contents.FirstOrDefault(x => x.ChapterTitle == chapterTitle).Text;
}
private int index;
public void BtnBack_Clicked(object sender, EventArgs e)
{
index--;
var firstId = _protocol.Contents.FirstOrDefault(x => x.Contentid != null).Contentid;
BtnNext.IsEnabled = true;
if (index == firstId - 1)
{
BtnBack.IsEnabled = false;
}
var content = _protocol.Contents.ElementAtOrDefault(index);
lblText.Text = content?.Text;
//get current navTitle on button click
getNewNavTitle(content);
}
public void BtnNext_Clicked(object sender, EventArgs e)
{
index ;
BtnBack.IsEnabled = true;
if (index == _protocol.Contents.Count - 1)
{
BtnNext.IsEnabled = false;
}
var content = _protocol.Contents.ElementAtOrDefault(index);
lblText.Text = content?.Text;
//get current navTitle on button click
getNewNavTitle(content);
}
//get new protocol chapter based on btnBack and btnNext
private void getNewNavTitle(Content content)
{
Title = null;
string nextTitile = _protocol.Name " - " content.NavTitle;
Title = nextTitile;
}
//go back to home
public void btnHome_Clicked(object sender, EventArgs e)
{
//go to mainpage
Navigation.PushAsync(new MainPage());
}
這是我使用的json
{
"protocols": [
{
"id": "1",
"name": "Pols meten",
"contents": [
{
"contentid": "1",
"chapterTitle": "Voorzorg",
"text": "voor blabla"
},
{
"contentid": "2",
"text": "voor blabla2"
},
{
"contentid": "3",
"text": "voor blablabla3"
},
{
"contentid": "4",
"chapterTitle": "Handeling",
}
]
}
這是我的領域類
public partial class RootObject
{
[JsonProperty("protocols")]
public List<Protocol> Protocols { get; set; }
}
public partial class Protocol
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("contents")]
public List<Content> Contents { get; set; }
}
public partial class Content
{
[JsonProperty("contentid")]
public long Contentid { get; set; }
[JsonProperty("chapterTitle", NullValueHandling = NullValueHandling.Ignore)]
public string ChapterTitle { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
}
如何使標簽文本而不是 FirstOrDefault 成為屬于所選章節開頭的文本?
uj5u.com熱心網友回復:
您正在使用此版本的FirstOrDefault,其內容如下:
如果找不到這樣的元素,則回傳滿足條件或默認值的序列的第一個元素。
在這種情況下,您指定的條件(或“謂詞”)是這樣的:
x => x.Text != null
這只是說“true如果Text屬性不是null,則回傳” -true正如您所提到的,它將始終回傳“第一個專案”(基于您證明的資料)。
相反,您可能希望謂詞true僅針對具有適當ChapterTitle.
從改變開始Handle_ItemTapped:
Navigation.PushAsync(new StepView(_protocol, nextTitle));
為此(這是因為我們需要新謂詞tappedStep.ChapterTitle的StepView建構式中的值):
Navigation.PushAsync(new StepView(_protocol, Title, tappedStep.ChapterTitle));
然后將建構式更新為StepView:
public StepView(Protocol protocol, string title, string chapterTitle)
{
_protocol = protocol;
InitializeComponent();
Title = title " - " chapterTitle;
// get label text
lblText.Text = protocol.Contents.FirstOrDefault(x => x.ChapterTitle == chapterTitle).Text;
}
現在,謂詞FirstOrDefault是:
x => x.ChapterTitle == chapterTitle
這意味著“true如果ChapterTitle屬性等于,則回傳chapterTitle”。
另請注意,設定該Title屬性與您的原始代碼略有不同,您可能需要對其進行調整以獲得您正在尋找的確切功能。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360054.html
