I was trying to create two different kinds of buttons: buttons with background and buttons without background.
And unfortunately I can't figure out how I can set some buttons to use the first style and some buttons to use the second.
Here's a code sample (although i doubt it'll help)
Is there some way to change the Style of the button in the .cs file, or any workaround?
//init hyperlink
Button hyperlink = new Button();
Label hyperlink_label = new Label();
hyperlink_label.Text = hyperlink_text;
hyperlink_label.ModifyFg(StateType.Normal, new Gdk.Color(0,0,0));
hyperlink_label.ModifyFg(StateType.Prelight, new Gdk.Color(255,255,255));
hyperlink_label.ModifyFg(StateType.Selected, new Gdk.Color(255,255,255));
Pango.FontDescription hyperlinkFontDesc = new Pango.FontDescription();
hyperlinkFontDesc.Family = "Adobe Garamond Pro";
hyperlinkFontDesc.AbsoluteSize = hyperlink_fontSize * Pango.Scale.PangoScale;
hyperlink_label.ModifyFont(hyperlinkFontDesc);
hyperlink.Add(hyperlink_label);
mainWindowFixed.Put(hyperlink, hyperlink_pos[0], hyperlink_pos[1]);
//init startbutton
Button startButton = new Button();
Label startLabel = new Label();
startLabel.Text = startButton_text;
startButton.Settings.ThemeName = "Ludwig_AutoUpdater";
startLabel.ModifyFg(StateType.Normal, new Gdk.Color(255,255,255));
startLabel.ModifyFg(StateType.Prelight, new Gdk.Color(255,255,255));
startLabel.ModifyFg(StateType.Selected, new Gdk.Color(255,255,255));
Pango.FontDescription startLabelFontDesc = new Pango.FontDescription();
startLabelFontDesc.Family = "Klavika bd";
startLabelFontDesc.AbsoluteSize = startButton_fontSize * Pango.Scale.PangoScale;
startButton.Add(startLabel);
startButton.Child.ModifyFont(startLabelFontDesc);
startButton.SetSizeRequest(startButton_size[0], startButton_size[1]);
mainWindowFixed.Put(startButton, startButton_pos[0],startButton_pos[1]);
Don't change the style of Gtk.Button. Create a custom widget derived from Gtk.Button and change that widget's style. For example:
public class BGButton : Gtk.Button {}
and
public class NoBGButton : Gtk.Button {}
and now change the style of BGButton and NoBGButton.
Then if you want a Button with a Background use BGButton , else use NoBGButton.
Related
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/.
I have a Storyboard with a TabBarController and six subviews as Tabs. The TabBarController and each Tab has a Controller Class assigned.
The content of some tabs is loaded from a server. In some cases there is no content for a tab and for this case I want to hide the TabBarItem.
I've tried (just for testing)
this.TabBarController.TabBar.Items [0].Enabled = false;
but it doesn't work.
How can I hide a single TabBarItems? I've searched Google, StackOverflow and the Xamarin Forum but found no solution for my problem.
The only solution I've found was to remove the subview from the subviews-array, but in this case I cannot simply "reactivate" the tab if I want to show the tab again.
In case of UITabBar:
UITabBar sampleTabBar;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
sampleTabBar = new UITabBar ();
sampleTabBar.Frame = new CoreGraphics.CGRect (10f, 64f, this.View.Frame.Width - 10, 50f);
var tabBarItem = new UITabBarItem ("TAB BAR ITEM", null, 4);
sampleTabBar.Items = new UITabBarItem[]{ tabBarItem };
sampleTabBar.ItemSelected += (sender, e) => {
Console.WriteLine ("tab bar button item slected");
//Disable Tab Bar item here
tabBarItem.Enabled = false;
tabBarItem.Title = "Disabled now";
};
this.View.AddSubview (sampleTabBar);
}
tabBarItem.Image property also can be used for getting the result.
1) Set a placeholder image as default.
2) Populate images from array/source to uibarButtonItem.
3) Use placeholder image wherever need to hide uibarbuttonitem.
I think if you want to completely hide the UITabbarItem you will need to remove the UIViewController from the UITabbarController like so:
var tbViewControllers = new List<UIViewController> (TabBarController.ViewControllers);
tbViewControllers.RemoveAt (2); // remove whatever index you need.
TabBarController.ViewControllers = tbViewControllers.ToArray ();
but you will need to keep a reference to all the UIViewControllers you want and add it back in like so:
var tbViewControllers = new List<UIViewController> (TabBarController.ViewControllers);
tbViewControllers.Insert (2, new RemvoedViewController());
TabBarController.ViewControllers = tbViewControllers.ToArray ();
I've created an activity indicator and added it to StackLayout and when I make it running, in the emulator it shows in the top right corner Android 4.4 and in iOS no show and in Android 6 phone, it don't show.
var indicator = new ActivityIndicator()
{
Color = Color.Blue,
};
indicator.SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy", BindingMode.OneWay);
indicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsBusy", BindingMode.OneWay);
AbsoluteLayout.SetLayoutFlags(indicator, AbsoluteLayoutFlags.PositionProportional);
AbsoluteLayout.SetLayoutBounds(indicator, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
mainLayout.Children.Add(indicator);
I want to show the activity indicator to the center of the screen because the operation takes time to complete.
The indicator that you are seeing in the status bar is the default behavior of the IsBusy property of the base page class. The reason your code isn't working is because you are attempting to bind visibility of your ActivityIndicator to that property - but you aren't specifying a binding source. If you look in your debugger's application output log then you will probably see messages along the lines of "Property 'IsBusy' not found on type 'Object'".
To fix it, you simply need to point the Binding Context of each binding to the form. Give this a try:
public partial class App : Application
{
public App ()
{
var mainLayout = new AbsoluteLayout ();
MainPage = new ContentPage {
Content = mainLayout
};
var containerPage = Application.Current.MainPage;
var indicator = new ActivityIndicator() {
Color = Color.Blue,
};
indicator.SetBinding(VisualElement.IsVisibleProperty, new Binding("IsBusy", BindingMode.OneWay, source: containerPage));
indicator.SetBinding(ActivityIndicator.IsRunningProperty, new Binding("IsBusy", BindingMode.OneWay, source: containerPage));
AbsoluteLayout.SetLayoutFlags(indicator, AbsoluteLayoutFlags.PositionProportional);
AbsoluteLayout.SetLayoutBounds(indicator, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
mainLayout.Children.Add(indicator);
containerPage.IsBusy = true;
}
}
You add your activity indicator to stack layout, but you are setting Absolute layout LayoutFlags, they won't work.
to be able to achieve what you want, you need suck structure
AbsoluteLayout
StackLayout
ActivityIndicator
mainLayout should be AbsoluteLayout, all content should be contained in nested StackLayout.
I'm loading elements on a page dynamically (reading the contents of an XML file). The dynamic content is loaded into a StackPanel. Each element of the content consists of a TextBlock and one other UI element, so for each pair I create a new StackPanel which is then added to the parent StackPanel. The code looks like this:
TextBlock header = new TextBlock() {
Text = "Heading 1",
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"],
};
TextBox item = new TextBox() {
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
};
StackPanel sp = new StackPanel();
sp.Children.Add( header );
sp.Children.Add( item );
parentSP.Children.Add( sp );
I want to add a ContextMenu to this StackPanel (sp, not parentSP); depending on some parameters read from the file it could be one of 2 different context menus. I tried the following but it is not working:
ContextMenu cm = new ContextMenu();
RoutedEventHandler clickHandler = new RoutedEventHandler( OnContextMenuClicked );
// Add "edit" entry
MenuItem menuItem = new MenuItem() {
Header = "edit",
Tag = "edit",
};
menuItem.Click += clickHandler;
cm.Items.Add( menuItem );
// Add "delete" entry
menuItem = new MenuItem() {
Header = "delete",
Tag = "delete",
};
menuItem.Click += clickHandler;
cm.Items.Add( menuItem );
parentSP.Children.Add( cm );
How do I add a context menu to the StackPanel programmatically?
Also, is there a better way to solve this problem? Maybe by storing the 2 different types of context menus in a XAML resources section and adding them as needed? I tried doing this by adding the context menus to the parent's StackPanel.Resource section but got an error saying "A property element cannot be the direct child of another property element"
Thanks in advance for your help
ContextMenuService.SetContextMenu(sp, cm);
I am new to the whole QT things, so please bear with me :-)
I am working on a program that has a QMainWindow with a couple of menu options. I then use QActions to call functions to do something with the menu options.
I have put together a QDialog, which shows when called directly, but not when called through the QAction dialog.
Code:
Constructor Code
View::View(QWidget *parent):QMainWindow(parent)
{
QWidget *topFiller = new QWidget;
topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(5);
layout->addWidget(topFiller);
setLayout(layout);
createActions();
createMenus();
change(); //Works but displayed as a separate window
QWidget::setWindowTitle( "Stock Editor");
setMinimumSize(300, 300);
resize(680, 520);
}
QAction Snippet
changeact = new QAction(tr("&Change"), this);
connect(saveact, SIGNAL(triggered()), this, SLOT(change()));
Change() Function
void View::change()
{
QDialog *dialogWin = new QDialog(this);
dialogWin->setWindowTitle("Add Item");
QFormLayout *formLayout = new QFormLayout();
QLineEdit *barcodeLineEdit = new QLineEdit;
QLabel *barcodeLabel = new QLabel("Barcode");
QLineEdit *descLineEdit = new QLineEdit;
QLabel *descLabel = new QLabel("Description");
QSpinBox *stockSpinBox = new QSpinBox;
QLabel *stockLabel = new QLabel("Stock");
QSpinBox *priceSpinBox = new QSpinBox;
QLabel *priceLabel = new QLabel("Price");
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
// connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
// connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
formLayout->addRow(barcodeLabel, barcodeLineEdit);
formLayout->addRow(descLabel, descLineEdit);
formLayout->addRow(stockLabel, stockSpinBox);
formLayout->addRow(priceLabel, priceSpinBox);
QVBoxLayout *mainLayout = new QVBoxLayout(dialogWin);
mainLayout->addLayout(formLayout);
mainLayout->addWidget(buttonBox);
dialogWin->setLayout(mainLayout);
dialogWin->activateWindow();
dialogWin->show();
}
How can I get the box to appear on top of the Main Window when called?
Thanks for the assistance