Add Content Programatically to TabViewItem in TabView Xamarin forms - xamarin.forms

I am using TabView in my project. I've installed Xamarin.community.toolkit.
I am trying to add Content for TabViewitem programmatically instead of xaml because each ContentView of TabViewItem has service calls in it's constructor which is causing the issue when all the TabViewItems load at once.
Below is my code:
<xcl:TabView x:Name="MainTabView"
TabStripPlacement="Top">
<xcl:TabViewItem
x:Name="Tab1"
Text="Details"
Margin="16,0,16,0"
TextColor="Black"
FontFamily="{StaticResource SFProRegular}"
TextColorSelected="Black"
FontSize="15">
</xcl:TabViewItem>
------
</xcl:TabView>
And I 'm trying to add Content to TabViewItem programatically like below (Page1 is a ContentView)
var stackPanel = new StackLayout { Orientation = StackOrientation.Vertical };
stackPanel.Children.Add(new Page1());
DetailsTabView.Content = stackPanel;
But the UI is not rendering in my app.
The same works If I add the Page1 like below:
<xcl:TabView x:Name="MainTabView"
TabStripPlacement="Top" >
<xcl:TabViewItem
x:Name="Tab1"
Text="Details"
Margin="16,0,16,0"
TextColor="Black"
FontFamily="{StaticResource SFProRegular}"
TextColorSelected="Black"
FontSize="15">
<template:Page1 />
</xcl:TabViewItem>
------
</xcl:TabView>
Any help is appreciated!

And I 'm trying to add Content to TabViewItem programatically like below (Page1 is a ContentView)
If you want to add one COntentview in one TabViewItem by code behind, you can take a look:
<xct:TabView
x:Name="mytabview"
TabIndicatorColor="Yellow"
TabStripBackgroundColor="Blue"
TabStripHeight="60"
TabStripPlacement="Bottom">
<xct:TabViewItem
x:Name="tb1"
FontSize="12"
Text="Tab 1"
TextColor="White"
TextColorSelected="Yellow">
<StackLayout />
</xct:TabViewItem>
<xct:TabViewItem
x:Name="tb2"
FontSize="12"
Text="Tab 2"
TextColor="White"
TextColorSelected="Yellow">
</xct:TabViewItem>
</xct:TabView>
Then adding ContentView to TabViewItem.
public MainPage()
{
InitializeComponent();
mytabview.TabItems[0].Content = new contentview1();
mytabview.TabItems[1].Content = new contentview2();
}
Note: please forget to add Stacklayout or other layout inside TabViewItem by xaml, then code you add contentview will be effective.

you need to do somethig like this
<xct:TabView TabItemsSource="{Binding TabVms}"
TabStripPlacement="Top"
TabStripBackgroundColor="LightGray"
TabIndicatorColor="Gray" IsTabStripVisible="True">
<xct:TabView.TabViewItemDataTemplate>
<DataTemplate>
<Grid>
// tabs
</Grid>
</DataTemplate>
</xct:TabView.TabViewItemDataTemplate>
<xct:TabView.TabContentDataTemplate>
<DataTemplate>
<Grid>
// content`enter code here`
</Grid>
</DataTemplate>
</xct:TabView.TabContentDataTemplate>
</xct:TabView>

Related

Is there a way to render ContentPages under the top ContentPage in the navigation stack?

I've created a Xamarin.Forms app and I'm using a NavigationPage to hold a stack of ContentPages. The navigation makes sense to me, but I'd like to somehow be able to render the ContentPages underneath the top ContentPage. Specifically, I'd like a transparent background on one that lets some of the page underneath be visible. I know that I could use the popup classes, but they just use the underlying dialog architecture which leaves a lot to be desired, especially on Android.
If this cannot be done natively with something like a simple switch that I've missed, is there any way to create my own NavigationPage class that implements the behavior I'm after? What about any tutorials that would walk me through this process?
As microsoft documents says
A Page object represents a ViewController in iOS and a Page in the
Universal Windows Platform. On Android, each page takes up the screen
like an Activity, but Xamarin.Forms pages are not Activity objects.
On the Android platform, the PageRenderer class instantiates a ViewGroup control.
If you want to display multiple pages, Carousel Page can do the job https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/carousel-page
To use popup, you can download Rg.Plugins.Popup.Here are my sample code.
popuppage:
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:pages="http://rotorgames.com"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
x:Class="MyFormsApp.Views.MyPage1">
<StackLayout VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand">
<Label Text="Hello World!" FontSize="Large"/>
</StackLayout>
</pages:PopupPage>
open popup over mainpage:
public MainPage()
{
InitializeComponent();
PopupNavigation.Instance.PushAsync(new MyPage1(), true);
}
result:
edit:
<AbsoluteLayout>
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<ContentView
BackgroundColor="Red" Padding="10,0" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All"
Opacity="0.5"
x:Name="myContentview">
<StackLayout HeightRequest="400" WidthRequest="400">
<Label Text="This is a popup" VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</ContentView>
</AbsoluteLayout>

