Send Text value from 2 Labels using Command inside BindableLayout - c#

I'm working with MVVM and I have a bindablelayout like this:
<StackLayout Margin="20" BindableLayout.ItemsSource="{Binding MyApartments}" >
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Margin="0,10,0,0">
<Frame Padding="15" HasShadow="True" BackgroundColor="WhiteSmoke" HorizontalOptions="Fill" VerticalOptions="CenterAndExpand" HeightRequest="150" CornerRadius="15">
<StackLayout x:Name="AptItems">
<Label x:Name="apartmentlabel" Text="{Binding MyApartments.apartment}" Margin="0,15,0,0" FontFamily="{StaticResource mon_extrabold}" TextColor="DarkBlue" FontSize="Title"/>
<Label x:Name="numberlabel" Text="{Binding MyApartments.number}" FontFamily="{StaticResource mon_bold}" TextColor="DarkBlue" FontSize="Subtitle"/>
<Button HorizontalOptions="End" FontFamily="{StaticResource mon_regular}" BackgroundColor="{StaticResource bounty}" TextColor="White" Command="{Binding Path=BindingContext.SelectApartment, Source={x:Reference ApartmentsPage}}"/>
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
I need to save the value in buildinglabel and numberlabel to my preferences but I don't know how to pass both labels to a command and searched for a long time now with no success, how is it done?

Related

Error:The unintended end of the file. The following elements are not closed: ContentPage., line 31, position 3

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:vm="clr-namespace:App1.ViewModel"
x:Class="App1.View.LoginPage" BackgroundColor="#f0f2f5">
<ContentPage.BindingContext>
<vm:LoginViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Image Source="XamarinLogo.png" ></Image>
<Frame BackgroundColor="White" CornerRadius="20" WidthRequest="396" HeightRequest="430" VerticalOptions="Center" HorizontalOptions="Center" >
<StackLayout>
<Label Text="Вход на Lockdown" TextColor="Black" HorizontalOptions="Center" FontSize="Title" Margin="0,30,0,10" />
<Entry Placeholder="Логин" x:Name="login" Text="{Binding Login}" />
<Entry Placeholder="Пароль" x:Name ="password" Text="{Binding Password}" IsPassword="True"/>
<Label x:Name="LoginMessageLabel" Text="{Binding LoginMessage,Mode=OneWay}" IsVisible="{Binding TurnLoginMessage,Mode=OneWay}" Margin="10,0,0,0" TextColor="Red"></Label>
<Button x:Name="loginButton" Text="Вход" TextColor="White" BorderRadius="6" BackgroundColor="#1877F2" Margin="10,10,10,10"
Command="{Binding cmdLogin}">
</Button>
<Button x:Name="createAccount" Text="Создать новый аккаунт" TextColor="White" BorderRadius="6" BackgroundColor="#42B72A" Margin="10,10,10,10" Command="{Binding cmdCreateAccount}"/>
<Label Text="Забыли пароль?" TextColor="#1877F2" HorizontalOptions="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding cmdForgotPassword}"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
</Frame>
</StackLayout></ContentPage.Content>
what is the problem?This is my first time working with xamarin, I don't understand what the error is.What is not closed if I closed it?
you need to close every control you create. you forget to close ContentPage
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:vm="clr-namespace:App1.ViewModel"
x:Class="App1.View.LoginPage" BackgroundColor="#f0f2f5">
<ContentPage.BindingContext>
<vm:LoginViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Image Source="XamarinLogo.png" ></Image>
<Frame BackgroundColor="White" CornerRadius="20" WidthRequest="396" HeightRequest="430" VerticalOptions="Center" HorizontalOptions="Center" >
<StackLayout>
<Label Text="Вход на Lockdown" TextColor="Black" HorizontalOptions="Center" FontSize="Title" Margin="0,30,0,10" />
<Entry Placeholder="Логин" x:Name="login" Text="{Binding Login}" />
<Entry Placeholder="Пароль" x:Name ="password" Text="{Binding Password}" IsPassword="True"/>
<Label x:Name="LoginMessageLabel" Text="{Binding LoginMessage,Mode=OneWay}" IsVisible="{Binding TurnLoginMessage,Mode=OneWay}" Margin="10,0,0,0" TextColor="Red"></Label>
<Button x:Name="loginButton" Text="Вход" TextColor="White" BorderRadius="6" BackgroundColor="#1877F2" Margin="10,10,10,10"
Command="{Binding cmdLogin}">
</Button>
<Button x:Name="createAccount" Text="Создать новый аккаунт" TextColor="White" BorderRadius="6" BackgroundColor="#42B72A" Margin="10,10,10,10" Command="{Binding cmdCreateAccount}"/>
<Label Text="Забыли пароль?" TextColor="#1877F2" HorizontalOptions="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding cmdForgotPassword}"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
</Frame>
</StackLayout></ContentPage.Content>
</ContentPage>

