Adding numbers from Entry - c#

Hello guys i'm making xamarin.forms app and have two inputs from entries and I need to add the numbers entered into a separate sums every time a button is pressed
x:Name="Player1"
Text="{Binding Player1.Text}"
Style="{StaticResource LabelStyle}"
FontSize="20"
HorizontalOptions="Center"
VerticalTextAlignment="Center"
LineBreakMode="WordWrap"
/>
<Entry Grid.Column="1" Grid.Row="0"
x:Name="scoreEntry1"
Keyboard="Numeric"
FontSize="25"
Placeholder="0"
HorizontalOptions="Fill"
HorizontalTextAlignment="Center"
Margin="0,20,0,0"
/>
<Label Grid.Column="0" Grid.Row="1"
x:Name="Player2"
Text="{Binding Player2.Text}"
Style="{StaticResource LabelStyle}"
FontSize="20"
HorizontalOptions="Center"
VerticalTextAlignment="Center"
LineBreakMode="WordWrap"
/>
<Entry Grid.Column="1" Grid.Row="1"
x:Name="scoreEntry2"
Keyboard="Numeric"
FontSize="25"
Placeholder="0"
HorizontalOptions="Fill"
HorizontalTextAlignment="Center"
Margin="0,20,0,0"
/>
<Grid>
<Button Grid.Column="0" Grid.Row="0"
Text="Next round"
BackgroundColor="#26940F"
TextColor="#FFFFFF"
Clicked="Next_round"
Command="{Binding ButtonNext}">
</Button>
I've tried to do the calculation in the .cs but couldn't figure it out. Can someone help me?

first, don't use an event and a command on the button - pick one or the other
to add the numbers, do something like this
public void Next_round(object sender, EventArgs args)
{
// use int.Parse if the number are ints, I'm assuming double
var val1 = double.Parse(scoreEntry1.Text);
var val2 = double.Parse(scoreEntry2.Text);
// add them
var sum = val1 + val2;
// you haven't specified what you want to do with the result???
}
note that this is a very basic approach, and very brittle - it will break if the user enters in a string that is not a valid double. To handle this you should add some exception handling (ie, a try/catch block), or use TryParse instead of just Parse. Refer to the C# docs on either of these topics

Related

Xamarin Forms FontAwesome button text and normal text

I want to create a button using fontawesome library in xamarin
(expected output)
(this is mine)
<Button Text=" Bireysel"
TextColor="Black"
VerticalOptions="Start"
HorizontalOptions="Start"
FontSize="30"
Margin="15,0,0,0"
FontFamily="{StaticResource FontAwesomeSolid}"
BackgroundColor="Transparent"/>
I wanna change text style how can i do that?
Button can have both text and image with FontImageSource:
<Button Text="Left">
<Button.ImageSource>
<FontImageSource Glyph=""
FontFamily="{StaticResource FontAwesomeSolid}" />
</Button.ImageSource>
</Button>
Try this TextTransform="None"
<Button Text=" Bireysel"
TextColor="Black"
VerticalOptions="Start"
HorizontalOptions="Start"
FontSize="30"
Margin="15,0,0,0"
FontFamily="{StaticResource FontAwesomeSolid}"
BackgroundColor="Transparent"
TextTransform="None"/>

TapGestureRecognizer no longer working in xamarin.forms

