Image not getting the Class ID - Xamarin Forms - c#

I have several images in my XAML as follows (for this example I included two images).
<StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,5,0,5">
<Image x:Name="Image1" ClassId="{Binding Image1}" Source="tickoff.png" HeightRequest="18">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="SelectedImage"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Label Text="Asian Foods" FontSize="16" HorizontalOptions="Center" Margin="5,0,0,0"
TextColor="{StaticResource greyishBrown}"/>
</StackLayout>
StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,5,0,5">
<Image x:Name="Image2" ClassId="{Binding Image2}" Source="tickoff.png" HeightRequest="18">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="SelectedImage"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Label Text="Japanese Foods" FontSize="16" HorizontalOptions="Center" Margin="5,0,0,0"
TextColor="{StaticResource greyishBrown}"/>
</StackLayout>
I want to check what image has been clicked by the user. Fort this I'm using ClassId. I have included ClassIds in all images. This has a single method in code behind and I'm accessing classId from there.
But I'm not getting the Id when user clicks an image. I'm new to C# and Xamarin Forms. Could someone help me.
public void SelectedImage(object sender, EventArgs eventArgs)
{
Image image = (Image)sender;
string imageString = image.ClassId;
DisplayAlert("Test", imageSender.ClassId,"OK");
//TODO
}

XAML
<StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,5,0,5">
<Image x:Name="Image1" ClassId="Image1" Source="tickoff.png" HeightRequest="18">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="SelectedImage"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Label Text="Asian Foods" FontSize="16" HorizontalOptions="Center" Margin="5,0,0,0" TextColor="{StaticResource greyishBrown}" />
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,5,0,5">
<Image x:Name="Image2" ClassId="Image2" Source="tickoff.png" HeightRequest="18">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="SelectedImage"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Label Text="Japanese Foods" FontSize="16" HorizontalOptions="Center" Margin="5,0,0,0" TextColor="{StaticResource greyishBrown}"
/>
</StackLayout>
C#
public void SelectedImage(object sender, EventArgs eventArgs)
{
Image image = (Image)sender;
string imageString = image.ClassId;
DisplayAlert("Test", imageString ,"OK");
//TODO
}

Related

How to set SelectedColour Property for TabView using TabViewItemDataTemplate?