Xamarin.Forms Image not showing from Source

I'm using fake Api for testing app.The Api is on this link: http://jsonplaceholder.typicode.com/. I connect my App, get all photos and add to CollectionView. Everything work perfectly fine but images are not showing.Here is my code:
<Grid>
<StackLayout>
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<RefreshView IsRefreshing="{Binding IsRefreshing, Mode=OneWay}"
Command="{Binding LoadRefreshCommand}">
<CollectionView ItemsSource="{Binding Photos}" SelectedItem="{Binding SelectedPhoto}" >
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="0,0,5,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference CurrentPage}, Path=BindingContext.LoadPhotosCommand}" />
</Grid.GestureRecognizers>
<Image Source="{Binding Url}" Grid.Column="0"></Image>
<Frame HasShadow="False" VerticalOptions="Center" Grid.Column="1">
<Label Text="{Binding Title}"></Label>
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
</StackLayout>
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="Center">
<Frame IsVisible="{Binding IsBusy}" BorderColor="#07987f" HasShadow="False" BackgroundColor="#eeeeee">
<StackLayout>
<ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" HorizontalOptions="Center" VerticalOptions="Center"></ActivityIndicator>
<Label TextColor="#07987f" Text="Loading Data, Please Wait..." HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="Center" VerticalOptions="Center" IsVisible="{Binding IsBusy}"/>
</StackLayout>
</Frame>
</StackLayout>
</Grid>
Here is the image i get from debug app:
What can be the problem or all images are blank.
Also i get this in the console:
[0:] HTTP Request: Could not retrieve https://via.placeholder.com/600/f66b97, status code Gone
[0:] ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri: https://via.placeholder.com/600/f66b97
Anyone had the same problem?
I don't know why source makes a reading problem. This is how i fix it:
Just change the following options and it should work with https too.
Android Options => Advanced
HttpClient implementation = Set to "Managed" option value
SSL/TLS implementation = set to "Native TLS 1.2+" option value

Label Is Never Showing

I am attempting to add a label above my button but the label never shows. This is the code xaml that I am using. What is it that keeps the label from being displayed?
<Frame HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10">
<Label x:Name="registererror" Text="Error, please verify all fields have valid input" TextColor="Red" />
</StackLayout>
<Button Command="{Binding SubmitCommand}" Text="Register" TextColor="White"
FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"
BackgroundColor="#088da5" Clicked="OnRegisterTap" />
</Frame>
a Frame can only have a single child. To contain multiple children you must use a layout container. Your Button is not enclosed in the StackLayout.
<Frame HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10">
<Label x:Name="registererror" Text="Error, please verify all fields have valid input" TextColor="Red" />
<Button Command="{Binding SubmitCommand}" Text="Register" TextColor="White"
FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"
BackgroundColor="#088da5" Clicked="OnRegisterTap" />
</StackLayout>
</Frame>

ListView Tapped removes button background color on data template Xamarin.Forms

