【发布时间】:2020-08-25 14:32:33
【问题描述】:
我是 Xamarin 的新手,无法将数据从一个 ViewModel 传递到另一个。 我尝试了很多东西,但没有任何效果。 那么到目前为止我做了什么
在我的 LoginViewModel 上,我有这段代码可以从数据库中获取信息
CRUD crud = new CRUD();
UserLogin = crud.Login(UserName, Password);
Application.Current.MainPage = new AppShell();
await Shell.Current.GoToAsync($"//{nameof(HomePage)}?UserLogin={UserLogin}");
在我的主页上,我尝试了很多东西。 这是我现在主页上的内容:ContentPage
{
public HomePage()
{
InitializeComponent();
}
public HomePage(List<User> UserLogin)
{
this.BindingContext = new HomeViewModel(UserLogin);
}
}
我尝试移动 InitializeCompognent(),删除它,删除基本构造函数,但我有空白页或错误。这是我加载页面的唯一方法。 在 HomeViewModel 我有这个代码
public class HomeViewModel : BaseViewModel
{
public List<User> UserLogin { get;}
public HomeViewModel() { }
public HomeViewModel(List<User> Users)
{
Title = "Acceuil";
UserLogin = Users;
}
}
当我调试时,我在登录视图模型中停止等待 Shell.Curent...我检查 HomeViewModel UserLogin 并且我有我的数据。但它似乎是在页面加载后将其清除。 有人可以帮我在 HomeViewModel 页面上获取数据吗? 谢谢。 Jc
【问题讨论】:
-
所以如果我明白没有办法轻松做到这一点?我不明白。 C#当然是面向对象的,没有一些复杂的技巧我不能传递一个对象??
-
有很多方法可以接近它。用户登录数据可以存储为 App 类的属性,因此可以普遍使用。如果您不使用 shell 导航,则可以更好地控制导航时数据的传递方式。或者有一个与其他问题相关联的项目可以使用 Shell