Use variable value as x:Name - c#

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.

Related

Can't find variable for button inside if/else

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.

C# XNA if-else and defaulting to first value

So I am developing a custom ButtonGUI class for my game. Here's the initialization of the button object:
// Button code:
ButtonGUI btn1 = new ButtonGUI("Button 1", new Rectangle(150, 300, (int)myFont.MeasureString(menuButtons[0]).X, (int)myFont.MeasureString(menuButtons[0]).Y), myFont, Color.CornflowerBlue);
Now consider this code:
// Draw() method:
btn1.Draw(spriteBatch);
if (btnHover)
{
btn1.btnRect = new Rectangle(140, 300, (int)hoverFont.MeasureString(menuButtons[0]).X, (int)hoverFont.MeasureString(menuButtons[0]).Y);
btn1.btnFont = hoverFont;
btn1.btnColour = Color.Red;
}
else
{
btn1.btnRect = new Rectangle(150, 300, (int)myFont.MeasureString(menuButtons[0]).X, (int)myFont.MeasureString(menuButtons[0]).Y);
btn1.btnFont = myFont;
btn1.btnColour = Color.CornflowerBlue;
}
This would be OK if I had only 1 button... But if I have like 10 buttons or more? This really isn't what DRY suggests. I feel like I'm missing something, there must be a way to return button properties to their default values once the condition is no longer met without doing the whole thing manually, or is there? Thanks in advance!
It may make sense to create a structure to hold all of the values that may change.
class ButtonData
{
// put members corresponding to each member of ButtonGUI you wish
// to change
}
class ButtonSwapper
{
ButtonGUI myButton;
ButtonData hoverData;
ButtonData notHoverData;
void change(bool hover)
{
ButtonData dataToUse = hover ? hoverData : notHoverData;
// set each relevant member of myButton to its pair in
// dataToUse
}
}
then call change as necessary.

Call self in object construction

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;
}

Why selecting text in the TextBlock throws exception in WinRT

The following code snippet throws a System Exception.
TextBlock selectionText = new TextBlock();
selectionText.IsTextSelectionEnabled = true;
selectionText.Text = "Hello world";
selectionText.Foreground = new SolidColorBrush(global::Windows.UI.Color.FromArgb(255, 255, 0, 0));
selectionText.SelectAll();
What is wrong in my code?
Thanks in advance
You should make sure selectionText is displayed before calling SelectAll() on it, i.e. you should add it to a panel inside your current page:
TextBlock selectionText = new TextBlock();
selectionText.IsTextSelectionEnabled = true;
selectionText.Text = "Hello world";
selectionText.Foreground = new SolidColorBrush(global::Windows.UI.Color.FromArgb(255, 255, 0, 0));
MainPanel.Children.Add(selectionText);
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, selectionText.SelectAll);
Notice two changes:
The call to MainPanel.Children.Add() where MainPanel is the name of a panel control on your page.
selectionText.SelectAll() called via Dispatcher to make sure selectionText is actually added to the panel before the call executes.
My guess is the forgrund color
Color.FromArgb(255, 255, 0, 0)
The first number desides the level of "Alpha" which means transparancy. Windows RT may not be able to handel that.
Try this instead
Color.FromArgb(0, 255, 0, 0)

Extend UIButton in monotouch

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://

Categories