Hi CharlesMills-5302,
Welcome to our Microsoft Q&A platform!
Since login is not a frequent operation, creating a new instance will not take up much memory.
If you want to avoid creating new instance every time, here is a workaround that you can create a custom login ContentView and add it into the Page, then set its "IsVisible" property.
Here is a simple example you can refer to.
LoginView.xaml
<ContentView ...
xmlns:local="clr-namespace:WayToPopLoginPage.Views">
<ContentView.Content>
<StackLayout>
<Entry Placeholder="User Name"/>
<Entry Placeholder="Password"/>
<Button Text="login" Command = "{Binding Source={RelativeSource AncestorType={x:Type local:ItemsPage}}, Path=ShowCommand}"/>
</StackLayout>
</ContentView.Content>
</ContentView>
ItemPage.xaml (Show info in this page)
<ContentPage ...>
<Grid RowDefinitions="*">
<StackLayout IsVisible="{Binding Login}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Label Text="Content here"/>
</StackLayout>
<localview:LoginView IsVisible="{Binding Notlogin}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
</Grid>
</ContentPage>
ItemsPage.xaml.cs
public partial class ItemsPage : ContentPage, INotifyPropertyChanged
{
public ItemsPage()
{
InitializeComponent();
ShowCommand = new Command(Show);
BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
Login = Preferences.Get("logged", false);
Notlogin = !Preferences.Get("logged", false);
}
public ICommand ShowCommand { get; }
bool notlogin;
public bool Notlogin
{
get => notlogin;
set
{
notlogin = value;
OnPropertyChanged("Notlogin");
}
}
bool login;
public bool Login
{
get => login;
set
{
login = value;
OnPropertyChanged("Login");
}
}
private void Show()
{
Login = true;
Notlogin = false;
Preferences.Set("logged", true);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Regards,
Kyle
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.