代码之家  ›  专栏  ›  技术社区  ›  Journeyman1234

Xamarin形成行为-与外部类的交互

  •  0
  • Journeyman1234  · 技术社区  · 7 年前

    我已经创建了一个与自定义Xamarin表单映射相关的ViewModel

    在这个地图上,我在触发器上显示一个输入字段,根据条件显示为可见或不可见

    我已经成功地将条目IsVisible参数绑定到行为,并且可以在按钮上进行切换

    然而,我真正想要的是实现这一点,以回应点击一个引脚

    尝试此操作时,无法启动OnPropertyChange

    我不知道如何克服这个问题-pin在一个单独的类中,所以我无法将它绑定到包含文本条目的网格视图

    我在自定义pin上附加了一个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 回复  |  直到 7 年前
        1
  •  0
  •   Umar3x    7 年前

    isEditorVisible 财产。

    我已成功地将条目IsVisible参数绑定到

    您正在使用 Editor Entry . 另外,你正在绑定 Command 命令 在你的 ViewModel ChangeBoolValue . 您没有使用 Behavior<T>

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

    所以你想要达到的是 编辑 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;
    }
    
    推荐文章