How to make my contentview in the center of the screen, above my grid? - xaml

Right now my ContentView (acts like a pop up window) stacks on top of my grid, see code below:
<StackLayout>
<ContentView>...</>
<StackLayout>
<Grid>...</>
</StackLayout>
</StackLayout>
The grid is a form, so when I press a button in my form, I want a new pop up window (the contentview) to be above the grid, in the center of the screen. The idea is that you can still see some of the form behind the contentview.
How do I put contentview in the front and not stacked on top of my grid?
Here's my contentview
<ContentView x:Name="popupAddDetails" AbsoluteLayout.LayoutBounds="0,0,1,1"
VerticalOptions="Center" HorizontalOptions="Center">
PS: I'm new to this environment and still learning. Thankful for any help
I tried to make a stacklayout above my first stacklayout with my grid in it and then set vertical and horizontal attributes in my contentview to center. But it just stacked upon my inner stacklayout.

You could simply use Xamarin Community Toolkit Popup to make it.
First of all, add a new Nuget named Xamarin.CommunityToolkit to your project.
Then, create a popup page which will be shown when you click a button. This is much similar to creating a ContentPage.
<?xml version="1.0" encoding="UTF-8" ?>
<xct:Popup
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ContentDemo.CommunityPopupPage"
xmlns:xct="clr- namespace:Xamarin.CommunityToolkit.UI.Views;assembly=Xamarin.CommunityToolkit">
<StackLayout WidthRequest="200" HeightRequest="200"BackgroundColor="Red"HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="hello"/>
</StackLayout>
</xct:Popup>
The .cs may be something like this:
using Xamarin.CommunityToolkit.UI.Views;
namespace ContentDemo
{
public partial class CommunityPopupPage : Popup
{
public CommunityPopupPage()
{
this.Size = new Size(300, 300); //you could change the size of the popup
InitializeComponent();
}
}
}
Finally, you may want to open this popup page via a button clicked event. Don't forget to add this line:
using Xamarin.CommunityToolkit.Extensions;
void Button_Clicked(System.Object sender, System.EventArgs e)
{
App.Current.MainPage.Navigation.ShowPopup(new CommunityPopupPage());
}
I created this demo and it worked well. You could refer to Xamarin Community Toolkit Popup and Getting Started with the Xamarin Community Toolkit for more information.
====================update =====
2.Using outer layout as a Grid instead of a StackLayout might be an alternative. Just something like this:
<Grid x:Name="outerGrid" VerticalOptions="Center" > //this grid includes the grid and the contentview to be shown
<Grid x:Name="innerGrid">
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
</Grid>
<ContentView>
... // design the content
<ContentView />
</Grid>
In this way, the ContentView will be placed above the innerGrid. But I prefer using a popup page.
Hope it works for you.

Related

Line break after back button - Xamarin Forms

I am new to Xamarin forms and have directed to a navigation page from my main page, I'd like the title to appear on the same line as the back button but it's currently appearing on the line below, like this -
https://i.imgur.com/lpsJjle.png
I am using the following code to display the title -
<NavigationPage.TitleView>
<StackLayout>
<Label
Text="About" TextColor="{StaticResource lightGreen}">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String"
Android="GothamRoundedMedium.ttf#GothamRoundedMedium"
iOS="GothamRoundedMedium">
</OnPlatform>
</Label.FontFamily>
</Label>
</StackLayout>
</NavigationPage.TitleView>
and this code to navigate to the about page -
Navigation.PushAsync(new NavigationPage(new Survey2()));
Any help would be much appreciated!
With new NavigationPage you are again creating a new NavigationPage.
So,just use:
Navigation.PushAsync(new Survey2());
If you need to use Modal,use this:
Navigation.PushModalAsync(new NavigationPage(new Survey2()));

Refresh UI after Dynamicly adding entry field to stacklayout on stipper value change

I have a stepper and i want to add new entry field when the value increment or delete entry on value decrement , i manged to do that but the UI won't refresh directly after the value of the stepper change (i need to click on other UI element first) then the fields will appear , and after clicking in other UI element the stepper will work properly (adding and deleting functionality) which i don't know why it's happening ?!! .
EDIT :
It's look like the problem is with having scroll view .. if i remove it the new fields will added or deleted directly .. but still don't know why .
The xaml page
<ScrollView>
<StackLayout x:Name="StackLayout">
<Label Text="عدد العناصر المراد اضافتها (اقصى عدد في المرة الواحدة 30)"></Label>
<Stepper Maximum="30"
Minimum="2"
Increment="1"
ValueChanged="Stepper_OnValueChanged"
Value="2"></Stepper>
<StackLayout x:Name="EntryStackLayout">
<Entry></Entry>
<Entry ></Entry>
</StackLayout>
</StackLayout>
</ScrollView>
Code Behind
public partial class AddNewListOfItemsPage : ContentPage
{
public AddNewListOfItemsPage ()
{
InitializeComponent();
}
private void Stepper_OnValueChanged(object sender, ValueChangedEventArgs e)
{
if (e.NewValue > e.OldValue)
{
EntryStackLayout.Children.Add(new Entry());
}
else
{
var childCount = EntryStackLayout.Children.Count;
EntryStackLayout.Children.RemoveAt(childCount - 1);
}
}
}
Ok i found that adding a stackLayout before the scrollView fix the problem
<StackLayout>
<ScrollView>
<StackLayout x:Name="StackLayout">
<Label Text="عدد العناصر المراد اضافتها (اقصى عدد في المرة الواحدة 30)"></Label>
<Stepper Maximum="30"
Minimum="2"
Increment="1"
ValueChanged="Stepper_OnValueChanged"
Value="2"></Stepper>
<StackLayout x:Name="EntryStackLayout">
<Entry></Entry>
<Entry ></Entry>
</StackLayout>
</StackLayout>
</ScrollView>
</StackLayout>
I had a similar problem. Size of elements deep inside ScrollView was not refreshed after layout change (on Android < 4.3).
Specifying height of ScrollView helped (specifying either one of the following for ScrollView in XAML helped):
VerticalOptions="FillAndExpand"
HeightRequest="100"

