簡體   English   中英

如何在Xamarin表單中刷新自定義工具欄

[英]How to Refresh Custom Toolbar in Xamarin Forms

我有一個自定義工具欄,這里是代碼:

public BoloToolbar()
            : base()
    {
        Init();
    }

    private void Init()
    {
        ClientViewModel Client = new ClientViewModel();

        if (Client.IsLogged == "true")
        {          
            this.ToolbarItems.Add(new ToolbarItem ("Twój Koszyk", "Images/cart.png", ()  =>
            {
                Navigation.PushAsync(new CartPage());
            }));

            this.ToolbarItems.Add(new ToolbarItem("Moje Zamówienia", null, () =>
            {
                Navigation.PushAsync(new Zamowienia());
            }, ToolbarItemOrder.Secondary, priority:0));


            this.ToolbarItems.Add(new ToolbarItem("Mój Profil", null, () =>
            {
                Navigation.PushAsync(new Profile());
            }, ToolbarItemOrder.Secondary, priority: 0));

            this.ToolbarItems.Add(new ToolbarItem("Ustawienia", null, () =>
            {
            }, ToolbarItemOrder.Secondary, priority: 0));

            this.ToolbarItems.Add(new ToolbarItem("Kontakt", null, () =>
            {
                Navigation.PushAsync(new Kontakt());
            }, ToolbarItemOrder.Secondary, priority: 0));

            this.ToolbarItems.Add(new ToolbarItem("Wyloguj", null, () =>
            {
                //Navigation.PushAsync(new Kontakt());
                Application.Current.Properties["isLogged"] = "false";
                Application.Current.Properties["userId"] = "";
                DisplayAlert("Logout", "Wylogowano Pomyślnie", "OK");
            }, ToolbarItemOrder.Secondary, priority: 0));

        } else
        {
            this.ToolbarItems.Add(new ToolbarItem("Zaloguj", null, () =>
            {
                Navigation.PushAsync(new LogRegister());
            }, ToolbarItemOrder.Secondary, priority: 0));

            this.ToolbarItems.Add(new ToolbarItem("Utwórz Konto", null, () =>
            {
                Navigation.PushAsync(new RegisterAccount());
            }, ToolbarItemOrder.Secondary, priority: 0));
        }

並說我啟動了應用程序並登錄等。每當我回到有工具欄的頁面(讓我們說“主頁”)時,工具欄不會刷新(它不會重新檢查您是否已實際登錄)

我知道ViewModels有INotifyPropertyChanged,自定義工具欄有類似的東西嗎?

這是因為僅調用一次構造函數即可在OnAppearing方法中移動此代碼,並且該代碼應該可以工作。

public BoloToolbar(): base()
    {

    }
protected override void OnAppearing()
    {
        base.OnAppearing();
        Init();
    }
    private void Init()
    {
        ClientViewModel Client = new ClientViewModel();

        if (Client.IsLogged == "true")
        {          
            this.ToolbarItems.Add(new ToolbarItem ("Twój Koszyk", "Images/cart.png", ()  =>
            {
                Navigation.PushAsync(new CartPage());
            }));

            this.ToolbarItems.Add(new ToolbarItem("Moje Zamówienia", null, () =>
            {
                Navigation.PushAsync(new Zamowienia());
            }, ToolbarItemOrder.Secondary, priority:0));


            this.ToolbarItems.Add(new ToolbarItem("Mój Profil", null, () =>
            {
                Navigation.PushAsync(new Profile());
            }, ToolbarItemOrder.Secondary, priority: 0));

            this.ToolbarItems.Add(new ToolbarItem("Ustawienia", null, () =>
            {
            }, ToolbarItemOrder.Secondary, priority: 0));

            this.ToolbarItems.Add(new ToolbarItem("Kontakt", null, () =>
            {
                Navigation.PushAsync(new Kontakt());
            }, ToolbarItemOrder.Secondary, priority: 0));

            this.ToolbarItems.Add(new ToolbarItem("Wyloguj", null, () =>
            {
                //Navigation.PushAsync(new Kontakt());
                Application.Current.Properties["isLogged"] = "false";
                Application.Current.Properties["userId"] = "";
                DisplayAlert("Logout", "Wylogowano Pomyślnie", "OK");
            }, ToolbarItemOrder.Secondary, priority: 0));

        } else
        {
            this.ToolbarItems.Add(new ToolbarItem("Zaloguj", null, () =>
            {
                Navigation.PushAsync(new LogRegister());
            }, ToolbarItemOrder.Secondary, priority: 0));

            this.ToolbarItems.Add(new ToolbarItem("Utwórz Konto", null, () =>
            {
                Navigation.PushAsync(new RegisterAccount());
            }, ToolbarItemOrder.Secondary, priority: 0));
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM