【问题标题】:Xamarin Forms Behaviors - interaction with external classXamarin Forms Behaviors - 与外部类的交互
【发布时间】: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


【解决方案1】:

所以我可能会错过一些东西,但我看不到 isEditorVisible 属性的用途。

我已成功将 Entry IsVisible 参数绑定到 行为,并且可以在按钮上切换它

您使用的是Editor,而不是Entry。另外,您将按钮的Command 属性绑定到ViewModel 中的Command,即ChangeBoolValue。您没有使用Behavior&lt;T&gt;,或者您在片段中错过了它:)。

<Editor x:Name="TextEntry" Grid.Row="1" VerticalOptions="FillAndExpand" IsVisible="{Binding SwitchVisible, Mode=TwoWay}"/>  

那么你要实现的是EditorIsVisibleProperty 来根据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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多