Friday, August 7, 2015

Change Grid/Button Background color through MVVM pattern in Windows Phone

Problem:


Having completed an iPhone app using an MVVMCross core backend.  I need to create the windows phone version UI for the backend.  The iPhone app uses the SegmentControl on a view.  The iPhone app works great and seemlessly with the core.  Now windows phone does not have a segement control per se.  You could simply use a hub/tiles or panorama control templates.  The customer however is very enamored of  the look an feel of the Segment Control.  So it was off too Xaml and blend to build out a similar control.  This I accomplished pretty quickly. 

The issue is the binding to the modelview.  Their is no command property on the grid; however the button control does.  So how to get the interaction to bind with the model view?  Fortunately we have the iValueConverter in the UI layer.  This allows us to bind to properties in the xaml controls.  This combined with model view can allow us to get the functionality to match the segmentcontrol.

Solution:


modelview

private Cirrious.MvvmCross.ViewModels.MvxCommand _myCommand
public ICommand MyCommand
{
             get
            {
                  _myCommand = _myCommand ?? new Cirrious.MvvmCross.ViewModels.MvxCommand(DoMyCommand);
                   return _myCommand;
            }

}

private void DoMyCommand()
{
             ISActive = true;
}


private bool _isActive;

public bool ISActive
{
         get
         { return _isActive;}
         set { _isActive = value;
             RaisePropertyChanged()) => ISActive);
        }

}

view (xaml)

xmlns:phone="clr-namespace:Microsoft.Phone.Controls..."
xmlns:local="clr-namespace:MyNamespace"

<phone:PhoneApplicationPage.Resources>
<local:BrushColorConverter x:Key="ColorConverter" />
</phone:PhoneApplicationPage.Resources>
......

<Grid
Background={Binding ISActive Converter={StaticResource ColorConverter}}
....>
<Button
Background={Binding ISActive Converter={StaticResource ColorConverter}}
Command="{Binding MyCommand}"
.....
/>

</Grid>


(BrushColorConverter.cs (separate class file) in UI project)
 public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return new SolidColorBrush(Colors.Transparent);
        }
        return System.Convert.ToBoolean(value) ?
            new SolidColorBrush(Colors.Red)
          : new SolidColorBrush(Colors.Transparent);
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}




Source:
  1. http://stackoverflow.com/questions/7329812/change-button-background-color-through-mvvm-pattern-in-wpf
  2. http://www.codeproject.com/Articles/758656/Use-Converters-in-your-Windows-Phone-Apps

Wednesday, August 5, 2015

Windows Phone disable status bar

Problem:
Trying to fix a problem on a Windows Phone Silverlight 8.1 app today.  The app starts with a splash screen and do to some extra processing it has an extended screen which simply displays the same screen again.  The issue arises when the splash screen which the OS handles switches over to the first view which also displays the same splash screen image.  Due to the status bar displaying on the first screen the image shifts down.  This leads the image dropping down vice staying in place.  The transition from needs to be smooth without any resizing of the image.  The solution seems to be simply to hide the status bar on that screen.  But how to do it?

Solution:

Windows Phone 8 / Windows Phone 8/8.1 Silverlight


Code behind in ExtendedSplashScreenView.xaml.cs:

public partial class ExtendedSplashScreenView(): MvxPhonePage
{
  InitializeComponent();
  Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs events)
{
  SystemTray.IsVisible = false;
}


Windows Phone 8.1

Code behind in ExtendedSplashScreenView.xaml.cs:

public partial class ExtendedSplashScreenView(): MvxPhonePage

{
  // Get the Status bar for the Current Window.
  Windows.UI.ViewManagement.StatusBar statusBar =     Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
  // Hides the status bar
  await statusBar.HideAsync();
}


Source:
1.) Windows Phone 8 / Silverlight
  • http://stackoverflow.com/questions/6674627/how-to-hide-the-status-bar-in-wp7-silverlight
2.) Windows Phone 8.1
  • http://blogs.msdn.com/b/johnkenn/archive/2014/04/16/hiding-the-status-bar-in-windows-phone-8-1-apps-or-not.aspx
  •  http://developerpublish.com/how-to-hide-status-bar-in-windows-phone-8-1-xaml-app/
  •  http://blogs.msdn.com/b/amar/archive/2014/05/12/status-bar-in-windows-phone-8-1.aspx

Thursday, July 30, 2015

Unable to read the project file Sqlite.vcxproj

Problem:

 Working with Peter Huene's sqlite port for use with Windows Phone project.  I need to compile the bits for SQLite for the MVVMCross Sqlite libraries.  The project in nuget has support for Droid and Touch but the windows phone project is incomplete.  I downloaded the bits and attempted to open the C++ project and got this wonderful message:

 Unable to read the project file Sqlite.vcxproj

C:\Program Files\Microsoft SDKs\Windows Phone\v8.0\ExtensionSDKs\SQLite.WP80\3.8.6\DesignTime\Common Configuration\Neutral\SQLite.QP80.props not found

