Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to pass ID from view page to viewmodel?

ShrikantPShrikantP Member ✭✭
edited November 2020 in Xamarin.Forms

I am trying to create a messenger application.
After login how to send the userid to viewmodel for beign chat with login person.

/* This code from list of people is available to chat */
public async void btnchat_clicked(object sender, EventArgs e)
{
string sellerid = custid.Text;
await Navigation.PushAsync(new ChatPageViewModel(sellerid.ToString()));

    }

/Viewmodel class page/
namespace MessengerApp.ViewModel
{
public class ChatPageViewModel : ContentPage
{

    public ObservableCollection<Message> MessageList { get; set; }
    private string tosenderid;

    public ChatPageViewModel()
    {
        //tosenderid = sellerid.ToString();

        load_chat_system();
    }

   public void load_chat_system()
    {
        string fromuserid = Application.Current.Properties["userid"].ToString();    /* This is login person ID*/
        MySqlConnection conn = new MySqlConnection(Constants.connString);
        conn.Open();
        string sqlcat = "select to_user_id, from_user_id, chat_message, timestamp, status from chat_message where from_user_id = '" + fromuserid.ToString() + "' and to_user_id = '" + tosenderid.ToString() + "'";
        MySqlCommand cmd = new MySqlCommand(sqlcat, conn);
        MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        MessageList = new ObservableCollection<Message>();
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                MessageList.Add(new Message { sellerid = dt.Rows[i]["to_user_id"].ToString(), userid = dt.Rows[i]["from_user_id"].ToString(), Text = dt.Rows[i]["chat_message"].ToString(), dateSent = dt.Rows[i]["timestamp"].ToString(), status = dt.Rows[i]["status"].ToString() });
            }
        }

    }


}

}

Posts

  • Xevious95Xevious95 Member ✭✭

    I don't understand.. where is this userid? Do you receive it from the login response? Is it saved in a sql lite database?
    Why your viewmodel extend ContentPage?
    Your view should extend contentpage.. and have the bindingContext to your viewmodel..

  • ShrikantPShrikantP Member ✭✭

    @Xevious95 said:
    I don't understand.. where is this userid? Do you receive it from the login response? Is it saved in a sql lite database?
    Why your viewmodel extend ContentPage?
    Your view should extend contentpage.. and have the bindingContext to your viewmodel..

    In above code userid is "sellerid" and i m receiving from login is "userid".
    i trying to create a messageing system but to show a previous communication i required a senderid i.e. sellerid and userid.
    i can call the userid but it is difficult to call the "sellerid" for chat page.

Sign In or Register to comment.