【问题标题】:How to get a number input from the ViewModel in Xamarin forms如何从 Xamarin 表单中的 ViewModel 获取数字输入
【发布时间】:2020-10-08 17:13:40
【问题描述】:

我需要在命令运行后向用户请求一个数字。这需要在视图模型中运行。我试过 ACR.UserDialog,但我似乎无法弄清楚如何从视图模型中调用它。

类似这样的:

void RemoveItem()
{ 
    int intQuantity = (dialog from user to get the quantity);
}

提前致谢!

【问题讨论】:

  • 无法理解的不完整问题,请修改以获得更好的答案
  • 对不起,我不知道它是如何不完整的。我只需要能够向用户询问来自 Viewmodel 的数字输入。也许你有一些建议?或需要澄清的问题?我不知道还能怎么问。谢谢。
  • 仍然,不明白 从 ViewModel 输入的数字 是什么意思......以及与 ACR 的链接是什么。分步写下你想要达到的目标。

标签: c# xamarin xamarin.forms dialog


【解决方案1】:

你想在 viewModel 中使用 Command 来达到如下结果吗?

首先,您需要创建AbstractViewModel

using System.ComponentModel;
using System.Runtime.CompilerServices;
using Acr.UserDialogs;

namespace MyCusListview
{
    public abstract class AbstractViewModel : INotifyPropertyChanged
    {
        protected AbstractViewModel(IUserDialogs dialogs)
        {
            this.Dialogs = dialogs;
        }


        protected IUserDialogs Dialogs { get; }


        protected virtual void Result(string msg)
        {
            this.Dialogs.Alert(msg);
        }


        public event PropertyChangedEventHandler PropertyChanged;

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

然后实现这个抽象类,不要忘记在你的视图模型构造函数中添加IUserDialogs dialogs。然后推送dialogs.PromptAsync,即可在input.Text得到结果。

  public class PersonsViewModel: AbstractViewModel
    {   
        public ObservableCollection<Person> persons { get; set; }
        public static ObservableCollection<Person> selectItems { get; set; }

        public ICommand TextChangeCommand { protected set; get; }
       

     
        public PersonsViewModel(INavigation navigation, IUserDialogs dialogs, ContentPage page):base(dialogs)
        {

            TextChangeCommand = new Command<Person>(async (key) =>
            {
                var input = await dialogs.PromptAsync("What is your email?", "Confirm Email", "Confirm", "Cancel");

                if (input.Ok)
                {
                    Console.WriteLine("Your email is" + input.Text);
                }

            });
        }
     }

当你在后台代码中绑定viewModel时,你只需要在初始化viewmodel的时候使用下面的代码。

  public MainPage()
        {
           InitializeComponent();

            personsViewModel =  new PersonsViewModel(Navigation, UserDialogs.Instance, this);
            this.BindingContext = personsViewModel;



        }

这是运行结果。

【讨论】:

    【解决方案2】:

    samples 显示如何执行此操作

    // Dialogs is an IUserDialogs that you pass to your VM from your page
    // using UserDialogs.Instance
    var result = await Dialogs.PromptAsync(new PromptConfig()
    
      .SetTitle("Max Length Prompt")
      .SetPlaceholder("Maximum Text Length (10)")
      .SetInputMode(InputType.Name)
      .SetMaxLength(10));
    
    // result.Text will contain the user's response
    

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 2018-02-16
      • 2020-08-28
      • 1970-01-01
      相关资源
      最近更新 更多