【问题标题】:Xamarin MVVM passing data to other viewXamarin MVVM 将数据传递到其他视图
【发布时间】:2019-01-03 03:25:11
【问题描述】:

我想将数据传递到另一个视图页面。到目前为止,我可以获得我需要传递的数据。我的问题是如何在 MVVM 中传递数据。我使用 Application.Current.MainPage.Navigation.PushAsync(new DatabaseSyncPage(), 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;
    }
}

DatabaseSyncPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TBSMobileApplication.View
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DatabaseSyncPage : ContentPage
    {
        public int myId { get; set; }

         public DatabaseSyncPage ()
         {
             InitializeComponent ();
             DisplayAlert("Message", Convert.ToString(myId), "ok");
         }
    }
}

【问题讨论】:

    标签: xamarin mvvm xamarin.forms


    【解决方案1】:

    我假设contactIDint

    在您的 DatabaseSyncPage 中创建一个额外的构造函数:

    public DatabaseSyncPage (int contactID)
    {
        // TODO: Do something with your id
    }
    

    但这会将数据传递给页面,而不是页面模型。 您是否使用任何类型的框架?这可能值得研究。

    【讨论】:

    • 是的,ContactID 是一个整数。如何从我的视图模型中获取联系人 ID 到数据库同步页面?
    • 通过将这个额外的构造函数添加到你的DatabaseSyncPage :)
    • 我相信这个问题的答案是广泛可用的,而不是在这里提问,您应该阅读一些文档或遵循 Xamarin 的教程,这里是 link
    【解决方案2】:

    如果你想发送 int.首先在您的DatabaseSyncPage 中声明 如下所示

    public partial class DatabaseSyncPage : ContentPage
    {
        public DatabaseSyncPage( int Id)
        {
        }
    }
    

    & 当您在代码else 块中推送页面时,请这样做

    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);
    }
    

    【讨论】:

    • 如何查看数据是否通过?
    • 我添加了 DisplayAlert("Message", Convert.ToString(myId), "ok");但没有显示消息显示
    • 我已经编辑了我的代码,我在初始化组件下方的 DatabaseSyncPage 上添加了警报
    • 我告诉你更新 LoginPageViewModel.cs 类代码。 & 删除 Login.xaml & Login.xaml.cs 页面
    • 一切看起来都很好。将断点置于您正在推送页面的 else 条件中,并在 DatabaseSyncPage 中设置 1 个断点并检查即将发生的值和即将发生的值。
    【解决方案3】:

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

    【讨论】:

    • 应用程序设置应用于持久值,例如服务器 url 和首次启动、最后检查日期。不适用于在视图之间传递数据!
    • @Axemasta 你能帮我的朋友stackoverflow.com/questions/51549737/…
    猜你喜欢
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    相关资源
    最近更新 更多