我正在尝试将可绑定属性添加到Xamarin.Forms中的条目。这应该允许我通过将布尔值分配给 HasFocus 属性来设置/取消设置条目的键盘焦点。我正在使用ReactiveUI作为MVVM框架,并且RaiseAndSetIfChanged方法隐式地引发了INotifyPropertyChanged事件(这在许多其他地方都有效(。
我无法在 FocusedEntry 类中命中任何断点,并且没有看到键盘出现。我错过了什么?
// XAML
<controls:FocusedEntry Text="My custom Entry"
HasFocus="{Binding EntryHasFocus, Mode=TwoWay}"/>
// View Model
private bool _entryHasFocus;
public bool EntryHasFocus
{
get => _entryHasFocus;
private set => this.RaiseAndSetIfChanged(ref _entryHasFocus, value);
}
// Custom View
public class FocusedEntry : Entry
{
public static readonly BindableProperty HasFocusProperty = BindableProperty.Create(
nameof(HasFocus), typeof(bool), typeof(FocusedEntry), false, BindingMode.TwoWay, propertyChanged: OnHasFocusedChanged);
public bool HasFocus
{
get => (bool)GetValue(HasFocusProperty);
set => SetValue(HasFocusProperty, value);
}
private static void OnHasFocusedChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is FocusedEntry entry)
{
bool hasFocus = (bool)newValue;
bool wasFocused = (bool)oldValue;
if (hasFocus == wasFocused) return;
if (hasFocus)
entry.Focus();
else
entry.Unfocus();
}
}
}
代码实际上一直有效。出于某种原因,Visual Studio没有更新iPad上的应用程序,我正在使用旧版本的应用程序进行测试。