单击 Icommand 按钮时数据未进入视图模型

data is not getting in viewmodel on Icommand button click

我有一个用于捕获用户数据的表单。

 <StackLayout Spacing="10" Margin="20,10,20,0">
            <Label Text="Job Name"></Label>
            <Entry BackgroundColor="White" x:Name="JobName" Text="{Binding SelectedJob.Name,Mode=TwoWay}"/>
            <Label Text="Goal"></Label>
            <Entry BackgroundColor="White" x:Name="Goal" Text="{Binding SelectedJob.Goal,Mode=TwoWay}"/>
 <Grid ColumnSpacing="8" HorizontalOptions="FillAndExpand">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>
                <Button Text="Save" Command="{Binding SubmitCommand}" BackgroundColor="#078fc1" Grid.Column="0" HorizontalOptions="FillAndExpand"/>
                <Button Text="Reset"  BackgroundColor="#078fc1" Grid.Column="1" HorizontalOptions="FillAndExpand"/>

            </Grid>         
</StackLayout>

单击按钮我想将数据绑定到 SelectedJob 对象。

这是我的 ViewModel

    public class JobViewModel: INotifyPropertyChanged
        {
    public JobViewModel()
            {
                SubmitCommand = new Command(OnSubmitAsync);

            }
 private JobDTO selectedob { get; set; }
        public JobDTO SelectedJob
        {
            get { return selectedob; }
            set
            {
                selectedob = value;
                OnPropertyChanged(nameof(SelectedJob));
            }
        }
    public ICommand SubmitCommand { protected set; get; }
            public async void OnSubmitAsync()
            {
                await.jobservice.postjob()
            }
}

这是我的模型

public class JobDTO:INotifyPropertyChanged
{
    private string name { get; set; }
    public string Name
    { get { return name;}
        set
        {
            name = value;
            OnPropertyChanged(nameof(Name));
        }
    }

    private string goal;

    public string Goal
    {
        get { return goal; }
        set
        {
            goal = value;
            OnPropertyChanged(nameof(Goal));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

单击按钮时,我将 SelectedJob 设置为空,尽管我将 class 设置为 JobDTO:INotifyPropertyChanged。任何人都可以帮助我我在这里做错了什么。

我更改了 ViewModel 和模型的代码。 根据你的代码有演示的GIF。

JobViewModel 的 ViewModel

如果要使用get/set方法,一个属性对应一个get/set方法,我也加了OnPropertyChanged方法。

    public class JobViewModel : INotifyPropertyChanged
{
    public JobViewModel()
    {
        SubmitCommand = new Command(OnSubmitAsync);

    }


    private JobDTO selectedob=new JobDTO();
    public JobDTO SelectedJob
    {
        get { return selectedob; }
        set
        {
            selectedob = value;
            OnPropertyChanged("SelectedJob");
        }
    }
    public ICommand SubmitCommand { protected set; get; }

    public event PropertyChangedEventHandler PropertyChanged;

    public async void OnSubmitAsync()
    {


        // i donnot know what is your aim of this part, i just pop up a alert that contains Name and Goal from Entry.
        await Application.Current.MainPage.DisplayAlert("Alert", "Name: "+SelectedJob.Name+"\n"+ "Goal: "+ SelectedJob.Goal , "Ok");


    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var changed = PropertyChanged;
        if (changed != null)
        {

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }

}

JobDTO的模型,修改的地方同ViewModel

    public class JobDTO: INotifyPropertyChanged
{

    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    private string goal;

    public string Goal
    {
        get { return goal; }
        set
        {
            goal = value;
            OnPropertyChanged("Goal");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    protected virtual void OnPropertyChanged(string propertyName)
    {
        var changed = PropertyChanged;
        if (changed != null)
        {

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }

}

MainPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:DateBoundingDemo"
         x:Class="DateBoundingDemo.MainPage">


<StackLayout Spacing="10" Margin="20,10,20,0">

    <Label Text="Job Name"></Label>
    <Entry BackgroundColor="White" x:Name="JobName" Text="{Binding SelectedJob.Name,Mode=TwoWay}"/>
    <Label Text="Goal"></Label>
    <Entry BackgroundColor="White" x:Name="Goal" Text="{Binding SelectedJob.Goal,Mode=TwoWay}"/>
    <Grid ColumnSpacing="8" HorizontalOptions="FillAndExpand">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />

        </Grid.ColumnDefinitions>
        <Button Text="Save" Command="{Binding SubmitCommand}" BackgroundColor="#078fc1" Grid.Column="0" HorizontalOptions="FillAndExpand"/>
        <Button Text="Reset"  BackgroundColor="#078fc1" Grid.Column="1" HorizontalOptions="FillAndExpand"/>
     </Grid>
 </StackLayout>


</ContentPage>

MainPage.xaml.cs

    public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BindingContext =new JobViewModel();
    }
}