【发布时间】:2019-03-30 18:31:46
【问题描述】:
我创建了一个与自定义 Xamarin 表单映射相关的 ViewModel
在这个地图上,我在触发器上显示一个条目字段,根据条件可见或不可见
我已成功将 Entry IsVisible 参数绑定到行为,并且可以在按钮上切换它
但是,我真正想要的是响应点击 Pin 来实现这一点
当我尝试这样做时,我无法触发 OnPropertyChange
我不知道如何解决这个问题 - 引脚位于单独的类中,因此我无法将其绑定到包含文本条目的网格视图
我已将点击事件附加到自定义引脚并使用它与视图模型交互
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>
这是视图模型
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");
}
【问题讨论】:
-
所以基本上你想要的是当你点击图钉时,条目应该改变可见性?
-
@G.hakim - 这是正确的,我希望 pin 信息摘要窗口附加一个单击事件(在我的代码示例中)并触发网格上的行为,或 Entry 元素,然后更改可见性
标签: xamarin xamarin.forms