i'm tring to extend UIButton class to add my id property.
How i can do this?
Problem is to create a button you use
var button = UIButton.FromType(UIButtonType.RoundRect);
so, if i've
public class MyButton : UIButton
how i can create my button?
thanks.
class MyButton : UIButton
{
public MyButton(RectangleF rect) : base(rect) {}
static MyButton FromType(UIButtonType buttonType)
{
var b = new MyButton (new RectangleF(0, 0, 200, 40));
b.SetTitle("My Button",UIControlState.Normal);
//additional customization here
return b;
}
}
Here is how you would create your button:
MyButton button = new MyButton (new RectangleF(0, 0, 250, 37));
button.SetTitle("My Button",UIControlState.Normal);
To Create a button of a type you could try:
MyButton button = new MyButton (UIButtonType.RoundRect);
and see if it returns a button of the correct type which inherits from your MyButton class - but I doubt it. You may have to add the image and size it yourself:
UIImage image = UIImage.FromFile("action.png");
MyButton button = new MyButton (new RectangleF(0, 0, 250, 37));
button.SetBackgroundImage(image,UIControlState.Normal);
Not sure if there's another way but looking at the docs the UIButton.ButtonType is read only and there's no SetType method so you can only set a UIButton class ButtonType property by instantiating it and passing the type into the constructor.
w://
Related
I want to have a different margin for iOS and Android so I tried to make if/else but the MyButton cannot be found when then button is inside the if/else like this:
if (Device.RuntimePlatform == Device.iOS)
{
var MyButton = new Button
{
Margin = new Thickness(0, -15, 0, 0)
};
}
else
{
var MyButton = new Button
{
Margin = new Thickness(-10, -15, 0, 0)
};
}
var MyStackLayout = new StackLayout
{
Children = { MyButton }
};
The name MyButton does not exist in the current context.
Is there a work around or a different method for this?
change your code as below:
Button MyNewButton;
if (Device.RuntimePlatform == Device.iOS)
{
MyNewButton= new Button
{
Margin = new Thickness(0, -15, 0, 0)
};
}
else
{
MyNewButton= new Button
{
Margin = new Thickness(-10, -15, 0, 0)
};
}
var MyStackLayout = new StackLayout
{
Children = { MyNewButton}
};
When you define a variable in if body, it cannot be access in out of if body.
And if you declare variable with the same name (Mybutton), change this button name.
As it was mentioned multiple times, the problem is that MyButton is living in a wrong scope. What is a scope?
The scope of a variable determines its visibility to the rest of a
program. In the examples throughout the C# Fundamentals tutorial,
variables have been defined within methods. When created in this way,
the scope of the variable is the entire method after the declaration.
This means that the variable is available to use within the method but
when control passes to another method the variable is unavailable.
You can read more about it here.
Basically I want to make functionality, when I input text to my Editor it will appear inserted data to my label. And if I will swipe page to another page, that data should be bind'ed to that label in previous page where I entered data.
So I have portable class. In that class I have method public ContentPage CreatePage(MyObject thing) here I define many Labels, boxes , buttons and etc. But I will indicate most important things: Here I am define my Label and Editor:
public partial class CoolPage: CarouselPage
{
public CoolPage()
{
foreach (MyObject p in things)
{
Children.Add(CreatePage(p));
}
}
public ContentPage CreatePage(MyObject thing) {
var emptyLabel = new Label
{
Text = "Text",
WidthRequest = 50,
HeightRequest = 50,
BackgroundColor = Color.White
};
((StackLayout)page.Content).Children.Add(emptyLabel);
var inputNumb = new Editor
{
Text=thing.Number,
TextColor = Color.Black,
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Fill,
IsVisible = true,
BackgroundColor = Color.White
};
inputNumb.SetBinding(Label.TextProperty, "Text");
inputNumb.BindingContext = thing.Number;
((StackLayout)page.Content).Children.Add(inputNumb);
}
}
I have tried to impelemnt such a event:
inputNumb.Completed += (sender, args) =>
{
inputNumb.SetBinding(Label.TextProperty, "Text");
inputNumb.BindingContext = thing.Number;
};
but it is not working. And I think because it is on same method. Also I tried to do out of method scope, by implementing such a line on CreatePage method inputCarNumb.Completed += InputCarNumb_Completed; But then when you define your variable inputNumb it doesn't recognize and I don't know how to implement in other case. I know it is very simple, but I think I miss something by doing SetBinding / BindingContext .
I solved this problem like this:
emptyLabel.SetBinding(Label.TextProperty, "Text");
emptyLabel.BindingContext = inputNumb;
Make sure your MyObject inherits from and implements INotifyPropertyChanged so that PropertyChanged fires whenever Number changes. I generally inherit from XLabs's ViewModel, and use their SetProperty method. Don't bother setting the binding in the event. But the 2nd parameter of SetBinding should be "Number" which is MyObject's property name. Also the BindingContext should = thing.
I have a button called myBtn. in XAML: ( <Button x:Name="myBtn" ... )
I have also a variable, which value is myBtn
Now, I need change this button's color:
public string buttonName = "myBtn";
private void method ()
{
this.buttonName.Background = new SolidColorBrush(Color.FromRgb(255, 0, 255));
}
This gives error:
string does not contain definition for Background
What is right syntax to do this?
Try this:
public Button buttonName;
private void method ()
{
buttonName = this.FindName("myBtn") as Button;
buttonName.Background = new SolidColorBrush(Color.FromRgb(255, 0, 255));
}
myBtn.Background = new SolidColorBrush(Color.FromRgb(255, 0, 255));
Once you give the control the x:Name attribute, you can refer to it directly from code behind without doing anything else.
You can access the background color of a named XAML button by using
myBtn.Background = new SolidColorBrush(Color.FromRgb(255, 0, 255));
The name you specify becomes a Button object for the code-behind in the application you are building.
I am initializing an object with several properties. However, there are multiple properties that are always the same (styling).
Consider the following initializing block of code:
private static Button _saveButton = new Button
{
Text = "Save",
HorizontalOptions = LayoutOptions.Center,
WidthRequest = 500,
IsVisible = false
//applyStandard(this) ?
};
I would like to pass _saveButton to a method, which changes its TextColor and BorderColor property with something like void applyStandard(View v).
How could I do that, if possible?
You can't access the button instance in the initializer, but you can make an extension method that you call right after it:
public static class Extensions {
public static Button ApplyStandard(this Button button) {
button.TextColor = Colors.Red;
return button;
}
}
By returning the button from the extension method, you can chain it into the creation:
private static Button _saveButton = new Button {
Text = "Save",
HorizontalOptions = LayoutOptions.Center,
WidthRequest = 500,
IsVisible = false
}.ApplyStandard();
You can't do the in the object initializer. You need to separate the method call from initialization.
What you have is nearly there, i think youre approaching the problem from the wrong direction. As already mentioned, you cant do what youre proposing with object initialization syntax. The simplest way to solve your problem (without simply creating your own button type) would be to have a method that creates a button, and sets all of your common properties. You can then set any of the others on a per instance basis:
private static Button CreateCustomButton()
{
Button button = new Button();
button.ForeColor = Color.Black;
// set other properties, initial setup etc...
return button;
}
I am searching for answer many hours and with no success. There is the way to do that, but every included element has to be static. What is not suitable for me.
I have MainPage class and Tile class. This tile class instantiates few kinds of tiles in MainPage layout. I need to add to some kinds of these tiles Tapped event. This event should modify MainPage elements.
What is the best way to do that? I always end with "non-static warning", as you can see in the code:
public class Tile
{
public static Grid buttonTile_MapPanel(int rowNum, int colNum)
{
Grid mapPanelButton = new Grid();
mapPanelButton.Background = new SolidColorBrush(Color.FromArgb(255, 50, 50, 50));
TextBlock title = new TextBlock();
title.Text = "Mapa";
title.HorizontalAlignment = HorizontalAlignment.Center;
title.VerticalAlignment = VerticalAlignment.Center;
title.Foreground = new SolidColorBrush(Colors.White);
mapPanelButton.Children.Add(title);
//this will be signed as error: An object reference is required for non static field...
mapPanelButton.Tapped += new RoutedEventHandler(MainPage.mapPanelService);
return mapPanelButton;
}
}
public sealed partial class MainPage : Page
{
public void mapPanelService(object sender, RoutedEventArgs e)
{
MapPanel.Margin = new Thickness(0,0,0,0);
}
}
Main problem was bad type of handler, which demanded something else than it should.
Correct code:
public void dataPanelService(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
.
.
.