Showing posts with label Monotouch. Show all posts
Showing posts with label Monotouch. 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

Wednesday, October 21, 2015

HTTP Client PostAsync Fails in MonoTouch/iPhone

Problem:
Running tests on our cross platform solution and encountered an issue. 

I get an exception thrown on my call to postasjsonasync(). The exception says "A type load exception occured"

One step in our app sends an email out of the app using the HTTPClient libraries from MS.  This is known to cause issues.  I hit the boards stackoverflow, Xamarin, etc..  This is a fairly common problem.  The crazy thing is that we are using this call in our pcl. It works with no issues on Android and Windows Phone.  It is a problem on iOS.  It appears to be a mapping issue in the PCL.

Solution:
James Montemagno did come up with a solution. The issue with the error was assembly binding error.  The iOS version was binding to the native Windows System.Net.Http for .Net 4.5. 

We should be binding to the Xamarin version which is at

C:\Program Files(x86)\Reference Assemblies\Microsoft\Framework\Xamain.iOS\v1.0

Why?
"The reason for the redirect is that the Microsoft.Net.HttpClient packages contains the System.Net.HttpClient namespace but that already exists on iOS and Android (with slightly different features even). At runtime it will resolve to the wrong assembly and not use the (Xamarin-)native iOS one." (Thanks James!)


You can fix this problem by modifying the app.config in the Touch project.
 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.0.5.0" />
        </dependentAssembly>
    </assemblyBinding>



Source:
http://stackoverflow.com/questions/27521738/httpclient-failing
https://github.com/Krumelur/HttpClientTest/blob/master/HttpClientTest_iOS/app.config

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

Thursday, May 7, 2015

How to Wrap Text in a Monotouch/Xamarin UILabel

Doing some UI work on our iPhone app and still trying to remember what the UI supports.
I had an issue with how to wrap text in a UILabel field.  Fortunately, Xamarin forums has some great recipes.

What I needed was (UILineBreakMode.WordWrap)

Solution

var descriptionField = new UILabel(new RectangleF(10, 45, 300, 140));
descriptionField.LineBreakMode = UILineBreakMode.WordWrap;
descriptionField.Lines = 10;
descriptionField.BackgroundColor = UIColor.Gray;
Add(descriptionField);


Source:
http://developer.xamarin.com/recipes/ios/standard_controls/labels/uilabel-truncate-wrap-text/

Friday, March 13, 2015

MVVMCross iOS: How to trigger RowSelected Table View?

I was struggling with how to raise an MVVM event on a click within a tableview.
Fortunately, Stuart Lodge has come through with a great demo of how this all works.
His N+17 video about 47 minutes in gives the details for iOS.  He also has a demo
in his MVVM samples library.  I lifted the pertinent code out of the repo.  The key is to bind
your MVVM command to the SelectionChangeCommand which is a property of the TableView
row item.

Sample:

ModelView:

new Cirrious.MvvmCross.ViewModels.MvxCommand<DilbertItem>((dilbertItem) =>
{   
// note that we can only pass an id here - do *not* serialiase the whole dilbertItem ShowViewModel<DilbertDetailViewModel>(new { id = dilbertItem.Id });
});


View:

// binding
set.Bind(source) .For(s => s.SelectionChangedCommand) .To(s => s.ShowDetailCommand);



Source:
N=17 https://www.youtube.com/watch?v=h0Eww89c9DM&feature=youtu.be&t=47m30s

http://stackoverflow.com/questions/17712589/mvvmcross-ios-how-to-trigger-rowselected-on-a-custom-tableviewsource-using-crea

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, December 10, 2014

Invalid IL code in system.xml.linq.xdocument:Parse (string): method body is empty

Working on getting a iPhone application off the ground.  We are using Xamarin/Monotouch and MVVMcross to build a cross platform solution.  The PCL does not support File.IO namespace so using some excellent posts about IoC.

Problem:

An issue cropped up in my attempts to get my example working.  I have the xml file being rendered in the iOS project of the solution.  The Interface is stored in the PCL and it compiles and runs.  But when I hit the line in the iOS project

doc = XDocument.Parse(DefaultData);

I got the runtime error:
Invalid IL code in system.xml.linq.xdocument:Parse (string): method body is empty

Solution:

The iOS project is using the Monotouch namespace which includes its own copies of the common
.Net dlls.  It appears that the namespaces for the non mono projects were included as references for the iOS project.  This meant that the source code would compile without errors.  The dll in question was System.XML.dll.  I dropped the reference to this dll and added in the correct mono dll and that removed the IL error.


Wrong System.XML.dll file

Program Files(x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.XML.dll

vs.


Correct System.XML.dll

Program Files(x86)\Reference Assemblies\Microsoft\Framework\MonoTouch\v1.0\System.XML.dll

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