首页 > 解决方案 > 如何在 OnAppearing 方法中重新加载页面?

问题描述

我实际上正在做一些 xamarin 并且我创建了一个我想在很多页面中使用的自定义 NavigationBar。我在我的 PageA.xaml 中添加这样的导航栏:

<NavigationPage.TitleView > <cvNavBar:CustomNavigationBar Title="{Binding Title}" Voyage="{Binding Voyage}" SeeCariste="{Binding SeeCariste}"/> </NavigationPage.TitleView>

我在我的 PageB.xaml 中做了同样的事情

当我从我的 PageA 转到我的 PageB 时,我的 2 页上有一个不错的 NavigationBar: 在此处输入图像描述 但是当我从我的 PageB 转到我的 PageA 时,该栏“坏了”: 在此处输入图像描述

我的第一个想法是重新加载我的 PageA,在我的 PageA 中使用 OnAppearing 方法。所以我问你是否可能,或者你是否有其他想法?

谢谢 !

编辑:我的 CustomNavigationBar.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"

x:Class="LogiStock.UI.ContentViews.UI.NavigationBar.CustomNavigationBar"
         x:Name="CView" Padding="5">

<ContentView.Content>
    <!-- Conteneur de la NavigationBar -->
    <StackLayout Orientation="Horizontal">
        <!-- Titre de la page -->
        <StackLayout Orientation="Horizontal" 
HorizontalOptions="StartAndExpand" VerticalOptions="Center">
            <Label Text="{Binding Title}" LineBreakMode="TailTruncation" 
TextColor="White" FontSize="{Binding FontSizeXXLarge}"/>
        </StackLayout>

        <!-- Test ContentView pour contenu éventuel -->
        <ContentView Content="{Binding Source={x:Reference 
CView},Path=AdditionalContent}"
                     VerticalOptions="Center">
        </ContentView>


        <StackLayout Orientation="Horizontal" HorizontalOptions="End">
            <!-- Icône Settings -->
            <Image Source="{OnPlatform Android='settings', 
UWP='Assets/settings.png'}"
                   IsVisible="{Binding Source={x:Reference CView}, 
Path=SettingsVisible}">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Source= 
{x:Reference CView},Path=Parent.BindingContext.UneCommand}"/>
                </Image.GestureRecognizers>
            </Image>



            <!--Icône Transporteur-->
            <Image Source="{OnPlatform Android='chargement', 
UWP='Assets/chargement_small.png'}" 
                   IsVisible="{Binding Source={x:Reference CView}, 
Path=NotificationsVisible}"
                   Margin="3"
                   VerticalOptions="Center"
                   HorizontalOptions="End">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Source= 
{x:Reference CView},Path=Parent.BindingContext.LoginUserCommand}"/>
                </Image.GestureRecognizers>
            </Image>
            <!--Vignette-->
            <Frame Padding="4,1" 
                   Style="{StaticResource Key=frameVignette}"
                   HasShadow="False" 
                   BackgroundColor="Red" 
                   VerticalOptions="Start"
                   HorizontalOptions="Start"
                   Margin="-30,0,0,0">
                <Label Text="{Binding Voyage}"
                       TextColor="White"
                       FontSize="{Binding FontSizeXXSmall}"
                       FontAttributes="Bold"
                       HorizontalOptions="Center"
                       HorizontalTextAlignment="Center"/>
            </Frame>
            <!--Choix Cariste
            <Picker IsVisible="{Binding SeeCariste} " ItemsSource=" 
{Binding Caristes}" SelectedItem="{Binding SelectedCariste}" 
SelectedIndex="{Binding SelectedCaristeIndex}"
                ItemDisplayBinding="{Binding Code, Mode=TwoWay}" 
HorizontalOptions="End" WidthRequest="{OnPlatform Android=150, UWP=250}" 
TextColor="White" FontSize="{Binding FontSizeMedium}">
                <Picker.Behaviors>
                    <b:EventToCommandBehavior 
EventName="SelectedIndexChanged" Command="{Binding 
CaristeChangedCommand}"/>
                </Picker.Behaviors>
            </Picker>-->



        </StackLayout>
    </StackLayout>
</ContentView.Content>
</ContentView>

