Forum Xamarin Xamarin.Forms

Shell.FlyoutHeaderTemplate Not Refresh

fakhravarifakhravari Member ✭✭✭✭

Hi
At first I have Three buttons

1) go to lagin page
2) go to main page and refresh Shell.FlyoutHeaderTemplate
3) hide bottom login and register and sho exit

not working FlyoutHeaderTemplate Not Refresh



Best Answer

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited January 16 Accepted Answer

    I've reproduced your code on my side. Please mark sure the binding model calss instance as unique to ensure that its properties can be changed correctly after the operation. Create a static instance of the ModelItem class in App class, then you can reference it and make changes in other pages.

    Check the code:

    App.xaml.cs

    public partial class App : Application
    {
        public static ModelItem model = null;
        public App()
        {
            InitializeComponent();
            ...
            model = new ModelItem { IsExitAcount = true, IsLogin = true, IsRegister = true };
            MainPage = new ShellMain();
        }
    }
    

    Login page

    public partial class Login : ContentPage
    {
        public Login()
        {
            InitializeComponent();
        }
    
        private async void Button_Clicked(object sender, EventArgs e)
        {
            ...
            if (Login.Status)
            {
                ...
                App.model.IsLogin = false;
                App.model.IsRegister = false;
                await Navigation.PopAsync();
                //await Navigation.PushAsync(new ShellMain());
            }
            else
            {
                await DisplayAlert("توجه", Login.Body, "فهمیدم");
            }
        }
    }
    

    Comment the code of 'Cheeker' method in FlyoutHeader page.

    public partial class FlyoutHeader : ContentView
    {
        public FlyoutHeader()
        {
            mdl = new ModelItem();
            this.BindingContext = App.model;
    
            InitializeComponent();
            //Cheeker();
            MessagingCenter.Subscribe<App>((App)Application.Current, "RefreshLogin", (sender) => { Cheeker(); });
        }
    
        private async void Cheeker()
        {
            ...
        }
    
        private async void Login_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new Login());
        }
        private async void Register_Clicked(object sender, EventArgs e)
        {
            ...
        }
        private async void ExitAcount_Clicked(object sender, EventArgs e)
        {
            ...
        }
        private async void ExitAcount2_Clicked(object sender, EventArgs e)
        {
            ...
        }
    }
    

    ModelItem class

    public class ModelItem : INotifyPropertyChanged
    {
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        private bool isExitAcount;
        public bool IsExitAcount
        {
            get
            {
                return isExitAcount;
            }
            set
            {
                isExitAcount = value;
                RaisePropertyChanged();
            }
        }
    
        private bool isLogin;
        public bool IsLogin
        {
            get
            {
                return isLogin;
            }
            set
            {
                isLogin = value;
                RaisePropertyChanged();
            }
        }
    
        private bool isRegister;
        public bool IsRegister
        {
            get
            {
                return isRegister;
            }
            set
            {
                isRegister = value;
                RaisePropertyChanged();
            }
        }
    }
    

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited January 16 Accepted Answer

    I've reproduced your code on my side. Please mark sure the binding model calss instance as unique to ensure that its properties can be changed correctly after the operation. Create a static instance of the ModelItem class in App class, then you can reference it and make changes in other pages.

    Check the code:

    App.xaml.cs

    public partial class App : Application
    {
        public static ModelItem model = null;
        public App()
        {
            InitializeComponent();
            ...
            model = new ModelItem { IsExitAcount = true, IsLogin = true, IsRegister = true };
            MainPage = new ShellMain();
        }
    }
    

    Login page

    public partial class Login : ContentPage
    {
        public Login()
        {
            InitializeComponent();
        }
    
        private async void Button_Clicked(object sender, EventArgs e)
        {
            ...
            if (Login.Status)
            {
                ...
                App.model.IsLogin = false;
                App.model.IsRegister = false;
                await Navigation.PopAsync();
                //await Navigation.PushAsync(new ShellMain());
            }
            else
            {
                await DisplayAlert("توجه", Login.Body, "فهمیدم");
            }
        }
    }
    

    Comment the code of 'Cheeker' method in FlyoutHeader page.

    public partial class FlyoutHeader : ContentView
    {
        public FlyoutHeader()
        {
            mdl = new ModelItem();
            this.BindingContext = App.model;
    
            InitializeComponent();
            //Cheeker();
            MessagingCenter.Subscribe<App>((App)Application.Current, "RefreshLogin", (sender) => { Cheeker(); });
        }
    
        private async void Cheeker()
        {
            ...
        }
    
        private async void Login_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new Login());
        }
        private async void Register_Clicked(object sender, EventArgs e)
        {
            ...
        }
        private async void ExitAcount_Clicked(object sender, EventArgs e)
        {
            ...
        }
        private async void ExitAcount2_Clicked(object sender, EventArgs e)
        {
            ...
        }
    }
    

    ModelItem class

    public class ModelItem : INotifyPropertyChanged
    {
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        private bool isExitAcount;
        public bool IsExitAcount
        {
            get
            {
                return isExitAcount;
            }
            set
            {
                isExitAcount = value;
                RaisePropertyChanged();
            }
        }
    
        private bool isLogin;
        public bool IsLogin
        {
            get
            {
                return isLogin;
            }
            set
            {
                isLogin = value;
                RaisePropertyChanged();
            }
        }
    
        private bool isRegister;
        public bool IsRegister
        {
            get
            {
                return isRegister;
            }
            set
            {
                isRegister = value;
                RaisePropertyChanged();
            }
        }
    }
    

  • fakhravarifakhravari Member ✭✭✭✭

    Peace be upon you :)

Sign In or Register to comment.