Solution:

This made me wonder and about other people with this problem.  Sure enough several people have had similar issues.  The project file checked into the GitHub for Peter's site.  They all resolve down to the version of the Windows Phone extension libraries for SQLite.  Depending on what version is installed.  I was able to resolve the problem by editing the vcxproj file with notepad.  I corrected the path references from 3.8.6 to 3.8.11 which is the version I have installed.  Easy enough and now it works.



Source:
1.) http://stackoverflow.com/questions/18692632/using-sqlite-for-a-windows-phone-8-app
2.)https://github.com/peterhuene/sqlite-net-wp8/issues/9
3.)http://wp.qmatteoq.com/working-with-sqlite-in-windows-phone-8-a-sqlite-net-version-for-mobile/


Thursday, July 16, 2015

plugin not registered for type Cirrious.MvvmCross.Community.Plugins.Sqlite - Unified API

Problem:



I just successfully published the app to the iTunes store.  I had the application written and ready to publish when the wonderful Application Loader refused to load my app to the store!!  Why, because
it was not built to support the Unified API.  Easy fix just use the Xamarin upgrade to Unified Api and voila!  But Unified API is not compatible with MVVMCross SQLite Plugin (v 3.2.2)

Wait, not so fast.  I immediately had problems with the SQLite libraries for MVVMCross. 
This caused me about a week of pain working through all the interdepencies.  I was able to get the latest source code from https://github.com/MvvmCross/MvvmCross-SQLite/issues/39.  Then I built out the bits for support of my project.  I was able to compile and run the app.  It would crash each time I ran and attempted to load the screen which loaded  the sql lite plugin!!!

Error:
plugin not registered for type Cirrious.MvvmCross.Community.Plugins.Sqlite

I wracked my brain as to why the ui failed each time on the sqlite load.  I finally reread the api documentation on the MVVMCross wiki.  It finally hit me when I came across this added requirement for the iOS platform plugin. 



Solution:

You must include a second argument in the bootstrap action for the sqllite plugin.  The bootstrap needs the second argument in the interface constructor for iOS.  If you fail to include it then the path to the Touch plugin then the runtime will not load the plugin and will give you the error.

This is not a problem in Droid or Windows Phone builds.

 
using Cirrious.CrossCore.Plugins; 
using Cirrious.MvvmCross.Community.Plugins.Sqlite; 
using Cirrious.MvvmCross.Community.Plugins.Sqlite.Touch; 


public class SqlitePluginBootstrap 
: MvxLoaderPluginBootstrapAction<Plugins.Sqlite.PluginLoader, Plugins.Sqlite.Touch.Plugin> 
{ }




Source:
  1. https://github.com/MvvmCross/MvvmCross/wiki
  2.  https://github.com/MvvmCross/MvvmCross-SQLite/issues/39
  3. http://stackoverflow.com/questions/30648697/mvvmcross-community-plugin-for-sqlite-with-unified-api 
  4.  http://blog.alectucker.com/post/2015/01/19/sqlite-error-with-xamarinios-unified-api.aspx
  5.  https://bitbucket.org/twincoders/sqlite-net-extensions

ERROR: ERROR ITMS-90047: "Disallowed paths ( "iTunesMetadata.plist" ) found at: MyNewTool.app"

Solution:

For command line or IDE builds with XamarinVS
  1. Ensure "Project Properties -> iOS IPA Options -> Include Artwork in IPA" is not checked. (Or manually set the BuildIpa and IpaIncludeArtwork properties as mentioned above.)
    image
  2. Build the app.
  3. Submit the .ipa file using Application Loader.
or

<Target Name="_CompileITunesMetadata" DependsOnTargets="_DetectSdkLocations;_DetectAppManifest;_GenerateBundleName;_CompileAppManifest">
  <Message Text="Skipping CompileITunesMetadata task to prevent inclusion of iTunesMetadata.plist in the IPA" />
</Target>


Source:
  1. https://forums.xamarin.com/discussion/40388/disallowed-paths-itunesmetadata-plist-found-at-when-submitting-to-app-store/p1
  2. https://discussions.apple.com/thread/6997898?start=0&tstart=0
  3. https://bugzilla.xamarin.com/show_bug.cgi?id=29180#c0

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

Tuesday, June 23, 2015

Localized Strings does not exist in namespace

Problem:
Having completed the iPhone version of our MVVM cross project, we are back to the Windows Phone version.  I created the baseline phone project and added reference to the Core.  Then I built the Windows Phone 8 project and kept getting this error:
The name "LocalizedStrings" does not exist in the namespace

Solution:
Close Visual Studio. Than, delete sub folders 10.0 and 11.0 from "%LOCALAPPDATA%\Microsoft\Phone Tools\CoreCon\" Open Visual Studio and rebuild solution.

Where
%LOCALAPPDATA% = C:\Users\[USER]\AppData\Local

Source:
http://stackoverflow.com/questions/23237549/vs-2013-the-name-localizedstrings-does-not-exist-in-the-namespace