以及背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace LogiStock.UI.ContentViews.UI.NavigationBar
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomNavigationBar : ContentView
{
    /// <summary>
    /// Title représente le titre de la page.
    /// </summary>

    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    public string Voyage
    {
        get { return (string)GetValue(VoyageProperty); }
        set { SetValue(VoyageProperty, value); }
    }
    public bool SeeCariste
    {
        get { return (bool)GetValue(SeeCaristeProperty); }
        set { SetValue(SeeCaristeProperty, value); }
    }

    public bool SettingsVisible
    {
        get { return (bool)GetValue(SettingsVisibleProperty); }
        set { SetValue(SettingsVisibleProperty, value); }
    }

    public bool NotificationsVisible
    {
        get { return (bool)GetValue(NotificationsVisibleProperty); }
        set { SetValue(NotificationsVisibleProperty, value); }
    }

    public ContentView AdditionalContent
    {
        get { return (ContentView)GetValue(AdditionalContentProperty); }
        set { SetValue(AdditionalContentProperty, value); }
    }

    public static BindableProperty TitleProperty = BindableProperty
        .Create("Title",
        typeof(string),
        typeof(CustomNavigationBar),
        string.Empty,
        BindingMode.OneWay
        , propertyChanged: TitlePropertyChanged
        );

    public static BindableProperty VoyageProperty = BindableProperty
        .Create("Voyage",
        typeof(string),
        typeof(CustomNavigationBar),
        string.Empty,
        BindingMode.OneWay
        , propertyChanged: VoyagePropertyChanged
        );

    public static BindableProperty SeeCaristeProperty = BindableProperty
        .Create("SeeCariste",
        typeof(bool),
        typeof(CustomNavigationBar),
        false,
        BindingMode.OneWay
        , propertyChanged: SeeCaristeChanged
        );

    public static BindableProperty AdditionalContentProperty = 
BindableProperty
        .Create("AdditionalContent",
        typeof(ContentView),
        typeof(CustomNavigationBar),
        new ContentView(),
        BindingMode.OneWay
        , propertyChanged: AdditionalContentPropertyChanged
        );

    public static BindableProperty SettingsVisibleProperty = 
BindableProperty
        .Create("SettingsVisible",
        typeof(bool),
        typeof(CustomNavigationBar),
        false,
        BindingMode.OneWay
        , propertyChanged: SettingsVisiblePropertyChanged
        );

    public static BindableProperty NotificationsVisibleProperty = 
BindableProperty
        .Create("NotificationsVisible",
        typeof(bool),
        typeof(CustomNavigationBar),
        true,
        BindingMode.OneWay
        , propertyChanged: NotificationsVisiblePropertyChanged
        );

    private static void NotificationsVisiblePropertyChanged(BindableObject 
bindable, object oldValue, object newValue)
    {
        var control = (CustomNavigationBar)bindable;
        control.NotificationsVisible = (bool)newValue;
    }

    private static void SettingsVisiblePropertyChanged(BindableObject 
bindable, object oldValue, object newValue)
    {
        var control = (CustomNavigationBar)bindable;
        control.SettingsVisible = (bool)newValue;
    }

    private static void TitlePropertyChanged(BindableObject bindable, object 
oldValue, object newValue)
    {
        var control = (CustomNavigationBar)bindable;
        control.Title = newValue.ToString();
    }

    private static void VoyagePropertyChanged(BindableObject bindable, 
object oldValue, object newValue)
    {
        var control = (CustomNavigationBar)bindable;
        control.Voyage = newValue.ToString();
    }
    private static void SeeCaristeChanged(BindableObject bindable, object 
oldValue, object newValue)
    {
        var control = (CustomNavigationBar)bindable;
        control.SettingsVisible = (bool)newValue;
    }

    private static void AdditionalContentPropertyChanged(BindableObject 
bindable, object oldValue, object newValue)
    {
        //if (newValue == null)
        //    return;

        var control = (CustomNavigationBar)bindable;
        control.AdditionalContent = (ContentView)newValue;

        //CustomNavigationBar cnb = new CustomNavigationBar();
        //cnb.cvContenu = control.AdditionalContent;
    }

    public CustomNavigationBar()
    {
        InitializeComponent();
    }



}
}

标签: c#xamlxamarinreloadnavigationbar

解决方案


所以,我通过从我的 Frame 输出我的标签(你可以在我的 CustomNavigationBar 中看到它)解决了我的问题!现在一切顺利


推荐阅读