当我单击xamarin表单中的提交按钮时,如何将isenabled属性绑定到MVVM中的条目

问题描述

当我使用mvvm架构以xamarin表单提交表单时,我遇到了一个问题,我的表单UI仍然可以使用,并且用户可以在从服务器获取数据时进行交互。我想在运行提交按钮以从服务器获取数据时禁用UI元素。实际上,我想在我的视图模型中绑定isEnabled属性。但是我不知道如何从我的视图模型中将其设置为bool值,然后将其绑定到UI元素。我需要在set函数中添加什么,以便当有人单击“提交”按钮时,我的UI元素将处于非活动状态,并且用户只有在响应来自服务器时才能进行编辑。 该怎么办请协助。这是我的代码。

Blockquote

            <StackLayout>
            <Entry x:Name="entryFullName" 
                   Text="{Binding FullName}" 
                   Placeholder="Full Name" 
                   IsEnabled="{Binding block}"
                   />
            
            <Picker x:Name="pickerGender"
                    Title="Gender"
                    ItemsSource="{Binding Genders}"
                    SelectedItem="{Binding SelectedGender}"
                    IsEnabled="{Binding gender}"
                    />
                 </StackLayout>
                 <StackLayout>
            <Button x:Name="btnSubmit"
                    Command="{Binding SubmitCommand}"
                    Text="Submit"
                    />
        </StackLayout>
        <ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" />

这是我的视图模型提交按钮功能的代码

Blockquote

             private string _Block;

             public string Block
           {
        get { return _Block }
        set { _Block = value; OnPropertyChanged(); }
           }
             
     private void OnSubmit()

    {

        if (string.IsNullOrEmpty(this.FullName))
        {
            this.ErrorOccurred?.Invoke(this,"Please enter full name");
            return;
        }
           Device.BeginInvokeOnMainThread(async () => await this.SaveProfile();
      }

解决方法

首先,将所有spring.jpa.hibernate.ddl-auto = create 属性绑定到相同的VM属性

IsEnabled

然后在您的MV中创建一个bool属性

<Entry x:Name="entryFullName" IsEnabled="{Binding NotBusy}" ... />
<Picker x:Name="pickerGender" IsEnabled="{Binding NotBusy}" ... />
...
<Button x:Name="btnSubmit" IsEnabled="{Binding NotBusy}" ... />
                

最后,保存时设置属性

private bool _NotBusy = true;

public bool NotBusy
{
   get { return _NotBusy }
   set { _NotBusy = value; OnPropertyChanged(); }
}
,

您可以添加属性IsNotSubmitting,

private bool _isNotSubmitting = true;
public bool IsNotSubmitting {
    get => _isNotSubmitting ;
    set {
        _isNotSubmitting  = value;
        OnPropertyChanged();
    }
}

在Xaml中绑定:

 <Entry x:Name="entryFullName"
    Text="{Binding FullName}" 
    Placeholder="Full Name" 
    IsEnabled="{Binding IsNotSubmitting}"  />

现在您可以在方法SubmitCommand的开头设置“ IsNotSubmitting = false”,并且在提交完成后可以设置“ IsNotSubmitting = true”

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...