This used to work for me but no longer does. Maybe because of updates? But I am just trying to get a tap event attached to my label called JournalWarning. I was using TapGestureRecognizer.
Before I had the TapGestureRecognizer calling an OnReconnect method in xaml like so
<StackLayout x:Name="Journal" IsVisible="false" VerticalOptions="FillAndExpand" Padding="20, 0, 20, 20" Spacing="10">
<StackLayout Orientation="Horizontal">
<Label x:Name="JournalTitle" FontSize="Micro" />
<Label x:Name="AboutFormat" Text="About Formatted Text" FontSize="Micro" TextColor="{StaticResource InputBackgroundColor}" HorizontalTextAlignment="End" HorizontalOptions="EndAndExpand" />
</StackLayout>
<StackLayout>
<local:DarkKeyboardEditor x:Name="JournalContent" Text="{Binding Source={x:Static local:JournalTracker.Current}, Path=Content}" TextChanged="OnJournalChanged" Focused="OnEdit" Unfocused="OffEdit" FontSize="Small" HeightRequest="{Binding Source={x:Static local:JournalTracker.Current}, Path=EntryHeight}" />
<Label x:Name="JournalWarning" Text="Your device has lost its connection to the journal server. Journals cannot be viewed or edited at this time. Tap here to try and reconnect." FontSize="Medium" Style="{StaticResource warningLabelStyle}" IsVisible="{Binding Source={x:Static local:JournalTracker.Current}, Path=IsConnected, Converter={StaticResource inverser}}" VerticalOptions="EndAndExpand" AutomationProperties.IsInAccessibleTree="true">
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnReconnect" />
</Label.GestureRecognizers>
</Label>
<BoxView x:Name="spacer" Style="{StaticResource SpacerStyle}" />
</StackLayout>
</StackLayout>
This stopped working so I thought maybe my padding was in the way and started to take away padding and margins but that made no difference so far. I also tried making the TapGestureRecognizer in C# like...
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
OnReconnect();
};
JournalWarning.GestureRecognizers.Add(tapGestureRecognizer);
Thought about making a command instead to see if that would make a difference. I'll probably try that next idk, this seems like a really noob question and I'm not sure why this isn't getting hit at all.
Can you see anything that might be an issue? Or did something changed in an update that I missed? Thanks

ListView items not completely loading from SQLite database [Xamarin.Forms]

I have recently noticed a problem with my listview and my local database using sqlite and Xamarin.Forms, my problem is that when the data of my table is dumped into my ObservableCollection (source item of the ListView) and my List View shows it, the ListView does not shows all the items in that are being supposed to be shown, i have tried by adding dynamically (without a database) items into my List View and by this way the problem seems to disappear, i am sure that the problem is not from my sqlite database beacuse also the problem in one past project that i use an api for getting my info the problem was the same, so i got in the conclusion that the problem was showing external information into my ListView so does anyone knows a possible solution for my problem, by the other way, it would be really helpful if someone could try doing a project with listview and sqlite database.
Important fact: The ListView in my XAML has the Frame controller, so when I don't use it, it shows me all the items from the database, I also don't know why
my current XAML code:
<ListView
SeparatorVisibility="None"
IsGroupingEnabled="True"
ItemsSource="{Binding TasksCollection}"
GroupDisplayBinding="{Binding Key}"
HasUnevenRows="True">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<TextCell
Text="{Binding Key}"
TextColor="White"/>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame
HeightRequest="150"
Margin="10"
HasShadow="True"
CornerRadius="25"
BackgroundColor="White">
<Grid
Padding="5">
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding GridExpandCommand}"/>
</Grid.GestureRecognizers>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label
Grid.Column="0"
Grid.Row="0"
Text="{Binding Name}"
FontAttributes="Bold"
FontSize="Small"
HorizontalOptions="Start"
VerticalOptions="Start">
</Label>
<Image
HeightRequest="25"
Grid.Row="0"
Grid.Column="1"
Source="{Binding TaskIcon}"
HorizontalOptions="End"
VerticalOptions="Start">
</Image>
<Label
Grid.Row="1"
Grid.Column="0"
Text="{Binding Description}"
FontAttributes="Bold"
FontSize="Small"
HorizontalOptions="Start"
VerticalOptions="End">
</Label>
<Button
Grid.Row="1"
Grid.Column="1"
Text="{Binding IsDone}"
TextColor="White"
FontAttributes="Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="Center"
FontSize="Small"
CornerRadius="100"
BackgroundColor="LawnGreen">
</Button>
</Grid>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
My code for adding the items into the collection (I use MvvmHelpers for grouping the data with Grouping) is the following:
foreach (var item in new string[]
{ Languages.HightPriorText, Languages.MediumPriorText, Languages.LessPriorityText })
{
var sorted = from tasks in tableList
orderby tasks.PriorityLevel // orberby is a keyword of System.Linq namespace
group tasks by tasks.PriorityLevel into taskList
select new Grouping<string, TaskItemViewModel>(taskList.Key, taskList);
List<Grouping<string, TaskItemViewModel>> listItems = sorted.Where(t => t.Key == item).ToList();
foreach (var item2 in listItems)
VeryImportantList.Add(item2);
}
the result is the following:
The image of the result of the app
Any answer i will be noticed about it, and also for any question, it would be awesome some help please!
I have finally solved it, and as i expected was a bug of my current version of Xamarin.Forms (4.0.0) so i update to the newest version of it and everything returns ok, as i expected!

