【发布时间】:2020-05-20 16:23:46
【问题描述】:
Xamarin.forms 如何从图像按钮获取 ID?
<ImageButton x:Name="quizButton" Source="quiz2.png" Clicked="OnQuizClicked" />
我试过这种方式,但他给我一个错误:System.NullReferenceException: 'Object reference not set to an instance of an object.'
private async void OnQuizClicked(object sender, EventArgs e)
{
var button = sender as Button;
var route = button?.BindingContext as Routes;
var token = Application.Current.Properties["MyToken"].ToString();
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await client.GetStringAsync("URL");
List<Question> punkty = JsonConvert.DeserializeObject<List<Question>>(response);
List<Question> list = new List<Question>();
foreach (Question tmp in punkty)
{
if (tmp.RouteId == route.Id)
{
list.Add(tmp);
}
}
await Navigation.PushAsync(new UserQuestionPage());
}
【问题讨论】:
-
您将
sender转换为Button,而不是ImageButton -
能否请您标记正确的答案,以便我们可以帮助更多有相同问题的人:)。
-
好的,没问题:)
标签: xaml xamarin xamarin.forms