I wrote this piece of code that navigates the user to the profile that he/she selects:
public void ActivitySelected(System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
return;
var activity = e.SelectedItem as Activity;
activityFeed.SelectedItem = null;
Navigation.PushAsync(new UserProfilePage(activity.UserId));
}
"activityFeed" is the link to the frontend XAML file.
Activity is a class that contains:
using System;
namespace Navigation_Exercise1.Models
{
public class Activity
{
public int UserId { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
}
}
UserProfilePage contains:
using System;
using System.Collections.Generic;
using Navigation_Exercise1.Models;
using Xamarin.Forms;
namespace Navigation_Exercise1
{
public partial class UserProfilePage : ContentPage
{
UserService _service = new UserService();
public UserProfilePage(int userId)
{
BindingContext = _service.GetUser(userId);
InitializeComponent();
}
}
}
The "UserService" class is just a back-end containing the user data. The "GetUser(userid)" method is linked to a User class to retrieve the same value of Id.
How am I able to do exactly the same but then with a Button instead of a Selection of a list property? This is what I got so far:
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
var response = await DisplayActionSheet("Options", "Cancel", "Delete activity", "Message this user", "Go to profile");
if (response == "Go to profile")
{
await Navigation.PushModalAsync(new UserProfilePage());
}
}
As can be observed, the UserProfilePage contains a constructor that requires a UserId, but how can I reference it as I did in the ActivitySelected() method?
Thanks in advance!
use the BindingContext
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
var btn = (Button)sender;
var act = (Activity)btn.BindingContext;
var id = act.UserId;
var response = await DisplayActionSheet("Options", "Cancel", "Delete activity", "Message this user", "Go to profile");
if (response == "Go to profile")
{
await Navigation.PushModalAsync(new UserProfilePage(id));
}
}