WPF Issue with same ALT hotkey

I have a window with 3 textboxes and labels with following properties:
<Label x:Name="label" Content="_Label" Target="{Binding ElementName=textBox}"/>
<Label x:Name="label1" Content="Label"/>
<Label x:Name="label2" Content="_Label" Target="{Binding ElementName=textBox2}"/>
See this image
You can see that "label" and "label2" have the same ALT + L key but different Target property. When I focus textBox middle and I press ALT + L I need that textBox2 is focues (like in MFC). I can't use code behide because I was made about 200 windows.
Thanks a lot!
I think if you really need with out code behind or ViewModel you can do it by adding the same short cut to Label1 like below,
<Label x:Name="label" Grid.Row="0" Content="_Label" Target="{Binding ElementName=textBox}"/>
<Label x:Name="label1" Grid.Row="1" Content="_Label" Target="{Binding ElementName=textBox1}"/>
<Label x:Name="label2" Grid.Row="2" Content="_Label" Target="{Binding ElementName=textBox2}"/>
<TextBox x:Name="textBox" Grid.Column="1" Grid.Row="0"/>
<TextBox x:Name="textBox1" Grid.Column="1" Grid.Row="1"/>
<TextBox x:Name="textBox2" Grid.Column="1" Grid.Row="2"/>
If you can make a ViewModel then InputBinding with KeyBinding can make it work.

changing textblock to textbox loses data bindings -

this is a wpf/c# app - I have one grid where I used text boxes and the data bindings were fine. I later built another grid using textblocks. both worked well. but when I changed the textblocks in the second grid into textboxes, all but one stopped displaying data.
I copied both the c# LINQ code and the XAML code that provide the data for the bindings, recompiled, saved it to NotePad++, and closed VS(8). After reopening the project, I reinserted the code, but I still got no data.
Any ideas that would help would be greatly appreciated.
Here's the c# code
var vendDetail = from v in dbC.Vendors
where v.VendorID == vendorid
select new
{
v.Address1,
v.Address2,
CSZ = v.City + ", " + v.State + " " + v.Zip,
v.Phone,
v.Fax,
v.Contact,
v.Terms,
v.eMail
};
grVendorData.DataContext = vendDetail;
And the XAML code:
<TextBox x:Name="txtVendorAddr1"
Text="{Binding Path= Address1}"
Background="AliceBlue"
Grid.Row="2"
Grid.Column="1"/>
<TextBox x:Name="txtVendorAddr2"
Text="{Binding Path= Address2}"
Grid.Row="3"
Grid.Column="1"
Background="AliceBlue"/>
<Label Content="City"
Grid.Row="4"
Grid.Column="0"/>
<TextBox x:Name="txtVendorCity"
Text="{Binding Path= CSZ}"
Grid.Row="4"
Grid.Column="1"/>
<Label Content="Phone"
Grid.Row="1"
Grid.Column="0"/>
<TextBox x:Name="txtVendorPhone"
Text="{Binding Path = Phone}"
Grid.Row="1"
Grid.Column="1"/>
<Label Content="Fax"
Grid.Column="2"
/>
<TextBox Text="{Binding Path = Fax}"
Grid.Row="0"
Grid.Column="3" />
<Label Content="Terms"
Grid.Row="2"
Grid.Column="2"/>
<TextBox Text="{Binding Path = Terms}"
Grid.Row="2"
Grid.Column="3"/>
<Label Content="Notes"
Grid.Row="3"
Grid.Column="2" />
<TextBox Text="{Binding Path = Notes}"
Grid.Row="3"
Grid.Column="3"
Grid.RowSpan="2"
TextWrapping="WrapWithOverflow" />
<Label Content="eMail"
Grid.Row="1"
Grid.Column="2" />
<TextBox Text="{Binding Path = eMail}"
Grid.Row="1"
Grid.Column="3"/>
You cannot bind to anonymous types, which is why the binding worked after you began selecting the whole record.

Categories

Resources