如何使用MVVM方法检测何时按下回车键(或任何其他键)。我是xamarin的新手,所以我希望我能提出正确的问题。我的想法是在密码Entry中添加一个命令。这是正确的方法吗?我可以在后端代码中添加一个Completed Event,但是如何将其链接到我的视图模型?
这是我的XAML代码
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:newApp"
x:Class="newApp.MainPage">
<ContentPage.Content>
<StackLayout Padding="30" Spacing="10" VerticalOptions="Center">
<Image Source="logo.png" />
<Entry Text="{Binding Username}" Placeholder="Username" />
<Entry Text="{Binding Password}" IsPassword="True" Placeholder="Password" />
<Label Text="{Binding DisplayMessage}"/>
<Button Text="Log In" Command="{Binding LogInCommand}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
这是后端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace newApp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new LoginViewModel();
}
}
}
这是我的ViewModel
namespace newApp
{
class LoginViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string username, password;
Boolean bol;
void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public string Password
{
get { return password; }
set
{
password = value;
OnPropertyChanged();
}
}
public string Username
{
get { return username; }
set
{
username = value;
OnPropertyChanged();
OnPropertyChanged(nameof(DisplayMessage));
}
}
public string DisplayMessage
{
get
{
if (username != ""){return $"This is your {Username}";}
else return "";
}
}
void Login()
{
if (username == "")
bol = false;
}
}
}
答案 0 :(得分:2)
简短的回答是:你不能。 (define (count-change-iter amount)
(cc-fifties amount 0))
(define (cc-fifties amount acc)
(cond ((= amount 0) (+ 1 acc))
((< amount 0) acc)
(else (cc-fifties (- amount 50)
(cc-quarters amount acc)))))
(define (cc-quarters amount acc)
(cond ((= amount 0) (+ 1 acc))
((< amount 0) acc)
(else (cc-quarters (- amount 25)
(cc-dimes amount acc)))))
(define (cc-dimes amount acc)
(cond ((= amount 0) (+ 1 acc))
((< amount 0) acc)
(else (cc-dimes (- amount 10)
(cc-nickels amount acc)))))
(define (cc-nickels amount acc)
(cond ((= amount 0) (+ 1 acc))
((< amount 0) acc)
(else (cc-nickels (- amount 5)
(cc-pennies amount acc)))))
(define (cc-pennies amount acc)
(+ acc 1))
事件就是:一个事件。由于事件的工作方式,它们不适合MVVM模式。
有几种方法可以解决这个问题。首先,您可以在代码隐藏中捕获事件,然后在Completed
属性中的视图模型中触发代码。虽然你从MVVM模式中稍微偏离了一点,但这是解决这个问题的方法。
另一种选择是创建自己的控件继承并实现一个新的属性,它取一个BindingContext
。然后,您可以在内部将事件循环到Command
。
但对此最简单的解决方案可能是创建Command
,将您的活动转变为Behavior
。要创建一个可重用的Command
,将其上的任何事件转换为Behavior
,请执行此操作(完整实现可在以下链接中找到):
Command
对于您的public class EventToCommandBehavior : BehaviorBase<View>
{
public static readonly BindableProperty EventNameProperty =
BindableProperty.Create ("EventName", typeof(string), typeof(EventToCommandBehavior), null, propertyChanged: OnEventNameChanged);
public static readonly BindableProperty CommandProperty =
BindableProperty.Create ("Command", typeof(ICommand), typeof(EventToCommandBehavior), null);
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create ("CommandParameter", typeof(object), typeof(EventToCommandBehavior), null);
public static readonly BindableProperty InputConverterProperty =
BindableProperty.Create ("Converter", typeof(IValueConverter), typeof(EventToCommandBehavior), null);
public string EventName { ... }
public ICommand Command { ... }
public object CommandParameter { ... }
public IValueConverter Converter { ... }
...
}
,请按照以下方式附加:
Entry