Showing posts with label iPhone. Show all posts
Showing posts with label iPhone. Show all posts

Wednesday, December 16, 2015

Kill a running iPhone App

Problem:

We had a situation in an app where the user should not be able to proceed without accepting a condition.  We had the view prompt them when they chose to exit.  The method called Thread.Abort() which lead to an error report.

Solution:

Using the System library, call the CloseMainWindow().  This will gracefully kill the app and return control back to the base OS.

system.diagnostics.process.getcurrentprocess().CloseMainWindow()

Source:

http://forums.xamarin.com/discussion/12026/close-an-application-programmatically
http://stackoverflow.com/questions/3622120/monotouch-reuse-uialertview

Monday, August 17, 2015

MVVMCross Core solution throws error - Unhandled managed exception

Problem:

I just completed the Windows Phone UI project for an MVVMCross solution.  The core project is used by iPhone and Windows Phone UI.  Upon completion of testing for WP, I decided to run a few tests against iPhone which I had completed earlier.  I was immediately confronted with an error -

"An Unhandled exception occurred"

The Xamarin log shows the error occurring at load time.  The compiler built out the bits.  The attempt to run in debug did reveal some more interesting leads. 

[3641:134567] Unhandled managed exception: Could not load file or assembly 'Windows' or one of its dependencies.  The system cannot find the file specified.  (System.IO.FileNotFoundException)

I continued to play around with the changes I had made to the Core PCL.  Then I found the issue.

Solution:

In the course of building the Windows Phone project I used the LongListSelector. This Windows Phone control has several dependcies which are specific to Windows Phone.  The Core PCL included some code which depended on the library Microsoft.Phone.dll.  This reference is not compatible with the Monotouch framework.  My code worked fine when I compiled to Windows Phone but as soon as I tried to run the exe to Monotouch I raised the error. 

My solution was to surround the code with conditional compilation commands

#if USE_MVVMCROSS_WP8

....

#endif

Source:
http://typecastexception.com/post/2012/08/18/Visual-Studio-Use-Conditional-Compilation-to-Control-Runtime-Settings-for-Different-Deployment-Scenarios.aspx

Friday, June 26, 2015

How to add strikeout/strikethrough to an UITextView

Problem
Had a number of UITextView controls on my Xamarin/C# screen.  Customer asked for one of them to be underlined.  I was unsure of how to accomplish this.

Solution
Fortunately, Guillermo over at Xamarin Forums had a similar solution for UILabel.  Here is my reworked solution.

UITextView aboutTextView = new UITextView(new RectangleF(30,225,280,80));
aboutTextView.TextColor = UIColor.Black;
var attrString = new NSAttributedString("The Text", underlineStyle: NSUnderlineStyle.Single);
aboutTextView.AttributedText = attrString;
aboutTextView.Font = UIFont.SystemFontOfSize(14.0f);
Add(aboutTextView);



Source:
http://forums.xamarin.com/discussion/11584/how-to-add-strikeout-strikethrough-to-an-uilabel

Wednesday, February 4, 2015

iOS UISegmentedControl custom binding in MVVMCross

Working on a UI screen for an MVVM view using the UISegmented Control.  My view needed to handle the SelectedSegment event which is not part of the MVVMCross libraries.  The good news is that MVVMCross Framework is extensible.  Stuart Lodge did 2 of his N+1 series on this particular subject.  There was some confusion on my part about how exactly to do this until I hit on this stackoverflow post.  The author provided the target binding for the Segmented control with enough hints to get me to the working solution.

Solution:
Create the file MvxUISegmentedControlSelectedSegmentTargetBinding.cs and place it in the Touch project:

1.)MvxUISegmentedControlSelectedSegmentTargetBinding .cs

public class MvxUISegmentedControlSelectedSegmentTargetBinding : MvxPropertyInfoTargetBinding<UISegmentedControl>
{
    public MvxUISegmentedControlSelectedSegmentTargetBinding(object target, PropertyInfo targetPropertyInfo)
        : base(target, targetPropertyInfo)
    {
        this.View.ValueChanged += HandleValueChanged;
    }

    private void HandleValueChanged(object sender, System.EventArgs e)
    {
        var view = this.View;
        if (view == null)
        {
            return;
        }
        FireValueChanged(view.SelectedSegment);
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.TwoWay; }
    }

    protected override void Dispose(bool isDisposing)
    {
        base.Dispose(isDisposing);
        if (isDisposing)
        {
            var view = this.View;
            if (view != null)
            {
                view.ValueChanged -= HandleValueChanged;
            }
        }
    }
}

2.)Touch project Setup.cs file needs to register the target factories

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
        registry.RegisterPropertyInfoBindingFactory(typeof(MvxUISegmentedControlSelectedSegmentTargetBinding), typeof(UISegmentedControl), "SelectedSegment");
}
The key here is that the 3rd argument must be match the SelectedSegment property in the UISegmentedControl.

3.)Core project ViewModel needs to include an integer property to bind against the SelectedSegment property. 

ViewModel

private int _selectedSegment

public int SelectedSegment
{
get {return _selectedSegment;}
set
{
   _selectedSegment = value;
   RaisePropertyChanged(); => SelectedSegment;
}


4.)  Touch project View needs to bind the SelectedSegment in the ViewModel to the SelectedSegment in the Mvx control

View Model

public override void ViewDidLoad()
{
....
var segmentControl = new UISegmentedControl();
....
var set = this.CreateBindingSet<View,ViewModel>();

set.Bind(segmentControl).For(sm=>sm.SelectedSegment).To(vm=>vm.SelectedSegment);
...


Source:

1.)mvvmcross-and-uibutton-selected-uisegmentedcontrol-bindings-ios
2.)Stuart Lodge's github repo
3.)MVVMCross N+1 N28 (Custom Binding)
4.)MVVMCross N+1 N19 (Custom Control)
5.)

Wednesday, October 15, 2014

MVVMcross mvxException failed to construct and initialize viewmodel check MvxTrace fo more information

Problem:

We are trying to construct the iOS layer to our MVVMcross project.  We have built the core for the initial ViewModel and wanted to add the iPhone layer.  The project when in just fine and the Mac Host even compiled everything. 

Once I ran the solution, I get this runtime error in the iPhone Simulator.

Solution:

Lots of great information about possible solutions at stackoverflow.  However, my problem was the SQLite plugin.  I neglected to add the plugin to my iPhone Layer and once I added it the app runs fine.


Source:
http://stackoverflow.com/questions/18940820/how-do-you-initialize-themvvmcross-sqlite-plugin