开发者

Setting button color in MonoTouch.Dialog

I have the following code:

[OnTap ("Account")]
[Alignment (UITextAlignment.Center)]
[Entry ("Create ScanDo! Account")]
public string Login;

And I'd like to set the Cell background color dynamically, based on the contents of another field and then after the button is clicked. Could anyone poin开发者_Go百科t me in a direction with some samples?

Thanks, Rick


The answer I came up with:

btnLogin = new StyledStringElement("", delegate {Account();})

To define the object, add it to the RootElement, then:

btnLogin.BackgroundColor = UIColor.Green;

To set the color! This method let me set color, font, size and caption.

Great work Miguel, Thanks!


As you're adding the button to the root collection you can set the colour. Just as you set the elements of a section.

Root = new RootElement("First Section") {
    new Section ("Test"){
        new StyledStringElement("Login", delegate { Account(); })
        {
            BackgroundColor = UIColor.Green
        }
    }
}


I don't like to keep pimping my projects but in this case it is the best option for you.

Check out https://github.com/RobertKozak/MonoMobile.MVVM

My project started out as adding Databinding support to MonoTouch.Dialog but has grown into a much bigger framework that is much easier to use than MonoTouch.Dialog.

Using MonoMobile.MVVM the code to do what you want looks like this:

public class ButtonView : View, INotifyPropertyChanged
{
  private UIColor ButtonColor = UIColor.Red;

   [Button]
   [Bind("ButtonColor", "BackgroundColor")]
   public void Test()
   {
       ButtonColor = UIColor.Green;
       PropertyChanged(this, new PropertyChangedEventArgs("ButtonColor"));
   }

   public event PropertyChangedEventHandler PropertyChanged = (s,e)=>{};
}

There are better ways to accomplish this using a proper ViewModel but this will work as listed (I tested it before I typed it in here.)

MonoMobile.MVVM is still in beta but there is enough there to get you going. It should be in full release status in the next couple of weeks after I finish up implementing INotifyCollectionChanged and adding a few more bindings for various Element properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