我有一個名為 stepview 的類,它基于 btnBack(1 個數字)或 btnNext(1 個數字)根據 json 中的 contentid 更改文本。這有效,但不會出現,因為那時我看不到影像,因為那時索引為 0。
currentindex 應該等于我的 json 檔案的 contentid,現在只能與我的標簽文本一起正常作業,顯示已經基于 id 的正確文本。
唯一適用于照片的是,如果您單擊下一步或后退,您將看到照片,但對于用戶首先到達的頁面,情況并非如此。我該如何解決這個問題?
這是我的 btnNext 和 btnBack,用于根據來自 json 的 contentid 的索引顯示文本 影像
public void BtnBack_Clicked(object sender, EventArgs e)
{
BtnNext.IsEnabled = true;
int currentIndex = getCurrentIndex();
//if its the first item disable back button
if (currentIndex.Equals(1))
{
BtnBack.IsEnabled = false;
}
var content = _protocol.Contents[currentIndex - 1];
_contentId = content.Contentid;
lblText.Text = content?.Text;
string protocolName = _protocol.Name;
//replace space with underscore to get correct picture name
protocolName = protocolName.Replace(" ", "_");
myImage.Source = ($"{protocolName}{content?.Contentid}.jpg");
//get current navTitle on button click
setNewNavTitle();
}
public void BtnNext_Clicked(object sender, EventArgs e)
{
BtnBack.IsEnabled = true;
int currentIndex = getCurrentIndex();
var content = _protocol.Contents[currentIndex 1];
//do something after second last
if (currentIndex == _protocol.Contents.Count - 2)
{
BtnNext.IsEnabled = false;
}
_contentId = content.Contentid;
lblText.Text = content?.Text;
string protocolName = _protocol.Name;
//replace space with underscore to get correct picture name
protocolName = protocolName.Replace(" ", "_");
myImage.Source = ($"{protocolName}{content?.Contentid}.jpg");
//get current navTitle on button click
setNewNavTitle();
}
當用戶第一次進入頁面時,它不顯示關聯的照片,只顯示他放在標簽中的文字
這是用于在 2 次按鈕點擊上顯示照片
myImage.Source = ($"{protocolName}{content?.Contentid}.jpg");
contentid 在開始時似乎是 0
這是我的 Json 檔案
"protocols": [
{
"id": "1",
"name": "Pols tellen",
"contents": [
{
"chapterTitle": "Voorzorg",
"contentid": "1",
"text": "test1"
},
{
"contentid": "2",
"text": "test2"
},
{
"chapterTitle": "Handeling",
"contentid": "3",
"text": "test3"
},
{
"contentid": "4",
"test1": "test4"
},
{
"chapterTitle": "Nazorg",
"contentid": "10",
"text": "test5"
},
{
"contentid": "11",
"text": "test6"
}
]
},
}
這是我的課程,用于根據他的 contentid 接收文本和影像(現在僅適用于標簽中的文本)
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StepView : ContentPage
{
//get index
private long _contentId;
//get step
private Protocol _protocol;
//go to selected step
public StepView(Protocol protocol, string title, string chapterTitle, long contentId)
{
_protocol = protocol;
InitializeComponent();
Title = title " - " chapterTitle;
// get label text
lblText.Text = protocol.Contents.FirstOrDefault(x => x.ChapterTitle == chapterTitle).Text;
_contentId = contentId;
}
public void BtnBack_Clicked(object sender, EventArgs e)
{
BtnNext.IsEnabled = true;
int currentIndex = getCurrentIndex();
//if its the first item disable back button
if (currentIndex.Equals(1))
{
BtnBack.IsEnabled = false;
}
var content = _protocol.Contents[currentIndex - 1];
_contentId = content.Contentid;
lblText.Text = content?.Text;
string protocolName = _protocol.Name;
//replace space with underscore to get correct picture name
protocolName = protocolName.Replace(" ", "_");
myImage.Source = ($"{protocolName}{content?.Contentid}.jpg");
//get current navTitle on button click
setNewNavTitle();
}
//go back to home
public async void btnHome_Clicked(object sender, EventArgs e)
{
//go to mainpage
await Navigation.PushAsync(new MainPage());
}
public void BtnNext_Clicked(object sender, EventArgs e)
{
BtnBack.IsEnabled = true;
int currentIndex = getCurrentIndex();
var content = _protocol.Contents[currentIndex 1];
//do something after second last
if (currentIndex == _protocol.Contents.Count - 2)
{
BtnNext.IsEnabled = false;
}
_contentId = content.Contentid;
lblText.Text = content?.Text;
string protocolName = _protocol.Name;
//replace space with underscore to get correct picture name
protocolName = protocolName.Replace(" ", "_");
myImage.Source = ($"{protocolName}{content?.Contentid}.jpg");
//get current navTitle on button click
setNewNavTitle();
}
private string getChapterTitle()
{
var currentIndex = getCurrentIndex();
string chapterTitle = _protocol.Contents[currentIndex].ChapterTitle;
//get the previous or next chapter based on where you clicked on
while (currentIndex > 0 && string.IsNullOrWhiteSpace(chapterTitle))
{
currentIndex -= 1;
chapterTitle = _protocol.Contents[currentIndex].ChapterTitle;
}
return chapterTitle;
}
private int getCurrentIndex()
{
var currentContent = _protocol.Contents.FirstOrDefault(x => x.Contentid == _contentId);
var currentIndex = _protocol.Contents.IndexOf(currentContent);
return currentIndex;
}
//get new protocol chapter based on btnBack and btnNext
private void setNewNavTitle()
{
string nextTitile = _protocol.Name " - " getChapterTitle();
Title = nextTitile;
}
如何為我的照片的標簽文本獲得相同的效果?提前致謝
uj5u.com熱心網友回復:
只需將此添加到頁面建構式的末尾
myImage.Source = ($"{protocolName}{content?.Contentid}.jpg");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/384137.html