I am working on TabView control to create multiple tab page using "TabItemSource". In most of the sample provided in open source, the "TextColorSelected" property can be set in xct:TabViewItem. For my case, how can I set "TextColorSelected" property while using TabItemSource?
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.UI.Views;assembly=Xamarin.CommunityToolkit"
x:Class="MultiPosApp.Restaurant.Views.KitchenOrderTicketView"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Name="KitchenOrderTicketVM"
Title="Kitchen Order Ticket">
<xct:TabView TabItemsSource="{Binding KitchenOrderCollection}" TabStripPlacement="Top" TabStripBackgroundColor="AliceBlue" TabStripHeight="50" IsSwipeEnabled="False" TabIndicatorColor="Black"
AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" SelectedIndex="{Binding SelectedIdx}">
<xct:TabView.TabViewItemDataTemplate>
<DataTemplate>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
<Label Text="{Binding Location}"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
TextColor="Black"
FontAttributes="Bold"/>
</StackLayout>
</DataTemplate>
</xct:TabView.TabViewItemDataTemplate>
<xct:TabView.TabContentDataTemplate>
<DataTemplate>
<ScrollView>
<StackLayout BindableLayout.ItemsSource="{Binding TableOrderCollection}" Orientation="Vertical" Margin="3">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Frame BorderColor="LightGray" Padding="10" BackgroundColor="GhostWhite" HasShadow="True" CornerRadius="10">
<StackLayout Orientation="Vertical">
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Label Text="TABLE " TextColor="Black" FontSize="Large" FontAttributes="Bold"/>
<Label Text="{Binding Table.TableNum}" TextColor="Black" FontSize="Large" FontAttributes="Bold"/>
</StackLayout>
<StackLayout BindableLayout.ItemsSource="{Binding MenuOrderCollection}" Orientation="Vertical">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<Label Text="{Binding ItemName}" TextColor="Black" FontSize="Medium" VerticalOptions="Center"/>
<Label Text="{Binding ItemQuantity}" TextColor="Black" FontSize="Medium" HorizontalOptions="EndAndExpand" VerticalOptions="Center"/>
<CheckBox IsChecked="{Binding MenuStatus}" IsEnabled="{Binding CheckboxEnabled}" HorizontalOptions="End">
<CheckBox.Behaviors>
<prism:EventToCommandBehavior EventName="CheckedChanged" Command="{Binding Path=BindingContext.ChkboxChangedCommand, Source={x:Reference Name=KitchenOrderTicketVM}}" CommandParameter="{Binding}"/>
</CheckBox.Behaviors>
</CheckBox>
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</Frame>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ScrollView>
</DataTemplate>
</xct:TabView.TabContentDataTemplate>
</xct:TabView>
I didn't find a way to set the TextColorSelected property in TabViewItemDataTemplate.
For scenarios where you are not sure of the number of TabViewItems, you could try drawing the layout in code behind.
Something like:
public Page10()
{
InitializeComponent();
....
foreach (var item in tabview.TabItemsSource) //here the tabitemsource is a array of strings for test.
{
var tabViewItem = new TabViewItem
{
Text = (string)item,
TextColor = Color.White,
TextColorSelected = Color.Red,
};
var label = new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Text = (string)item
};
tabViewItem.Content = label;
tabview.TabItems.Add(tabViewItem);
}
}
define a x:Name for TabView:
<xct:TabView x:Name="tabview" TabItemsSource="{Binding KitchenOrderCollection}" TabStripPlacement="Top" TabStripBackgroundColor="AliceBlue" TabStripHeight="50" IsSwipeEnabled="False" TabIndicatorColor="Black"
AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" SelectedIndex="{Binding SelectedIdx}">
...
</xct:TabView>

Signup and Login Check with Application.Current.Properties in Xamarin Forms

