我已经创建了一个与自定义Xamarin Forms Map相关的ViewModel
在此地图上,我正在根据条件在触发器上显示一个Entry字段
我已成功将Entry IsVisible参数绑定到该行为,并且可以在按钮上进行切换
但是,我真正想要实现的是响应于单击图钉
尝试此操作时,无法触发OnPropertyChange
我不确定如何克服这一点-引脚位于单独的类中,因此我无法将其绑定到包含文本项的Grid视图中
我已将click事件附加到自定义图钉,并使用它与视图模型进行交互
pin.Clicked += (object sender, EventArgs e) =>
{
var p = sender as CustomPin;
var callingMethod = new HandleEditor();
callingMethod.Pin_Clicked(p);
};
这是XAML
<Grid x:Name="LayoutGrid">
<Grid.BindingContext>
<local:HandleEditor />
</Grid.BindingContext>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="150" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<local:CustomMap Grid.RowSpan="3" Grid.Row="0" MapType="Street" WidthRequest="300" HeightRequest="300" />
<Editor x:Name="TextEntry" Grid.Row="1" VerticalOptions="FillAndExpand" IsVisible="{Binding SwitchVisible, Mode=TwoWay}"/>
<Button Grid.Row="2" Text="Switch Bool Test" Command="{Binding ChangeBoolValue}"/>
</Grid>
这是ViewModel
public event PropertyChangedEventHandler PropertyChanged;
bool isEditorVisible = false;
public ICommand ChangeBoolValue { protected set; get; }
public ICommand Debug { protected set; get; }
public bool SwitchVisible
{
get
{
System.Diagnostics.Debug.WriteLine("Debug: SwitchVisible has been fired " + isEditorVisible);
return isEditorVisible;
}
}
public HandleEditor()
{
ChangeBoolValue = new Command(() =>
{
if (isEditorVisible == true)
{
isEditorVisible = false;
OnPropertyChanged("SwitchVisible");
}
else
{
isEditorVisible = true;
OnPropertyChanged("SwitchVisible");
}
});
}
protected virtual void OnPropertyChanged(string propertyName)
{
System.Diagnostics.Debug.WriteLine("Debug: OnPropertyChanged has been fired " + propertyName + " : " + isEditorVisible);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void Pin_Clicked(CustomPin pin)
{
isEditorVisible = true;
OnPropertyChanged("SwitchVisible");
}
答案 0 :(得分:0)
所以我可能会错过一些东西,但是我看不到isEditorVisible
属性的用途。
我已经成功将Entry IsVisible参数绑定到了 行为,并可以在按钮上进行切换
您使用的是Editor
,而不是Entry
。另外,您将按钮的Command
属性绑定到Command
中的ViewModel
中的ChangeBoolValue
。您没有使用Behavior<T>
,或者您在代码段中未添加它:)。
<Editor x:Name="TextEntry" Grid.Row="1" VerticalOptions="FillAndExpand" IsVisible="{Binding SwitchVisible, Mode=TwoWay}"/>
所以您要实现的是Editor
IsVisibleProperty根据SwitchIsVisible
属性进行更改,这是您要绑定的属性,对吗?
我会在ViewModel中执行类似的操作,而不是实际的实现:
public event PropertyChangedEventHandler PropertyChanged;
public ICommand ChangeBoolValue { protected set; get; }
public ICommand Debug { protected set; get; }
private bool _switchVisible;
public bool SwitchVisible
{
get => _switchVisible;
set
{
if(_switchVisible == value) return;
_switchVisible = value;
OnPropertyChanged("SwitchVisible");
}
}
public HandleEditor()
{
ChangeBoolValue = new Command(() =>
{
SwitchVisible = !SwitchVisible;
});
}
protected virtual void OnPropertyChanged(string propertyName)
{
System.Diagnostics.Debug.WriteLine("Debug: OnPropertyChanged has been fired " + propertyName + " : " + isEditorVisible);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void Pin_Clicked(CustomPin pin)
{
SwitchVisible = true;
}