Xamarin MVVM将数据传递到其他视图

Xamarin MVVM将数据传递到其他视图,xamarin,mvvm,xamarin.forms,Xamarin,Mvvm,Xamarin.forms,我想将数据传递到另一个查看页面。到目前为止,我可以获得我需要传递的数据。我的问题是如何在MVVM中传递数据。我使用了Application.Current.MainPage.Navigation.PushAsync(newdatabasesyncpage(),true);在DatabaseSyncPage()中添加contactId时,会发生错误。“错误是'DatabaseSyncPage'不包含接受1个参数的构造函数” 我的代码: LoginPageViewModel.cs using New

我想将数据传递到另一个查看页面。到目前为止,我可以获得我需要传递的数据。我的问题是如何在MVVM中传递数据。我使用了Application.Current.MainPage.Navigation.PushAsync(newdatabasesyncpage(),true);在DatabaseSyncPage()中添加contactId时,会发生错误。“错误是'DatabaseSyncPage'不包含接受1个参数的构造函数”

我的代码:

LoginPageViewModel.cs

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Input;
using TBSMobileApplication.Data;
using TBSMobileApplication.View;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace TBSMobileApplication.ViewModel
{
    public class LoginPageViewModel : INotifyPropertyChanged
    {
        void OnProperyChanged(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        public string username;
        public string password;

        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                OnProperyChanged(nameof(Username));
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                OnProperyChanged(nameof(Password));
            }
        }

        public class LoggedInUser
        {
            public string ContactID { get; set; }
        }

        public ICommand LoginCommand { get; set; }

        public LoginPageViewModel()
        {
            LoginCommand = new Command(OnLogin);
        }

        public void OnLogin()
        {
            if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
            {
                MessagingCenter.Send(this, "Login Alert", Username);
            }
            else
            {
                var current = Connectivity.NetworkAccess;

                if (current == NetworkAccess.Internet)
                {
                    var link = "http://192.168.1.25:7777/TBS/test.php?User=" + Username + "&Password=" + Password;
                    var request = HttpWebRequest.Create(string.Format(@link));
                    request.ContentType = "application/json";
                    request.Method = "GET";

                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
                        }
                        else
                        {
                            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                            {
                                var content = reader.ReadToEnd();

                                if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content))
                            {
                                MessagingCenter.Send(this, "Http", Username);
                            }
                            else
                            {
                                var result = JsonConvert.DeserializeObject<List<LoggedInUser>>(content);
                                var contactId = result[0].ContactID;
                                Application.Current.MainPage.Navigation.PushAsync(new DatabaseSyncPage { myId = contactId }, true);
                            }
                        }
                    }
                }
                else
                {
                    MessagingCenter.Send(this, "Not Connected", Username);
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

我假设
contactID
int

数据库SyncPage
中创建其他构造函数:

public DatabaseSyncPage (int contactID)
{
    // TODO: Do something with your id
}
但这会将数据传递给页面,而不是页面模型。
您是否使用任何类型的框架?如果您想发送int,可能值得研究一下。

首先在
数据库同步页面中声明它
如下

public partial class DatabaseSyncPage : ContentPage
{
    public DatabaseSyncPage( int Id)
    {
    }
}
&当您在代码块中推送页面时,请执行以下操作

if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content))
{
    MessagingCenter.Send(this, "Http", Username);
}
else
{
    var result = JsonConvert.DeserializeObject<List<LoggedInUser>>(content);
    var contactId = result[0].ContactID;
    Application.Current.MainPage.Navigation.PushAsync(new DatabaseSyncPage(contactId), true);
}
if(content.Equals(“[]”)字符串.IsNullOrWhiteSpace(content)|字符串.IsNullOrEmpty(content))
{
发送(这是“Http”,用户名);
}
其他的
{
var result=JsonConvert.DeserializeObject(内容);
var contactId=结果[0]。contactId;
Application.Current.MainPage.Navigation.PushAsync(新数据库同步页面(contactId),true);
}

您可以使用xamrin.plugins.settings nuget软件包。

是,ContactID是一个int。如何将ContactID从我的viewmodel获取到databasesync页面?通过将此附加构造函数添加到您的
DatabaseSyncPage
:)我相信这个问题的答案是广泛可用的,您应该阅读一些文档或遵循Xamarin的教程,下面是如何检查数据是否已传递的问题?我添加了DisplayAlert(“Message”,Convert.ToString(myId),“ok”);但是没有显示消息显示我已经编辑了我的代码我在DatabaseSyncPage的InitializeComponent下面添加了警报我告诉你更新LoginPageViewModel.cs类代码删除Login.xaml&Login.xaml.cs页面一切正常。将断点置于else条件下,此时您正在数据库SyncPage中推送页面和1断点,并检查将要执行的值和将要执行的值。应用程序设置应用于持久化值,如服务器url和首次启动、上次检查的日期。不用于在视图之间传递数据@Axemasta你能帮我的朋友吗
if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content))
{
    MessagingCenter.Send(this, "Http", Username);
}
else
{
    var result = JsonConvert.DeserializeObject<List<LoggedInUser>>(content);
    var contactId = result[0].ContactID;
    Application.Current.MainPage.Navigation.PushAsync(new DatabaseSyncPage(contactId), true);
}