如何在页面 C# Xamarin 出现时使 currentindex 成为正确的数字而不是 0
How to make currentindex the correct number instead of 0 on appearing of the page C# Xamarin
我有一个名为 stepview 的 class,它基于 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"
}
]
},
}
这是我的 class 用于根据他的 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;
}
如何使我的照片标签文本具有相同的效果?提前致谢
只需将其添加到页面构造函数的末尾
myImage.Source = ($"{protocolName}{content?.Contentid}.jpg");
我有一个名为 stepview 的 class,它基于 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"
}
]
},
}
这是我的 class 用于根据他的 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;
}
如何使我的照片标签文本具有相同的效果?提前致谢
只需将其添加到页面构造函数的末尾
myImage.Source = ($"{protocolName}{content?.Contentid}.jpg");