C# 如何从Xamarin表单中的ViewModel获取数字输入
我需要在命令运行后向用户请求一个号码。这需要在视图模型中运行。我尝试过ACR.UserDialog,但似乎不知道如何从viewmodel调用它C# 如何从Xamarin表单中的ViewModel获取数字输入,c#,xamarin,xamarin.forms,dialog,C#,Xamarin,Xamarin.forms,Dialog,我需要在命令运行后向用户请求一个号码。这需要在视图模型中运行。我尝试过ACR.UserDialog,但似乎不知道如何从viewmodel调用它 public MainPage() { InitializeComponent(); personsViewModel = new PersonsViewModel(Navigation, UserDialogs.Instance, this); this.Bi
public MainPage()
{
InitializeComponent();
personsViewModel = new PersonsViewModel(Navigation, UserDialogs.Instance, this);
this.BindingContext = personsViewModel;
}
大概是这样的:
void RemoveItem()
{
int intQuantity = (dialog from user to get the quantity);
}
提前谢谢 演示如何做到这一点
// 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
是否希望在viewModel中使用命令获得如下结果 首先,您需要创建
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));
}
}
}
然后实现这个抽象类,不要忘记在viewmodel构造函数中添加IUserDialogs对话框。然后按下对话框.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输入的数字是什么意思。。与ACR的联系是什么。分步写下你想要达到的目标。