Is it possible to popUp a lottie animation View while an async Task is request

I have created a content view , and inside this content view I've insert a lottie animation :
I'm trying to make it a common animation view so I can use it to multiple async Task
LoadingAnimation.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:lottie="clr-namespace:Lottie.Forms;assembly=Lottie.Forms"
x:Class="Fit_Plans.Views.Common.LoadingAnimation">
<ContentView.Content>
<StackLayout>
<lottie:AnimationView
Grid.Column="0"
Margin="-20"
x:Name="LoadingAnimationView"
Animation="loading_animation.json"
AutoPlay="false"
HeightRequest="160"
WidthRequest="160"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"/>
</StackLayout>
</ContentView.Content>
</ContentView>
Now in my SomeView.Xaml.cs
async protected override void OnAppearing()
{
Product produits = new Product();
if(getListProduit ==null || getListProduit.Count<=0)
{
getListProduit = await produits.loadMenuAsync(api, siteUrl);
PopulateProductsLists(getListProduit);
}
base.OnAppearing();
}
You can notice that I have a await asynch task :
getListProduit = await produits.loadMenuAsync(api, siteUrl);
Is it possible to make my lottie animation pop-up the animation.jason , and when the task is done close the popup? Or , what would be the best way to do such thing?
If you want to show lottie animation in a popup, I encourage you to use the great Rg.Plugins.Popup library. You can get it from nuget and then create a popup page in which you should add your lottie animation (the same as you did above). This way you'll be able to reuse this popup animation page.
XAML of your popup page would look similar to this:
<pages:PopupPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
x:Class="MyProject.MyPopupPage">
<!--You can set an animation in the xaml file or in the csharp code behind-->
<pages:PopupPage.Animation>
<animations:ScaleAnimation
PositionIn="Center"
PositionOut="Center"
ScaleIn="1.2"
ScaleOut="0.8"
DurationIn="400"
DurationOut="300"
EasingIn="SinOut"
EasingOut="SinIn"
HasBackgroundAnimation="True"/>
</pages:PopupPage.Animation>
<!--You can use any elements here which are extended from Xamarin.Forms.View-->
<lottie:AnimationView
Grid.Column="0"
Margin="-20"
x:Name="LoadingAnimationView"
Animation="loading_animation.json"
AutoPlay="true"
HeightRequest="160"
WidthRequest="160"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"/>
</pages:PopupPage>
Here you can find the code to use your new popup page.

Xamarin.forms content page layout

I have content page in my xaml file. I have a Stacklayout with orientation vertical in it. I want to put a dialog in middle of content page above this stacklayout. Please suggest some way. Thanks in advance!
An easy way to provide dialogs that feel native to the platform your on is through using the UserDialogs plugin.
If you want to create something of your own you would probably have to come up with something like this:
<Grid>
<StackLayout>
...
</StackLayout>
<Grid IsVisible="{Binding ShowMessage}">
<Label Text="Message" HorizontalOptions="Center" VerticalOptions="Center" />
</Grid>
</Grid>
The nested Grid will act as an overlay of sorts spanning your entire page.

SilverLight 4 - How to have Buttons to switch between view in grid

I‘m trying to create a page with Silver Light 4, that is similar in functionality to the main page at the Silver Light Showcase website (http://www.silverlight.net/showcase/).
Essentially I want to have buttons that change the view of the data in a Grid. One view might have just an image, another might have a smaller image with a smattering of data, and the third would be all the details.
I wondering if anyone has a recommendation of how to achieve this?
I would place a Border control inside of the Grid. Then on button click change the border.Child to the new view. You could define each view in a seperate UserControl xaml file.
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="contentFrame" />
<Button x:Name="changeViewButton" click="click_event" Grid.Row="1" Height="22" Width="150" />
</Grid>
// code behind
protected void click_event(object s, EventArgs e)
{
View1 view1 = new View1();
// add some code to decide which view to show, possible hold onto the view in memory etc.
this.ContentFrame.Child = view1;
}