Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to bind XAML contentView to its code behind

$
0
0

I have Some UI logic and I need to Add them in Code behind. In order to overcome the code duplication, I need to use a property and change it. Normal MVVM thing. But when I try to do it with Codebehind. Mean I bind XAML with my code behind in order to access the isvisible function in several places. Problem is it does not binding or any other problem visibility did not change when action is fired.

My 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}"                                                                                                                                                                                                                                                                                                                             
                />

In code behind

 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);
}

}
How to bind this in xamarin forms. Is my binding is wrong or where is the problem


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>