I'm new to WP7 and I want to know if there is any way to add items like a TextBlock to
a page dynamically using the .cs part??
Try this
var textBlock = new TextBlock();
// set some properties
YourMainContainer.Children.Add(textBlock); //
If you need more details just comment this
If you know the controls that you'd like to appear on the page dynamically, then I'd approach the problem by including those controls in the XAML and using the Visibility property on the controls to show and hide them. In Silverlight, the Visibility enumeration is limited to the values Visible and Collapsed, so when it isn't visible the it doesn't take up any space. You can control Visibility with data-binding by using a converter (search on "visibility bind converter") if you are intersted in pursuing that avenue. You can show/hide groups of controls by changing the Visibility of their parent control, such as StackPanel or custom control.
Try this one,
TextBlock txtmsg = new TextBlock();
txtmsg.Text = "New Program.";
txtmsg.Margin = new Thickness(10, 20, 10, 10);
txtmsg.TextWrapping = TextWrapping.Wrap;
txtmsg.FontSize = 28;
txtmsg.TextAlignment = TextAlignment.Center;
ContentPanel.Children.Add(txtmsg);
Related
I have a page created using Xamarin Forms. I want to add a toolbar at the bottom of the screen like an overlay but doesnt change the actual layout of the screen. It should just overlay above everything and always be at the bottom.
It should showup only when there is a particular event. SO it will be added dynamically.
Any ideas - or if you can send point me in the right direction. I do not want to use any nuget packages.
Have you tried to use AbsoluteLayout/Grid?
<Grid>
<StackLayout><Label Text="Your content"/></StackLayout>
<Button x:Name="toolbar" VerticalOptions="End" Text="Your toolbar" />
</Grid>
Show/hide the control based on your event.
If you could show the code of displayToolbar it would be easier to write a code that suits your needs, but it should be pretty easy even without the code :)
You could do this with a RelativeLayout, AbsoluteLayout or a Grid but I recommend doing it with a Grid because is much lighter than RelativeLayout and has a lot more functions than AbsoluteLayout.
So, make the root of every page that you want to use displayToolbar a Grid with one row and one column. Add the actual content of the page to that Grid as a child view.
Now comes the part that would be easier with your code. When you want to display the toolbar, add the toolbar view as a child of the root Grid with VerticalOptions set to LayoutOptions.End.
That's it. The toolbar will be added in front of every view of the page and if you want to dynamically remove the toolbar, remove the root Grid's last child.
The layout would be something like this:
Grid root;
internal SomePageConstructor()
{
root = new Grid
{
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
},
RowDefinitions =
{
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }
}
};
root.Children.Add(actualPageContent, 0, 0);
Content = root;
}
void DisplayToolbar()
{
var toolbar = new StackLayout { VerticalOptions = LayoutOptions.End };
root.Children.Add(toolbar, 0, 0);
}
void RemoveToolbar()
{
root.Children.Remove(root.Children[1]);
}
Note: I did this directly on StackOverflow, code could need some corrections.
From this sample, you could do an animation of the toolbar coming from below the page or something like it. Just change the DisplayToolbar and the RemoveToolvar methods.
Obs. Method names on C# are PascalCased (every word is capitalized) so your displayToolbar is DisplayToolbar.
Hope it helps! :)
I am using Xamarin.Form platform to use Map feature in my application. I could able to add an annotation on the map. However, I would like to know is there a way to add disclosure indicator on annotation that enables user to tap to go to DetailViewController.
using Xamarin.Forms.Maps;
Map map;
Title= "MapView";
map = new Map {
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand
};
map.Pins.Add(new Pin {
Position = new Position(29.7,-95.0177232),
Label = "Boardwalk"
});
I want something similar to the following screnshot.
I don't believe this is possible to provide your own views in the current Xamarin.Forms Maps component (v1.2.3x) as it is very locked down.
Update 1:-
Unfortunately not. This is a requested feature for Xamarin.Forms, as of October 24 here.
The only way around at present would be to create your own custom renderer.
Is there a way to search a name on the table(Listview) in Xamarin Form without using any MonoTouch components - for crossplatform?
I have a number of names and populated on listview and what I want is that once user types the name and listview would be sorted and it should bring to matched name. I have added searchbar at the top of the listview.
I do not know how to implement OnSearchBarButtonPressed method. I believe that I should not reinvent the wheel again.
SearchBar searchBar = new SearchBar
{
Placeholder = "Search Employee Name",
};
searchBar.SearchButtonPressed += OnSearchBarButtonPressed;
Padding = new Thickness (10, 20, 10, 10);
Content = new StackLayout () {
Children={searchBar,listView}
};
In relation to your other posting here your ListView ItemsSource is a collection of objects.
The ListView control has no way of knowing about your model and wouldn't be able to any filtering by itself.
There appears to be nothing inbuilt into the ListView that would allow you to specify some Predicate to help with this task anyway.
You should therefore do the filtering in the SearchBar.SearchButtonPressed and then re-assign the ListView ItemsSource to reflect the newly filtered ItemsSource you want to show to the end-user. Your filtering logic could then be anything you wanted as your be able to customize this to even filter over many fields etc.
The SearchBar.SearchButtonPressed is just an event which you assign an EventHandler, like you are already doing. You just need to fill in the filtering logic and update the ListView accordingly.
hi i have created a user control.
now i want to add this control to a page e.g. when the user clicks a button (important is, that i dont want to add it direcly in xaml, i want to add id in the c# code section)
how to so this?
In your button's Click event, you could do something like:
MyControl control = new MyControl();
// Set whatever properties you need in your control here
LayoutRoot.Children.Add(control);
if (listBox1.Items.Count == 0)
{
Popup popup = new Popup();
WPCpopup po = new WPCpopup(popup);
popup.Width = System.Windows.Application.Current.Host.Content.ActualWidth;
popup.Child = po;
popup.VerticalOffset = 300;
popup.IsOpen = true;
}
WPCpopup is usercontrol
I have a TabFolder which was resized initially. Under the TabFolder is a TabItem and under that TabItem is a Button. The Button inherited the size of the TabFolder so it's huge. What's the best way to resize the Button? Using button.setBounds(...) doesn't work.
Here is the code snippet:
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
TabFolder tabFolder = new TabFolder(container, SWT.NONE);
Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
tbtmNewItem.setText("1");
TabItem tbtmBrowse = new TabItem(tabFolder, SWT.NONE);
tbtmBrowse.setText("3");
Button btnNewButton = new Button(tabFolder, SWT.BORDER | SWT.CENTER);
btnNewButton.setAlignment(SWT.CENTER);
tbtmBrowse.setControl(btnNewButton);
btnNewButton.setText("Push");
tabFolder.setBounds(0, 0,dim.width-10,dim.height-10);
createActions();
initializeToolBar();
initializeMenu();
this.setPartName("Home");
}
I found a solution. I just removed the button from the control of the tab.
Removed:
tbtmBrowse.setControl(btnNewButton);
The button can now be resized independently or using setBounds when using a null layout.
So the Control you set on TabItem was a Composite? Or a Button?
The was to control the size of the button is to create a Composite and set that on the TabItem. Then you can add your button(s) to the Composite. You then set a layout on the composite to control how your button(s) are laid out. See http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html for more details on layouts.
EDIT:
To use it, you insert a composite between the tab folder and the button:
Composite page = new Composite(tabFolder, SWT.NONE);
page.setLayout(new GridLayout(1, false));
Button btnNewButton = new Button(page, SWT.BORDER | SWT.CENTER);
btnNewButton.setAlignment(SWT.CENTER);
btnNewButton.setText("Push");
tbtmBrowse.setControl(page);
You use layouts to control the sizes of child controls ... in this case, your button. See the Understanding Layouts article.