Showing posts with label iOS. Show all posts
Showing posts with label iOS. Show all posts

Tuesday, August 05, 2014

Auto binding of outlets in Xamarin iOS and MvvmCross

 

If you are like me and you don’t like doing the bindings in code, you start thinking about solutions.

So I’m experimenting with the idea of creating the bindings at runtime, based on a simple name convention:

  • An UIButton outlet with the name 'btnXXX' is bound to the view-model 'XXXCommand' property.
  • An UITextField outlet with the name 'txtXXX' is bound to the view-model 'XXX' property.
  • An UILabel outlet with the name 'lblXXX' is bound to the view-model 'XXX' property.

For example, if you have a UIButton outlet called btnLogin, it's bound to view-model LoginCommand property.

I uploaded the code here;

https://github.com/nitescua/AutoBinding/

This can be done for other platforms as well.

Monday, April 07, 2014

A busy MvxViewController for MvvmCross + iOS

In my MVVMCross apps, in the shared PCL core library, I usually have a MvxViewModel derived class called ViewModelBase with a IsBusy property:


public class ViewModelBase : MvxViewModel
{
bool isBusy;
public bool IsBusy
{
get { return this.isBusy; }
set { if (this.isBusy != value) { this.isBusy = value; this.RaisePropertyChanged("IsBusy"); } }
}

// other base stuff in ViewModelBase
}
The property is to update a progress indicator control in the view.
For iOS the control can be UIActivityIndicatorView.

But instead of placing a UIActivityIndicatorView on each view, we can dynamically create it at run-time when IsBusy property changes to true.
We can have this implemented in a ViewController base class like this:

using System;
using System.ComponentModel;
using MonoTouch.ObjCRuntime;
using MonoTouch.UIKit;
using Cirrious.MvvmCross.Touch.Views;
using Cirrious.CrossCore.WeakSubscription;
using YourApp.Core.ViewModels;

namespace YourApp.Touch
{
public abstract class MvxViewControllerBase : MvxViewController
{
// weak subscription to NotifyProperty event
IDisposable npSubscription;

public override void ViewDidLoad()
{
base.ViewDidLoad();

// subscribe to view-model's PropertyChanged
this.npSubscription = ((INotifyPropertyChanged)this.ViewModel).WeakSubscribe<bool>("IsBusy", (s, e) => { this.UpdateActivityIndicatorView(); });

// at this point view-model exists, so update the indicator view
this.UpdateActivityIndicatorView();

// add other common UIViewController stuff
}

protected override void Dispose(bool disposing)
{
if (disposing && this.npSubscription != null)
{
this.npSubscription.Dispose();
this.npSubscription = null;
}
base.Dispose(disposing);
}

void UpdateActivityIndicatorView()
{
// get the activity indicator
var activityIndicatorView = (UIActivityIndicatorView)this.View.ViewWithTag(1000);

var vm
= (ViewModelBase)this.ViewModel;
if (vm.IsBusy)
{
// show busy indicator. create it first if it doesn't already exists
if (activityIndicatorView == null)
{
activityIndicatorView
= new UIActivityIndicatorView(this.View.Frame)
{
ActivityIndicatorViewStyle
= UIActivityIndicatorViewStyle.Gray,
Tag
= 1000
};

this.Add(activityIndicatorView);
this.View.BringSubviewToFront(activityIndicatorView);
activityIndicatorView.StartAnimating();
}

// show the activity indicator
activityIndicatorView.Hidden = false;
}
else
{
// hide the activity indicator
if (activityIndicatorView != null)
{
activityIndicatorView.Hidden
= true;
}
}
}
}
}
Instead of having a base class we can delegate the implementation to an extension class which has the same implementation.

Please let me know if you see any possible issues.

I am thinking to do a similar implementation for other platforms as well.
Maybe MVVMCross could support out of the box a similar implementation. A built in implementation in MVVMCross would probably require to be able to override the style for the indicator.

Thursday, September 12, 2013

identify iPod model

Look on the back of the iPod and read the number "Model No. AXXXX"
Mine says A1367.
Then google the model number.

Monday, August 26, 2013

Xamarin iOS app setup intro (Visual Studio)


To make the Visual Studio build iOS apps, you need to install Xamarin Studio on the Mac and possibly update your XCode version. Follow the requirements and instructions from http://docs.xamarin.com/guides/ios/getting_started/introduction_to_xamarin_ios_for_visual_studio
After all setup is done, in Visual Studio create a iOS \ Universal app.
Make sure you have Solution Platforms combo-box available in the standard command bar, otherwise click on the overflow button and choose Add\Remove buttons, and choose Solution Platforms. This combo-box should show iPhone and iPhoneSimulator options.
To connect to the mac machine, I am using VNC viewer: http://www.realvnc.com/download/viewer/
In Visual Studio, running the app you might get an error on the mac: “The simulated application quit.” and an option to switch SDK.
Here’s the things I did to make it work:
- make sure you have Application name (‘HelloWorld_App1’), Identifier (‘com.helloworldapp’), Version (1) and Deployment Target (6.1) set.
- if you get the error, right click and select to dock the simulator.  I was able to select ‘Reset settings’ from the menu just before the error dialog appeared. If not try to delete ~/Library/Application Support/iPhone Simulator directory on your Mac



Other info on the iOS setup:

http://forums.xamarin.com/discussion/comment/6084/#Comment_6084
http://blogs.endjin.com/2013/05/xamarin-platform-setup-gotchas/