【问题标题】:data is not getting in viewmodel on Icommand button click数据未在 Icommand 按钮单击时进入视图模型
【发布时间】:2019-01-28 03:06:30
【问题描述】:

我有一个用于从用户那里获取数据的表单。

 <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 对象。

这是我的视图模型

    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 设为 null,尽管我将我的课程设为 JobDTO:INotifyPropertyChanged。任何人都可以帮助我做错了什么。

【问题讨论】:

    标签: mvvm data-binding xamarin.forms inotifypropertychanged


    【解决方案1】:

    我更改了 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的Model,改动的地方和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();
        }
    }
    

    【讨论】:

    • @ Leon Lu - MSFT 我之前缺少的是私有 JobDTO selectedob=new JobDTO();。这就是为什么它在保存命令时为空。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多