Drag and Drop on Xamarin Forms

We want to implement drag and drop functionality in Xamarin forms
and in this user should be able to drag an emoji from 1 place, and put on another. We don’t want the source emoji to go away though and should be able to come to know on which employee, rating is given .
Image how it is before
Image how it is after
Question also posted on:
https://forums.xamarin.com/discussion/184104/drag-and-drop-on-xamarin-forms/p1?new=1
We have checked a few links like below, but none of them seem to work:
Xamarin Forms Drag Image between Views
https://blog.francois.raminosona.com/drag-and-drop-in-xamarin-forms/
Will appreciate if could get some tips on how to do it and even if this was possible on forms?
You could check the code below.
xaml:
<StackLayout Margin="10">
<StackLayout Margin="10" Orientation="Horizontal">
<components:DragAndDropSample3ReceivingView
BackgroundColor="Beige"
HeightRequest="80"
WidthRequest="80" />
<components:DragAndDropSample3ReceivingView
BackgroundColor="Beige"
HeightRequest="80"
WidthRequest="80" />
<components:DragAndDropSample3ReceivingView
BackgroundColor="Beige"
HeightRequest="80"
WidthRequest="80" />
</StackLayout>
<BoxView
BackgroundColor="Blue"
HeightRequest="5"
WidthRequest="3" />
<StackLayout Margin="10" Orientation="Horizontal">
<components:DragAndDropSample3MovingView
BackgroundColor="Red"
CornerRadius="40"
HeightRequest="40"
WidthRequest="40" />
<components:DragAndDropSample3MovingView
BackgroundColor="Green"
CornerRadius="40"
HeightRequest="40"
WidthRequest="40" />
</StackLayout>
</StackLayout>
Code Behind:
public void OnDropReceived(IDragAndDropMovingView view)
{
if (view is DragAndDropSample3MovingView sender)
{
var control = new DragAndDropSample3MovingView()
{
BackgroundColor=sender.BackgroundColor,
CornerRadius=sender.CornerRadius,
WidthRequest=sender.WidthRequest,
HeightRequest=sender.HeightRequest,
};
Content = control;
}
}
Screenshot:
You could check the source file from the code project for reference.
https://github.com/WendyZang/Test/tree/master/Drag_Drop_Controls/Xamarin-Developer-Sample-master

Binding Image to CarouselView

I have a database that I have created using SQLite. I'm using Xamarin.Forms. I would like to display a carousel of images using CarouselView.FormsPlugin and although I have looked at documentation I have had no luck in displaying it on my app.
I have the CarouselViewRenderer.Init() in the Android MainActivity.cs file. However, no images are displayed.
XAML:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:forms="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
x:Class="HostelRentalApp.HostelDetailsPage">
<ContentPage.Content>
<StackLayout>
<forms:CarouselViewControl x:Name="CRSLImages" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<forms:CarouselViewControl.ItemTemplate HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<DataTemplate>
<Image Source="{Binding .}"/>
</DataTemplate>
</forms:CarouselViewControl.ItemTemplate>
</forms:CarouselViewControl>
</StackLayout>
</ContentPage.Content>
</ContentPage>
CS:
public HostelDetailsPage (HostelInfo hostel)
{
var images = App.Database.GetHostelImageList(0);
this.CRSLImages.ItemsSource = images;
}
SQLite Query:
public List<HostelImages> GetHostelImageList(int id)
{
return database.Query<HostelImages>("SELECT Image FROM HostelImages WHERE Hostel_id = ?", id);
}
The Query is returning the correct rows. As shown, when using a tracepoint, the image filenames are retrieved. However, the images are not shown on the UI. What am I doing wrong? Thank you.
ItemsSource is a List<HostelImages> so your binding path should be
<Image Source="{Binding Image}"/>

