在我的 json 檔案的步驟中,我想構建一個移動到下一個或上一個文本的 linq,具體取決于您是點擊后退按鈕(上一個 id)還是下一個按鈕(下一個 contentid)。如果發生這種情況,則必須修改標簽的文本。我不確定如何在 Linq 中實作這一點。
現在的問題是當我第一次單擊按鈕時文本為空。然后他顯示了相同的文本!我究竟做錯了什么?
按鈕的點擊處理程式
private Content content;
public void BtnNext_Clicked(object sender, EventArgs e)
{
var index = content == null ? 0 : _step.Contents.IndexOf(content) 1;
content = _step.Contents.ElementAtOrDefault(index);
lblText.Text = content?.Text;
}
public void BtnBack_Clicked(object sender, EventArgs e)
{
var index = content == null ? _step.Contents.Count - 1 : _step.Contents.IndexOf(content) - 1;
content = _step.Contents.ElementAtOrDefault(index);
lblText.Text = content?.Text;
}
領域類
public class RootObject
{
[JsonProperty("protocols")]
public List<Protocol> Protocols { get; set; }
}
public class Protocol
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("steps")]
public List<Step> Steps { get; set; }
[JsonProperty("versie")]
public string Versie { get; set; }
}
public class Step
{
[JsonProperty("chapterTitle")]
public string ChapterTitle { get; set; }
[JsonProperty("contents")]
public List<Content> Contents { get; set; }
}
public class Content
{
[JsonProperty("contentid")]
public int Contentid { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
}
json檔案
{
"protocols": [
{
"id": "1",
"name": "Pols meten",
"steps": [
{
"chapterTitle": "Voorzorg",
"contents": [
{
"contentid": "1",
"text": "voor blabla"
}
]
},
{
"contents": [
{
"contentid": "2",
"text": "voor blabla2"
}
]
},
{
"chapterTitle": "Handeling",
"contents": [
{
"contentid": "3",
"text": "handeling blabla"
}
]
},
{
"contens": [
{
"contentid": "4",
"text": "handeling blabla2"
}
]
},
{
"chapterTitle": "Nazorg",
"contents": [
{
"contentid": "5",
"text": "nazorg blabla"
}
]
},
{
"contents": [
{
"contentid": "6",
"text": "nazorg blabla2"
}
]
}
],
"versie": "1"
}
]
}
我想要的是根據您單擊的位置顯示下一個或上一個內容 ID 中的文本。
uj5u.com熱心網友回復:
每一步都有一個內容:
...
"steps": [
{
"chapterTitle": "Voorzorg",
"contents": [
{
"contentid": "1",
"text": "voor blabla"
}
]
},
...
首先單擊不顯示任何內容的下一個按鈕:
// Initialized on the first step's content (I think)
private Content content;
public void BtnNext_Clicked(object sender, EventArgs e)
{
int index;
if(content == null)
{
index = 0;
}
else // Go here, because content is the first step's content (that is not null)
{
index = _step.Contents.IndexOf(content)
// index <= 0
index = index 1;
// index <= 1
}
content = _step.Contents.ElementAtOrDefault(index);
// content <= _step.Contents.ElementAtOrDefault(1);
// content <= the second step's content
// But the step has ONE content, then ElementAtOrDefault return null
// content <= null
lblText.Text = content?.Text;
// Display nothing
}
第二次點擊下一步按鈕:
// After the first click, content is null
private Content content;
public void BtnNext_Clicked(object sender, EventArgs e)
{
int index;
if(content == null) // Go here, because content is null
{
index = 0;
}
else
{
index = _step.Contents.IndexOf(content) 1;
}
content = _step.Contents.ElementAtOrDefault(index);
// content <= _step.Contents.ElementAtOrDefault(0);
// content <= the first step's content
lblText.Text = content?.Text;
// Redisplay the first step's content
}
您可以保留索引(而不是內容)并檢查它是否在范圍內(而不是內容不為空):
private int index;
public void BtnNext_Clicked(object sender, EventArgs e)
{
index ;
// If the index go after the last element
if(index >= _step.Contents.Count)
// Then reset
index = 0;
var content = _step.Contents.ElementAtOrDefault(index);
lblText.Text = content?.Text;
}
public void BtnBack_Clicked(object sender, EventArgs e)
{
index--;
// If the index go before the first element
if(index < 0)
// Then go to the last
index = _step.Contents.Count - 1;
var content = _step.Contents.ElementAtOrDefault(index);
lblText.Text = content?.Text;
}
并在步驟中添加更多內容(否則在唯一步驟的內容上回圈):
"steps": [
{
"chapterTitle": "Voorzorg",
"contents": [
{
"contentid": "1",
"text": "voor blabla"
},
{
"contentid": "2",
"text": "vuur blabla"
},
{
"contentid": "3",
"text": "soor blybli"
}
]
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360055.html
