Adding buttons dynamically in stacklayout - xamarin

I would like to add a button dynamically in StackLayout when "add" button is clicked. I wrote stacklayoutname.children.add(button), it does not giving me the thing i am looking for.
In xaml:
<StackLayout x:Name="layout">
<Button Text="add" Clicked="Addbutton"/>
</StackLayout>
In code:
private void Addbutton(object sender, EventArgs e)
{
var layout = new StackLayout();
var btn = new Button { Text = "New button", FontSize = 30, TranslationY = 30 };
this.Content = layout;
layout.Children.Add(btn);
}
It is giving only new button and add button is disappearing, but I want whenever we click on add button it should give number of new button equal to the number of clicks on add button.

Since you already have a StackLayout, there's no need to add a new one, because it replaces the old one if you do. The following will add a button to the StackLayout on every button click.
// Define a field for StackLayout
StackLayout parent;
public void Addbutton(object sender, EventArgs e)
{
// Define a new button
Button newButton = new Button { Text = "New Button" };
// Creating a binding
newButton.SetBinding(Button.CommandProperty, new Binding ("ViewModelProperty"));
// Set the binding context after SetBinding method calls for performance reasons
newButton.BindingContext = viewModel;
// Set StackLayout in XAML to the class field
parent = layout;
// Add the new button to the StackLayout
parent.Children.Add(newButton);
}
For more information about Binding, check out BindableObject Class and Data Binding Basics.

Related

FontAwesome icon with normal text in Xamarin

I want to make a button that has a small icon (from FontAwesome) and text on it in my Xamarin app. I know I can just make a button but the problem is that I will require two fonts (the standard font for text and FontAwesome for the icon). Would anyone happen to know how I can do this, or if there is another way to achieve what I want?
Thanks!
As the json mentioned, I just made a simple implementation.
Create a new class inherit from Label, set FormattedText to combine the string(standard and icon), and add tap gesture on it .
public class MyLabel : Label
{
public delegate void MyHandler(object sender, EventArgs e);
public event MyHandler myEvent;
public MyLabel(string _myIcon, string _myText)
{
//build the UI
FormattedString text = new FormattedString();
text.Spans.Add(new Span { Text = _myIcon ,FontFamily= "FontAwesome5Free-Regular" });
text.Spans.Add(new Span { Text = _myText, TextColor = Color.Red ,BackgroundColor = Color.Blue });
FormattedText = text;
//tap event
TapGestureRecognizer tap = new TapGestureRecognizer();
tap.Tapped += (sender,e) => {
myEvent(sender,e);
};
}
}
Usage
MyLabel label = new MyLabel("", "test");
label.myEvent += (sener,e) =>
{
//do something when tapping
};
Content = label;
For how to integrate FontAwesome in Xamarin.Forms ,refer to
https://montemagno.com/xamarin-forms-custom-fonts-everywhere/.

Change BoxView color on Tap

I have a BoxView that I want to change the color of when the user taps it. I created a BoxView called tapView and set the GestureRecognizer like so:
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
// handle the tap
OnTapGestureRecognizerTapped(s, e);
};
tapView.GestureRecognizers.Add(tapGestureRecognizer);
and my method to handle the tap:
void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
var box = (BoxView)sender;
box.Color = Color.Red;
}
However this doesn't actually change the color on the Tap. Debugging and stepping through the code it does actually update the BoxView color in the code but not on the screen. What do I need to do to make this actually update the color when touched?
Try this solution, assuming you give your BoxView the following property in your XAML x:Name="myBox"
//This code betlow the page InitializeComponent call
var boxTapHandler = new TapGestureRecognizer();
boxTapHandler.Tapped += ChangeColor;
myBox.GestureRecognizers.Add(boxTapHandler);
Change color method, similar, though referencing the BoxView by name
private void ChangeColor(object sender, EventArgs args)
{
myBox.Color = Color.red;
}
So I discovered as standalone code this works ok, but in my MVVM app it does not. I wound up creating a box on top and changing the opacity on a tap worked just fine.

How to identify user tapped on Editor field in Xamarin forms?

