0

我有一个自定义工具栏,我在这里制作的是代码:

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

假设我启动应用程序并登录等,每当我回到我有工具栏的页面(比如说主页)时,工具栏不会刷新(它不会重新检查你是否真的登录了)

我知道视图模型有 INotifyPropertyChanged,自定义工具栏有类似的东西吗?

4

1 回答 1

1

这是因为构造函数只被调用一次,在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));
        }
于 2019-04-11T10:50:32.580 回答