I have a registration page. First the user enters his informations like email, password, weight, height...
After the user has filled out this form I want to store information most important ones email and password.
Then I want to use this data in the LoginPage( named in MainPage in my case).
So I need to store and then check this info in loginpage what user enters true or false
I am adding my registration page and others code. If you have any idea or suggestions I am open for them.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HealNow.Signup"
NavigationPage.HasNavigationBar="False">
<ContentPage.Resources>
<ResourceDictionary>
<LinearGradientBrush x:Key="Theme" EndPoint="0,1">
<GradientStop Color="#48b6a6" Offset="0.1" />
<GradientStop Color="#2b78d4" Offset="1.0" />
</LinearGradientBrush>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0">
</OnPlatform>
</ContentPage.Padding>
<StackLayout Background="{StaticResource Theme}">
<StackLayout Background="{StaticResource Theme}" >
</StackLayout>
<StackLayout Margin="0,35,0,0">
<Image HorizontalOptions="Center" HeightRequest="60" WidthRequest="60" Source="heartbeatt.png" ></Image>
</StackLayout>
<StackLayout Padding="0" Margin="10,20,10,0" HorizontalOptions="FillAndExpand" >
<Frame BackgroundColor="Transparent" HeightRequest="600" Padding="0" Margin="0">
<StackLayout>
<StackLayout Padding="0" Margin="15,10">
<Frame BackgroundColor="Transparent" BorderColor="White" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
<StackLayout Orientation="Horizontal">
<Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
<Image Source="user.png" Aspect="AspectFill" Margin="0"/>
</Frame>
<Entry x:Name ="email" Placeholder="Email" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0" Completed="OnChange"/>
</StackLayout>
</Frame>
<Frame BackgroundColor="Transparent" BorderColor="White" Margin="0,12,0,0" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
<StackLayout Orientation="Horizontal">
<Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
<Image Source="broken.png" Aspect="AspectFill" Margin="0"/>
</Frame>
<Entry x:Name ="password" Placeholder="Password" IsPassword="True" TextColor="White" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0" Completed="OnChange"/>
</StackLayout>
</Frame>
<Frame BackgroundColor="Transparent" BorderColor="White" Margin="0,12,0,0" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
<StackLayout Orientation="Horizontal">
<Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
<Image Source="babyy.png" Aspect="AspectFill" Margin="0"/>
</Frame>
<Entry Placeholder="Date of Birth" TextColor="White" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
</StackLayout>
</Frame>
<Frame BackgroundColor="Transparent" Margin="0,12,0,0" BorderColor="White" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
<StackLayout Orientation="Horizontal">
<Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
<Image Source="barrr.png" Aspect="AspectFill" Margin="0"/>
</Frame>
<Entry Placeholder="Weight" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
</StackLayout>
</Frame>
<Frame BackgroundColor="Transparent" Margin="0,12,0,0" BorderColor="White" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
<StackLayout Orientation="Horizontal">
<Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
<Image Source="height.png" Aspect="AspectFill" Margin="0"/>
</Frame>
<Entry Placeholder="Height" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
</StackLayout>
</Frame>
<Button Margin="30" Text="SIGN UP" BackgroundColor="#2a52be" TextColor="White" CornerRadius="30" Clicked="Button_Clicked" />
</StackLayout>
</StackLayout>
</Frame>
</StackLayout>
</StackLayout>
</ContentPage>
and it looks like this
Signup.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace HealNow
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Signup : ContentPage
{
public Signup()
{
InitializeComponent();
Application.Current.Properties[email.Text] = password.Text;
if (Application.Current.Properties.ContainsKey("Email"))
{
var email = Application.Current.Properties["Email"] as string;
}
if (Application.Current.Properties.ContainsKey("Password"))
{
var password = Application.Current.Properties["Password"] as string;
}
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new MainPage());
}
private void OnChange(object sender, EventArgs e)
{
Application.Current.Properties["Email"] = email.Text;
Application.Current.Properties["Password"] = password.Text;
Application.Current.SavePropertiesAsync();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
}
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HealNow.MainPage"
NavigationPage.HasNavigationBar="False">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0">
</OnPlatform>
</ContentPage.Padding>
<StackLayout>
<StackLayout.Background>
<LinearGradientBrush x:Name="Theme" EndPoint="0,1">
<GradientStop Color="#48b6a6" Offset="0.1" />
<GradientStop Color="#2b78d4" Offset="1.0" />
</LinearGradientBrush>
</StackLayout.Background>
<StackLayout Margin="0,35,0,0">
<Image HorizontalOptions="Center" HeightRequest="60" WidthRequest="60" Source="heartbeatt.png" ></Image>
</StackLayout>
<StackLayout Padding="0" Margin="10,20,10,0" HorizontalOptions="FillAndExpand" >
<Frame BackgroundColor="Transparent" HeightRequest="600" Padding="0" Margin="0">
<StackLayout>
<StackLayout Padding="0" Margin="15,10">
<Frame BackgroundColor="Transparent" BorderColor="White" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
<StackLayout Orientation="Horizontal">
<Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
<Image Source="user.png" Aspect="AspectFill" Margin="0"/>
</Frame>
<Entry x:Name="email2" Placeholder="Email" TextColor="#666666" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
</StackLayout>
</Frame>
<Frame BackgroundColor="Transparent" BorderColor="White" Margin="0,15,0,0" Padding="0" HorizontalOptions="FillAndExpand" CornerRadius="30">
<StackLayout Orientation="Horizontal">
<Frame BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" CornerRadius="30" Padding="0" Margin="5">
<Image Source="broken.png" Aspect="AspectFill" Margin="0"/>
</Frame>
<Entry x:Name="password2" Placeholder="Password" IsPassword="True" TextColor="White" FontAttributes="None" HorizontalOptions="FillAndExpand" Margin="0,0,20,0"/>
</StackLayout>
</Frame>
<StackLayout Orientation="Horizontal">
<CheckBox IsChecked="False" Color="White" />
<Label Text="Remember me" TextColor="White" FontSize="10" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
<Label Text="Forgot Password" TextColor="White" HorizontalOptions="EndAndExpand" FontSize="10" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
</StackLayout>
<Button Margin="10" Text="SIGN IN" BackgroundColor="#2b78d4" TextColor="White" CornerRadius="30" Clicked="Button_Clicked" />
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Label Text="Still Not Connected ?" TextColor="White" FontSize="10"/>
<Button Margin="0,-17,0,0" Text="Sign Up" TextColor="White" FontAttributes="Bold" FontSize="10" BackgroundColor="Transparent" Clicked="Button_Clicked2" />
</StackLayout>
<StackLayout Margin="0,25,0,0" Padding="0">
<Grid>
<BoxView BackgroundColor="White" HeightRequest="1" WidthRequest="150" HorizontalOptions="Center" VerticalOptions="Center"/>
<Frame BackgroundColor="White" HeightRequest="45" WidthRequest="45" CornerRadius="45" HasShadow="False" BorderColor="White" Margin="0" HorizontalOptions="Center" Padding="0">
<Label Text="OR" TextColor="#666666" FontSize="Small" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
</Frame>
</Grid>
</StackLayout>
<StackLayout Margin="0,25" Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Frame BackgroundColor="Transparent" HeightRequest="45" WidthRequest="45" CornerRadius="45" HasShadow="False" BorderColor="White" Margin="0" HorizontalOptions="Center" Padding="0">
<Image Source="https://www.pngitem.com/pimgs/m/44-440455_transparent-background-fb-logo-hd-png-download.png" Aspect="AspectFill" Margin="0"/>
</Frame>
<Frame BackgroundColor="Transparent" HeightRequest="45" WidthRequest="45" CornerRadius="45" HasShadow="False" BorderColor="White" Margin="0" HorizontalOptions="Center" Padding="0">
<Image Source="https://blog.hubspot.com/hubfs/image8-2.jpg" Aspect="AspectFill" Margin="0"/>
</Frame>
</StackLayout>
</StackLayout>
</StackLayout>
</Frame >
</StackLayout>
</StackLayout >
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace HealNow
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new WelcomePage());
}
private async void Button_Clicked2(object sender, EventArgs e)
{
await Navigation.PushAsync(new Signup());
}
}
}
string email;
string password;
public MainPage()
{
InitializeComponent();
if (Application.Current.Properties.ContainsKey("Email"))
{
email = Application.Current.Properties["Email"] as string;
}
if (Application.Current.Properties.ContainsKey("Password"))
{
password = Application.Current.Properties["Password"] as string;
}
}

