'Xamarin.Forms: Reload a contentpage contained in a tabbedpage to update the data
The default language of my Xamarin.Forms app for Android is French. I would like the user to be able to change the language and choose English or Spanish. To do this, I added 3 resource files: AppResources.resx, AppResources.en.resx, and AppResources.es.resx to a Resx folder. In these files, I filled in the keywords to be translated. Here is the beginning of the xaml code of the page to be translated:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
xmlns:sync="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"
xmlns:resx="clr-namespace:MemoCourses.Resx"
mc:Ignorable="d"
x:Class="MemoCourses.Views.ParametreDestPage"
Title="{x:Static resx:AppResources.ParamDest_Titre}">
And the corresponding cs code :
using System;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using MemoCourses.Models;
using MemoCourses.ViewModels;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Threading;
using MemoCourses.Resx;
namespace MemoCourses.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ParametreDestPage : ContentPage
{
ObservableCollection<Parametre> DB_ParametreList = new ObservableCollection<Parametre>();
ReadAllParametreList dbparametre = new ReadAllParametreList();
ObservableCollection<Panier> DB_PanierList = new ObservableCollection<Panier>();
ReadAllPanierList dbpanier = new ReadAllPanierList();
//
Parametre parametres;
// ===
public ParametreDestPage()
{
InitializeComponent();
string language = Thread.CurrentThread.CurrentUICulture.Name;
picker.SelectedIndex = language == "es" ? 2 : language == "en" ? 1 : 0;
}
// ...
private void picker_SelectedIndexChanged(object sender, EventArgs e)
{
if (App.SelectedIndex == picker.SelectedIndex)
return;
App.SelectedIndex = picker.SelectedIndex;
CultureInfo language = new CultureInfo(picker.SelectedIndex == 0 ? "" : picker.SelectedIndex == 1 ? "en" : "es");
Thread.CurrentThread.CurrentUICulture = language;
AppResources.Culture = language;
Application.Current.MainPage = new NavigationPage(new ParametreDestPage());
}
}
}
Everything works except that this contentpage is part of a tabbedpage, and when I reload the page with the code:
Application.Current.MainPage = new NavigationPage(new ParametreDestPage());
... the title bar and tabs are no longer displayed. The user can no longer go back or access the menu. To complete the information in my code, here is the xaml page of the tabbedpage:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MemoCourses.Views"
mc:Ignorable="d"
BarBackgroundColor="DarkOliveGreen"
BarTextColor="White"
SelectedTabColor="White"
Title="{x:Static resx:AppResources.ParamPage_Titre}"
x:Class="MemoCourses.Views.ParametrePage">
<local:ParametreDestPage />
<local:ParametreVersionsPage />
</TabbedPage>
How do I apply the changes (refresh the data) to the ParametreDestPage by leaving the title and tabs visible? Thank you very much for your help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|