Xamarin.Forms,如何从输入形式将对象发送到ViewModel?

问题描述 投票:0回答:1

大家好,我正在制作xamarin.forms应用程序,并具有两个字段Name和Surname的输入表单,它们来自对象User:

xml中的代码

<StackLayout Orientation="Vertical" >                     
        <Entry Placeholder="Name" MaxLength="30"  Text="{Binding Name}" WidthRequest="220"> </Entry>                                                                            
        <Entry Placeholder="Surname" Text="{Binding Surname}" WidthRequest="220"></Entry>
        <Button Command={Binding SaveUserCommand }> Save user </Button>
 </StackLayout>                       

在ViewModel中,我已定义:

public Command SaveUserCommand { get; set; }  
      public User user;
       public User User
       {
           get { return user; }
           set
           {
               if (user != value)
               {
                   user = value;

                   SetProperty(ref user, value);
                   OnPropertyChanged("User");

               }
           }
       } 
       public SaveUserViewModel()
       {

           SaveUserCommand = new Command(execute: async () => await ExecuteUserCommand());

       }
 async Task ExecuteUserCommand()
       {

           try
           {

               User = await DataStore.Saveuser(User); 
           }
           catch (Exception ex)
           {
               Debug.WriteLine(ex);
           }

       }

我想发送类似的对象,但不起作用吗?我不想像单独的字符串Name和Surname一样发送,我想像对象User一样发送它们?有人可以帮助我吗?

xamarin.forms xamarin.android xamarin.ios
1个回答
0
投票
您需要将模型与INotifyPropertyChanged实现一起使用。
© www.soinside.com 2019 - 2024. All rights reserved.