如何将XAML contentView与其后面的代码绑定

时间:2019-04-29 06:29:44

标签: c# xaml xamarin.forms

我有一些UI逻辑,我需要在后面的代码中添加它们。为了克服代码重复,我需要使用一个属性并对其进行更改。正常的MVVM。但是,当我尝试使用Codebehind进行操作时。意味着我将XAML与我的代码绑定在一起,以便在多个位置访问isvisible函数。问题是它没有约束力,或者在触发操作时可见性没有任何其他变化。

我的ContentView Xaml

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:customRenderes="clr-namespace:DipsDemoXaml.CustomRenderes;assembly=DipsDemoXaml"
         x:Class="DipsDemoXaml.Views.Page1"
         x:Name="navi">

<StackLayout   BindingContext="{x:Reference Name=navi}">
        <customRenderes:NavigationImageButton Source="MenuSettings"                                                
                                                      x:Name="Button1"                                                        
                                                    Margin="0"                                                       
                                                    IsVisible="{Binding Visibility}"                                                                                                                                                                                                                                                                                                                             
                />

在后面的代码中

public partial class Page1 : ContentView
{
    private bool _expand;

    private bool _visibility;

    public bool Visibility
    {
        get => _visibility;
        set
        {
            _visibility = value;
            OnPropertyChanged();
        }
    }

    public Page1 ()
    {
        InitializeComponent ();        
    }

    private  void  Button1_OnItemTapped(object sender, EventArgs e)
    {
        if (_expand)
        {
            this.Hide();
        }
        else
        {
            this.Expand();
        }
    }

    private async void Expand()
    {
        _expand = true;
        Button5.Opacity = 1;          
        _visibility = true;
        await Button5.RotateTo(180, 200);
    }

    private async void Hide()
    {
        _expand = false;
        Button5.Opacity = 0.4;
        _visibility = false;
        await Button5.RotateTo(360, 200);
    }
}

如何以xamarin形式绑定它。是我的绑定错误还是问题所在

我的财产变更方法

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;

        changed?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

2 个答案:

答案 0 :(得分:1)

首先,PropetyChanged接受一个参数,

 OnPropertyChanged("Visibility");

我想这应该可行,但是将您是ViewModel代码放在后面的代码中是很奇怪的。

MVVM的想法是将逻辑从页面移动到ViewModel,从而使您可以使用XAML后面的页面中几乎0的代码来管理同一ViewModel中多个页面的状态。

因此,您可能应该创建另一个称为ViewModel的文件,并将您的业务逻辑放入其中。

 <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     xmlns:customRenderes="clr- 
     namespace:DipsDemoXaml.CustomRenderes;assembly=DipsDemoXaml"
     x:Class="DipsDemoXaml.Views.Page1"
     xmlns:vm="clr-namespace:DipsDemoXaml.ViewModels;assembly=DipsDemoXaml"
     x:Class="DipsDemoXaml.Views.Page1"
     x:Name="navi">

<ContentView.BindingContext>
  <vm:MyViewModel/>
</ContentView.BindingContext>
<StackLayout>
<customRenderes:NavigationImageButton Source="MenuSettings"                                                
                                      x:Name="Button1"                                                        
                                      Margin="0"                                                       
                                      IsVisible="{Binding Visibility}"/>                                                                                                                                                                                                                                                                                                                     

在MyViewModel.cs中:

private bool _visibility;

public bool Visibility
{
    get => _visibility;
    set
    {
        _visibility = value;
        OnPropertyChanged("Visibility");
    }
}

因此,您可以处理所需的任何绑定,并轻松地在不同页面中使用它们。

我希望这会有所帮助。

答案 1 :(得分:1)

呼叫Visibility,而不是_visibility或手动触发OnPropertyChanged。但是第一个选项是首选。顺便说一句,我不知道您的OnPropertyChanged实现是什么,但通常会使用您的属性名称(如@Maniax所述)或使用nameof运算符(例如OnPropertyChanged(nameof(Visibility))