I don't know why, but when I tap any ListView item, the Button inside it's template loses the background color.
I use default ViewCell inside ListView. Is this a Xamarin.Forms bug?
I have this problem only on iOS.
<ListView x:Name="lv" Grid.Row="2" HasUnevenRows="true" BackgroundColor="White" ItemTapped="OnItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="16" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="{Binding .,Converter={StaticResource IndexColorConverter}">
<StackLayout VerticalOptions="Center">
<Label Text="{Binding Auditorium}" FontSize="18" TextColor="Black"/>
<ContentView Padding="0,-5,0,0">
<Label Text="{Binding Title}" FontSize="16" TextColor="Black"/>
</ContentView>
<StackLayout Orientation="Horizontal">
<Button WidthRequest="100" Text="{Binding ButtonOverviewText}" TextColor="White" BackgroundColor="{Binding ButtonColor}" CommandParameter="{Binding .}" Clicked="Handle_Clicked"/>
<Button WidthRequest="100" Text="{Binding ButtonOverviewText}" TextColor="White" BackgroundColor="{Binding ButtonColor}" CommandParameter="{Binding .}" Clicked="Handle_Clicked" />
</StackLayout>
</StackLayout>
<Label Text="{Binding Coordinator}" FontSize="14" TextColor="Gray"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
There was an old issue that I filed fora similar bug: https://bugzilla.xamarin.com/show_bug.cgi?id=27502.
However, the specific scenario you mention is expected behavior on iOS and happens in native (non-Xamarin.Forms) projects too. See the comment here. You might consider disabling the UITableViewCellSelectionStyle of your cells with UITableViewCellSelectionStyle.None. Here is an example of a cell renderer. You can set the cell.SelectionStyle here. This would stop the gray highlight of the cell. If you don't want that it could be a workaround to keep your button colors.
My workaround for this problem is the following.
<Grid>
<BoxView BackgroundColor="{Binding ButtonColor}"/>
<Button Text="{Binding ButtonOverviewText}" TextColor="White" BackgroundColor="Transparent" CommandParameter="{Binding .}" Clicked="Handle_Clicked"/>
</Grid>

How to get children of the ListView in xamarin.forms?

<ListView x:Name="ListNewsLetter" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" SeparatorVisibility="Default" VerticalOptions="Fill" ItemTapped="ListNewsLetter_ItemTapped" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout HorizontalOptions="Fill" VerticalOptions="Center" Padding="5">
<StackLayout Padding="1" BackgroundColor="#f15a23" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout x:Name="stkNewsLetter" VerticalOptions="Center" BackgroundColor="#f15a23" Orientation="Horizontal" HeightRequest="30" Padding="6,0,6,0">
<Label x:Name="lblName" Text="{Binding NewsHeadline}" HorizontalOptions="StartAndExpand" TextColor="White" VerticalTextAlignment="Center" FontSize="Small"/>
<Image x:Name="imgPlus_Minus" HeightRequest="20" WidthRequest="20" Source="ico_down_arrow2_right.png" HorizontalOptions="End" ClassId="{Binding TapId}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="img_Tapped" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
</StackLayout>
<StackLayout x:Name="stkNewsLetterDetail" Orientation="Vertical" BackgroundColor="#ffffff" IsVisible="true" HorizontalOptions="FillAndExpand" Padding="6,0,6,0">
<Label x:Name="lblDate" Text="{Binding NewsDate}" HorizontalOptions="StartAndExpand" VerticalTextAlignment="Start" TextColor="#585858" FontSize="Small"/>
<Label x:Name="lblDetail" Text="{Binding NewsDetails}" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Start" TextColor="#585858" FontSize="Small"/>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
As you see in code ListView having main two child named "stkNewsLetter" and "stkNewLetterDetail". First Stack having Image and I want to change visibility of second stack on Image tap event.
In windows phone It will possible to get UI elements of List view but In xamarin.forms how it is possible?
You should bind to the Visability property on the stackLayout in the same way you are binding the Text. If the stackLayout doesn't have a visibility property you may need to wrap it within another control that does.
The click should change a visibility property in you viewmodel which will get picked up by the binding mentioned above.
For those who came here to get the child elements through Name like me,here is the solution:
var selectedItem = //any item selected within listview through OnItemTapped
StackLayout child1 = selectedItem.FindByName<StackLayout>("stkNewsLetter");

Categories