Xaml 如何使用ViewModel绑定页面中的条目

Xaml 如何使用ViewModel绑定页面中的条目,xaml,mvvm,xamarin.forms,binding,Xaml,Mvvm,Xamarin.forms,Binding,我正在尝试实现如下登录系统: public Command LoginCommand => new Command(async () => { LoginModel model = new LoginModel("dnetTest", "dnetTest"); // (get value from entry instead of "dnetTest") if (model.CheckInformation())

我正在尝试实现如下登录系统:

public Command LoginCommand => new Command(async () =>
        {
            LoginModel model = new LoginModel("dnetTest", "dnetTest"); // (get value from entry instead of "dnetTest")

        if (model.CheckInformation())
        {
            bool isSuccess = await LoginService.Login(model);
            if (isSuccess)
            {
                await Application.Current.MainPage.DisplayAlert("Пријављивање", "Успешно сте се пријавили", "OK.");
                Application.Current.MainPage = new MainPage();
            }
        }
public string Username { get; set; }
public string Password { get; set; }
public Command LoginCommand => new Command(async () =>
{
    // Notice how I changed this line
    LoginModel model = new LoginModel(Username, Password);

    if (model.CheckInformation())
    {
        bool isSuccess = await LoginService.Login(model);
        if (isSuccess)
        {
            await Application.Current.MainPage.DisplayAlert("Пријављивање", "Успешно сте се пријавили", "OK.");
            Application.Current.MainPage = new MainPage();
        }
    }
我的登录页也有:

 <Label Text="Korisničko ime"/>
 <Entry x:Name="Entry_Username" Placeholder="Korisničko ime"/>
 <Label Text="Lozinka"/>
 <Entry x:Name="Entry_Password" Placeholder="Lozinka"/>
 <Button Text="Prijavi se" Command="{Binding LoginCommand}"/>

所以,我的问题是如何在LoginView模型中将Entry_用户名和Entry_密码与LoginModel绑定?
有没有办法不使用x:Names来绑定它?

公平地说;这是一个很容易找到的非常基本的MVVM问题,其中的解决方案存在于多个博客和页面中。但是,让我来帮你开始吧

有没有办法不使用x:name来绑定它

数据绑定的全部要点是,您不必对这些控件进行任何硬引用。我们希望将逻辑从UI中分离出来,这样我们就可以轻松地替换其中一个或两个,而无需触摸另一个。例如,假设您想要实现一个新的设计,如果您使用数据绑定,那么如果您在新的UI中引用这些属性,您可以在视图模型(或者在Xamarin空间中引用的页面模型)中显示当前属性

如果您要有所有类型的
x:Name
引用,那么您不仅需要触摸UI,还需要触摸视图模型,查看所有对这些字段的引用并替换它们

使用数据绑定在很大程度上促进了可重用性和可测试性

至于你的具体情况。我看不到你的完整代码,所以这里会有一些假设。首先,我将假设您的
LoginCommand
现在位于自己的视图模型中。您已经在那里使用了数据绑定,这很好。我不明白为什么视图模型和登录需要单独的模型,可能您的
LoginModel
更像一个服务。此外,我假设您正在手动执行此操作,而无需MVVM框架的帮助。知道引擎盖下会发生什么很好,但我建议使用MVVM框架,例如FreshMvvm或Prism

在保存XAML的登录页面上,我将调用
LoginPage.XAML
,它应该有一个
LoginPage.XAML.cs
代码隐藏文件。在其中,进入构造函数并指定以下行:

public LoginPage()
{
    InitializeComponents();

    // This line is relevant here
    BindingContext = new LoginViewModel();
}
看到您的
LoginCommand
已经在使用数据绑定,这可能已经在这里了

现在,在您的
LoginPage.xaml
中,将您的xaml更改为:

 <Label Text="Korisničko ime"/>
 <Entry Text="{Binding Username}" Placeholder="Korisničko ime"/>
 <Label Text="Lozinka"/>
 <Entry Text="{Binding Password}" Placeholder="Lozinka"/>
 <Button Text="Prijavi se" Command="{Binding LoginCommand}"/>
每当
条目中的文本发生更改时
控件中的这些属性都应包含相应的值。现在,您可以将发布的代码更改为以下内容:

public Command LoginCommand => new Command(async () =>
        {
            LoginModel model = new LoginModel("dnetTest", "dnetTest"); // (get value from entry instead of "dnetTest")

        if (model.CheckInformation())
        {
            bool isSuccess = await LoginService.Login(model);
            if (isSuccess)
            {
                await Application.Current.MainPage.DisplayAlert("Пријављивање", "Успешно сте се пријавили", "OK.");
                Application.Current.MainPage = new MainPage();
            }
        }
public string Username { get; set; }
public string Password { get; set; }
public Command LoginCommand => new Command(async () =>
{
    // Notice how I changed this line
    LoginModel model = new LoginModel(Username, Password);

    if (model.CheckInformation())
    {
        bool isSuccess = await LoginService.Login(model);
        if (isSuccess)
        {
            await Application.Current.MainPage.DisplayAlert("Пријављивање", "Успешно сте се пријавили", "OK.");
            Application.Current.MainPage = new MainPage();
        }
    }
这应该适合你


如前所述,我建议从整体上进一步研究MVVM以及MVVM框架。这是亚当·佩德利(Adam Pedley)不久前的作品。

谢谢,它正在发挥作用。整个问题出现在
公共字符串用户名{get;set;}公共字符串密码{get;set;}
中,因为我是在LoginModel.cs中定义的,而不是在LoginViewModel中定义的。太棒了!是的,数据绑定引擎在页面的
BindingContext
属性中定义的对象中查找属性