Can I create some small views including created views to use it in mainPage?

Can I create a view including Entry Progress bar or others to make a view which I can use in other places?
for example I want to create a card view which I can use to list the lists.
I can only write in the list not to write the card in every list.
Yes, you can create a custom view and use it in the MainPage or other Pages, Views you want.
To create a custom view, add a new item --> ContentView,let's call it CardView:
In .cs of CardView:
public partial class CardView : ContentView
{
public CardView()
{
InitializeComponent();
}
}
In xaml of CardView, add your labels,progressbar, entry there:
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="App35.CardView">
<ContentView.Content>
<StackLayout>
<Label Text="Hello Xamarin.Forms!" />
<ProgressBar Progress="0.5" />
<Entry Placeholder="I'm entry"/>
</StackLayout>
</ContentView.Content>
</ContentView>
To use it in MainPage, in listView or in the page:
<StackLayout>
<projectName:CardView HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
<ListView x:Name="listView" RowHeight="70">
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>mono</x:String>
<x:String>monodroid</x:String>
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<projectName:CardView HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
I upload a sample here and you can check.
Yes you can do that using ContentView, if you want to use it as views
Or you can make ViewCells if you want to use it in ListViews. Its easy and almost every project does that.
You can refer this link :- https://xamarinhelp.com/xamarin-forms-user-control/
Let me know if you have more questions on this. Thanks!

XamarinForm DataTemplate ListView Binding

I want to bind to a variable from inside a CustomControl referenced in a ListView DataTemplate
This is the situation:
I have a custom control containing a ListView with many DataTemplates to display the various ViewCell (it's basically a set of dynamic fields that must be displayed according to their "type").
<?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:s4gvForms="clr-namespace:S4GVMobile.Forms;assembly=S4GVMobile"
xmlns:s4gvControls="clr-namespace:S4GVMobile.Controls;assembly=S4GVMobile"
x:Class="S4GVMobile.Controls.AttributesList"
x:Name="S4GVAttributesListControl"
>
<ContentView.Resources>
<ResourceDictionary>
<!-- MANY DATA TEMPLATES HERE, ONLY ONE LEFT FOR REFERENCE -->
<DataTemplate x:Key="s4gvAttributePictureTemplate">
<ViewCell x:Name="s4gvAttributePictureViewCell">
<ViewCell.View>
<StackLayout Orientation="Vertical">
<Label Text="{Binding Attribute.Key}" />
<s4gvControls:AttributePicture ParentID="{Binding Source={x:Reference s4gvAttributePictureViewCell}, Path=Parent.BindingContext.EntityID}" AttributeValue="{Binding .}" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
<!-- ... -->
<!-- AGAIN, LONG LIST IN THE SELECTOR -->
<s4gvForms:AttributeDataTemplateSelector x:Key="s4gvAttributeDataTemplateSelector"
AttributePictureTemplate="{StaticResource s4gvAttributePictureTemplate}"
/>
</ResourceDictionary>
</ContentView.Resources>
<StackLayout>
<ListView x:Name="s4gvAttributes" HasUnevenRows="True" ItemTemplate="{StaticResource s4gvAttributeDataTemplateSelector}" ItemsSource="{Binding AttributeValues}" />
</StackLayout>
I'm passing to this custom control both the list of items (required by the ListView) and an additional EntityID.
<s4gv:AttributesList x:Name="s4gvSerialAttributes" AttributeValues="{Binding Serial.AttributeValues}" EntityID="{Binding Serial.SerialID}" VerticalOptions="FillAndExpand"/>
The custom control has the required bindable properties (AttibuteValues and EntityID) and relies on a dedicated ViewModel
I need to retrieve the EntityID from inside the DataTemplate (and put it in the ParentID property); this value is in the custom control's ViewModel but it is "outside" the ListView's DataTemplates. I've tried using the Parent.BindingContext (it works on Command and CommandParameter) but it seems that it can only go up to the ListView's context and not higher though I need to move one further step up to the CustomControl itself.
<s4gvControls:AttributePicture ParentID="{Binding Source={x:Reference s4gvAttributePictureViewCell}, Path=Parent.BindingContext.EntityID}" AttributeValue="{Binding .}" />
Any idea? I'm happy to restructure the whole thing if required.

Resources