Cannot display : display alert

I am trying to display , display alert when I click on "OnLogOut" but is it not woring and I cannot see why
<StackLayout Grid.Row="1" BackgroundColor="White" HorizontalOptions="FillAndExpand" Orientation="Vertical" VerticalOptions="FillAndExpand" Spacing="30" Padding="0,0,0,0">
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<!--Le text est dans la classe-->
<Label x:Name="myemail" Text="" TextColor="#36688D" Font="Bold,Italic,20" />
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<Label HorizontalOptions="Center"
TextColor="#36688D"
FontSize="15"
Text="Log Out" x:Name="mylogout">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="OnLogOut" />
</Label.GestureRecognizers>
</Label>
</StackLayout>
</StackLayout>
Here is my c# code:
public async void OnLogOut(object sender, EventArgs args)
{
Console.WriteLine("Log out is tapped");
await DisplayAlert("Email", "Your email please", "✔");
}
thanks in advance

Debugging: System.DivideByZeroException: Attempted to to divide by zero [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I can't figure out why I am getting this divide by zero exception. I'm very new to programming in general and can't figure out what I'm doing wrong. Sorry for the long code, I think I've removed about as much as possible and have it still give the exception.
Here is the C# code...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace CalculationApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BreakEvenCalculation();
}
private void BreakEvenCalculation()
{
var purchaseprice = Decimal.Parse(PurchasePriceEntry.Text);
var purchaseweight = Decimal.Parse(PurchaseWeightEntry.Text);
var purchasefreight = Decimal.Parse(PurchaseFreightEntry.Text);
var medicineimplants = Decimal.Parse(MedicineImplantsEntry.Text);
var costofgain = Decimal.Parse(CostOfGainEntry.Text);
var poundstillsell = Decimal.Parse(PoundsTillSellEntry.Text);
var deathloss = Decimal.Parse(DeathLossEntry.Text);
var saleweight = Decimal.Parse(SaleWeightEntry.Text);
var salefreight = Decimal.Parse(SaleFreightEntry.Text);
var breakeven = Decimal.Parse(BreakEvenEntry.Text);
var purchasedollars = purchaseprice * purchaseweight;
var feedcost = costofgain * poundstillsell;
poundstillsell = saleweight - purchaseweight;
var costs = (purchasedollars + purchasefreight + medicineimplants + feedcost + deathloss + salefreight);
breakeven = costs / saleweight; //throws exception.... System.DivideByZeroException: Attempted to divide by zero
//breakeven = costs / (saleweight + 1); //correctly equals 1.111302549965541
//breakeven = costs * saleweight; //correctly equals 2338125.00
//breakeven = saleweight; //correctly equals 1450.00
//breakeven = costs; //correctly equals 1612.50
////////////breakeven should equal 1.112068965517241
BreakEvenEntry.Text = breakeven.ToString();
}
private void PurchasePriceEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void PurchaseWeightEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void PurchaseFreightEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void MedicineImplantsEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void CostOfGainEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void PoundsTillSellEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void DeathLossEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void SalePriceEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void SaleWeightEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
private void SaleFreightEntry_Completed(object sender, TextChangedEventArgs e)
{
BreakEvenCalculation();
}
}
And here is the xaml code...
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="CalculationApp.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CalculationApp"
xmlns:customentry="clr-namespace:CustomEntry">
<StackLayout Padding="0" BackgroundColor="AliceBlue" Orientation="Vertical">
<Label Text="CalculationApp" TextColor="Black" FontAttributes="Italic" FontSize="Large" TextDecorations="Underline" Scale="1.5"
Margin="0,15,0,10" HorizontalOptions="CenterAndExpand" VerticalTextAlignment="Center" VerticalOptions="CenterAndExpand"/>
<Grid ColumnSpacing="0" RowSpacing="5">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width=".6*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="Purchase Price" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="0" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap"/>
<Label Text="Purchase Weight" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="1" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Label Text="Purchase Freight" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="2" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Label Text="Medicine/Implants" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="3" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Label Text="Cost of Gain" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="4" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Label Text="Pounds Till Sell" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="5" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Label Text="Death Loss" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="6" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Label Text="Sale Price" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="7" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Label Text="Sale Weight" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="8" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Label Text="Sale Freight" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2"
Grid.Column="0" Grid.Row="9" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<Entry x:Name="PurchasePriceEntry" Text="{Binding Source={x:Reference PurchasePriceStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="0" HorizontalTextAlignment="Center" Keyboard="Numeric" ReturnType="Next" VerticalOptions="End" MaxLength="5"
TextChanged="PurchasePriceEntry_Completed"/>
<Entry x:Name="PurchaseWeightEntry" Text="{Binding Source={x:Reference PurchaseWeightStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="1" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center"
TextChanged="PurchaseWeightEntry_Completed" />
<Entry x:Name="PurchaseFreightEntry" Text="{Binding Source={x:Reference PurchaseFreightStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="2" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center"
TextChanged="PurchaseFreightEntry_Completed" />
<Entry x:Name="MedicineImplantsEntry" Text="{Binding Source={x:Reference MedicineImpantsStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="3" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center"
TextChanged="MedicineImplantsEntry_Completed" />
<Entry x:Name="CostOfGainEntry" Text="{Binding Source={x:Reference CostofGainStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="4" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center"
TextChanged="CostOfGainEntry_Completed" />
<Entry x:Name="PoundsTillSellEntry" Text="1000" TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="5" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center" IsEnabled="False" />
<Entry x:Name="DeathLossEntry" Text="{Binding Source={x:Reference DeathLossStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="6" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center"
TextChanged="DeathLossEntry_Completed" />
<Entry x:Name="SalePriceEntry" Text="{Binding Source={x:Reference SalePriceStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="7" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center"
TextChanged="SalePriceEntry_Completed" />
<Entry x:Name="SaleWeightEntry" Text="{Binding Source={x:Reference SaleWeightStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="8" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center"
TextChanged="SaleWeightEntry_Completed" />
<Entry x:Name="SaleFreightEntry" Text="{Binding Source={x:Reference SaleFreightStepper}, Path=Value}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="3"
Grid.Column="1" Grid.Row="9" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="Center"
TextChanged="SaleFreightEntry_Completed" />
<Stepper x:Name="PurchasePriceStepper" Grid.Column="2" Grid.Row="0" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="5" Minimum=".01" Increment=".01" Value="1.75" />
<Stepper x:Name="PurchaseWeightStepper" Grid.Column="2" Grid.Row="1" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="2000" Minimum="1" Increment="1" Value="550"/>
<Stepper x:Name="PurchaseFreightStepper" Grid.Column="2" Grid.Row="2" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="100" Minimum="0" Increment=".25" Value="10"/>
<Stepper x:Name="MedicineImpantsStepper" Grid.Column="2" Grid.Row="3" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="500" Minimum="0" Increment=".25" Value="25.00"/>
<Stepper x:Name="CostofGainStepper" Grid.Column="2" Grid.Row="4" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="3" Minimum="0" Increment=".01" Value="0.65"/>
<Stepper x:Name="DeathLossStepper" Grid.Column="2" Grid.Row="6" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="500" Minimum="0" Increment="1" Value="20"/>
<Stepper x:Name="SalePriceStepper" Grid.Column="2" Grid.Row="7" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="5" Minimum="0" Increment=".01" Value="1.25"/>
<Stepper x:Name="SaleWeightStepper" Grid.Column="2" Grid.Row="8" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="5000" Minimum="0" Increment="5" Value="1450.00"/>
<Stepper x:Name="SaleFreightStepper" Grid.Column="2" Grid.Row="9" HorizontalOptions="Center" VerticalOptions="Center" Scale=".75" TranslationX="3" Maximum="100" Minimum="0" Increment=".25" Value="10"/>
</Grid>
<Grid Padding="10" ColumnSpacing="5" RowSpacing="10" VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Text="Break Even" x:Name="BreakEvenLabel" TextColor="Black" FontSize="Medium" FontAttributes="Bold" Grid.Column="0" Grid.Row="0" HorizontalTextAlignment="Center" VerticalOptions="Center" LineBreakMode="NoWrap" />
<Label Text="Profit" TextColor="Black" FontSize="Medium" FontAttributes="Bold" Grid.Column="1" Grid.Row="0" HorizontalTextAlignment="Center" VerticalOptions="Center" LineBreakMode="NoWrap" />
<Entry x:Name="BreakEvenEntry" Text="125" FontSize="Large" TextColor="Black" Grid.Column="0" Grid.Row="1" FontAttributes="Bold" BackgroundColor="Ivory" IsEnabled="False" HorizontalTextAlignment="Center" VerticalOptions="Center" />
<Entry x:Name="ProfitEntry" TextColor="Black" FontSize="Large" Grid.Column="1" Grid.Row="1" FontAttributes="Bold" BackgroundColor="Ivory" IsEnabled="False" HorizontalTextAlignment="Center" VerticalOptions="Center" />
</Grid>
</StackLayout>
</ContentPage>
The exception happens on this line in C#...
breakeven = costs / saleweight;
As you can see, I've tried changing the code several different ways to try to figure out whats going on. In my mind, to get the divide by zero exception, the saleweight has to be zero but if I change the code to simply read...
breakeven = saleweight;
The Xamarin.Forms BreakEvenEntry gives me the correct sale weight...so it isn't zero right??
What am I missing? What am I doing wrong??
Thanks for the help in advance!!
'Cost' is being created as a float or double and needs to be explicitly defined as decimal to match your parsed decimal variables.
decimal costs = (purchasedollars + [...]
Here is a link on integer vs float division for C#:
"If one of the operands is decimal, another operand can be neither float nor double, because neither float nor double is implicitly convertible to decimal. You must explicitly convert the float or double operand to the decimal type."
(Conversely to the above suggestion, you may consider parsing your variables as doubles or floats. Same result: float division instead of integer division.)
I attempted to reproduce your error, but was unable. I, however, was reminded of a time in Python 2.7 where I found a similar error.

Changing Opacity of multiple images Xamarin.Forms

I got these five images and I want to change the opacity of the selected Icon to 1 and then set the rest to 0.25.
I got this far, but got no idea how to change the opacity on all of them. All the guides say to x:reference every single image, but as I got multiple that would get weird quickly.
public async void OnSeverityTapped(object sender, EventArgs args)
{
String[] buttons = new String[SeverityValues.Count];
for (int n = 0; n < SeverityValues.Count; ++n)
{
buttons[n] = SeverityValues[n].Name;
}
var res = await this.DisplayActionSheet("Pick Severity", "Cancel", "", buttons);
if (res != "Cancel")
foreach (var item in SeverityValues)
{
if (item.Name == res)
{
Issue.SeverityEx = item.Value;
lbSeverity.Text = item.Name;
return;
}
}
}
Xaml
<!--Severity-->
<Label Grid.Row="0" Grid.Column="0" Text="SEVERITY" XAlign="Start" YAlign="Center" Style="{StaticResource Labelfont}" />
<Label x:Name="lbSeverity" Grid.Column="1" Text="" YAlign="Center" HorizontalOptions="StartAndExpand" Style="{StaticResource Labelfont}" />
<StackLayout Grid.Row="1" Grid.Column="0" Orientation="Horizontal" Spacing="4">
<Image Source="severity_5.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40" Opacity="0.4"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<StackLayout Grid.Row="1" Grid.Column="1" Orientation="Horizontal" Spacing="4">
<Image Source="severity_4.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40" Opacity="0.4"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<StackLayout Grid.Row="1" Grid.Column="2" Orientation="Horizontal" Spacing="4">
<Image Source="severity_3.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40" Opacity="1"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<StackLayout Grid.Row="1" Grid.Column="3" Orientation="Horizontal" Spacing="4">
<Image Source="severity_2.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40" Opacity="0.4"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<StackLayout Grid.Row="1" Grid.Column="4" Orientation="Horizontal" Spacing="4">
<Image Source="severity_1.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40" Opacity="0.4"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
Your code behind for changing opacity could look like this:
public void OnSeverityTapped(object sender, EventArgs args)
{
severity5.Opacity = 0.25;
severity4.Opacity = 0.25;
severity3.Opacity = 0.25;
severity2.Opacity = 0.25;
severity1.Opacity = 0.25;
var image = (Image)sender;
image.Opacity = 1;
}
}
and xaml:
<StackLayout>
<Image x:Name="severity5" Source="severity_5.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
<Image x:Name="severity4" Source="severity_4.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
<Image x:Name="severity3" Source="severity_3.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
<Image x:Name="severity2" Source="severity_2.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
<Image x:Name="severity1" Source="severity_1.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnSeverityTapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>

Categories