First thing, I have an Editor.I can Identify if the user entered any text in that Edior.But i need to return something when the user tapped on the Editor. How to achieve this?
var msgEditor = new Editor
{
Margin = new Thickness(10, 0, 10, 0),
HeightRequest = App.ScreenHeight,
};
Secong thing, Editor is inside the scrollview.When i tapped on the Editor scrollview scrolls down.I need to manually pull down to see the cursor.How to set content offset when i tapped on Editor?
ScrollView scroll = new ScrollView
{
Content = msgEditor,
};
Content = scroll;
On the editor, you have the focus event that notifies you that the user tapped the editor. You can do as follow :
{
var editor = new Editor();
editor.Focused += EditorOnFocused;
}
private void EditorOnFocused(object sender, FocusEventArgs focusEventArgs)
{
//do your stuff
}

Xamarin.Forms: Exchange view in container

I've created some pages
this.content1 = new DetailPage("ContentPage1");
this.content2 = new DetailPage("ContentPage2");
and I have defined a field
private View detailView;
with the following layout
Content = new StackLayout
{
Padding = new Thickness(0, Device.OnPlatform<int>(20, 0, 0), 0, 0),
Orientation = StackOrientation.Horizontal,
Children = {
buttonContainer,
this.detailView,
},
};
On a button click the detailView should be exchanged
private void Button1_Clicked(object sender, EventArgs e)
{
this.detailView = this.content1.Content;
}
The click event is called, but the view isn't updated. Is this the wrong way to exchange a "subview" in a container? How is this done?
You need to remove the current detailView from the Children collection and then add your new view afterward. Simply swapping the value of detailView will not affect the visual UI.
If I am understanding the context of your code snippets correctly, then this should resolve the problem in your Button1_Clicked handler:
((StackLayout) Content).Children.Remove(this.detailView);
this.detailView = this.content1.Content;
((StackLayout) Content).Children.Add(this.detailView);

Show applicationbar menu programmatically (wp7)

I have a wp7 with some buttons in the application bar.
When each button is pressed I change the menuItems of the application bar's menu.
After this, I want to automatically open the menu when an application bar button is pressed.
But it seems that the SDK doesn't allow me to do that.
Do you know any work around?
I was thinking, if the above is not possible, to simulate a user finger click at the bottom right corner of the screen to open the menu. Any ideas on that?
Thanx in advance
It's possible to change the Application Bar Menu Items in response to an Icon Button click as demonstrated in the code below.
There isn't a way to forcibly open (or close) the application bar through code through.
It also isn't possible to simulate a finger click on the application bar as this isn't part of the actual page. Note that even if possible any click would need to be in the top right or bottom left if the device was in a landscape orientation.
Here's some code which demonstrates changing the menu items:
public partial class MainPage : PhoneApplicationPage
{
private ApplicationBar appbar;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
appbar = new ApplicationBar();
var ib1 = new ApplicationBarIconButton(new Uri("/images/one.png", UriKind.Relative)) { Text = "Option one" };
ib1.Click += new EventHandler(ShowMenuOption1);
var ib2 = new ApplicationBarIconButton(new Uri("/images/two.png", UriKind.Relative)) { Text = "Option two" };
ib2.Click += new EventHandler(ShowMenuOption2);
appbar.Buttons.Add(ib1);
appbar.Buttons.Add(ib2);
// Show menu option 1 as default
DisplayMenuOption1();
this.ApplicationBar = appbar;
}
private void DisplayMenuOption1()
{
appbar.MenuItems.Clear();
var itemA = new ApplicationBarMenuItem("AAAA");
var itemB = new ApplicationBarMenuItem("BBB");
appbar.MenuItems.Add(itemA);
appbar.MenuItems.Add(itemB);
}
private void DisplayMenuOption2()
{
appbar.MenuItems.Clear();
var itemC = new ApplicationBarMenuItem("CCCC");
var itemD = new ApplicationBarMenuItem("DDDD");
appbar.MenuItems.Add(itemC);
appbar.MenuItems.Add(itemD);
}
private void ShowMenuOption2(object sender, EventArgs e)
{
DisplayMenuOption2();
}
private void ShowMenuOption1(object sender, EventArgs e)
{
DisplayMenuOption1();
}
}
As far as I'm aware this capability has not been exposed as yet. It wasn't possible during beta and I've not noticed anything that's changed since that would allow it. You could always comment on the their suggestions forum or raise it on connect (vs/wpdt).

Resources