Saturday, January 25, 2014

Type-safe ViewModel to ViewModel navigation in MvvmCross


In MvvmCross you can navigate between view-model's as described in the Wiki:
https://github.com/MvvmCross/MvvmCross/wiki/ViewModel--to-ViewModel-navigation

It's good however to use a simple type-safety mechanism.
Suppose we have a ProductViewModel which expects two arguments for initialization:

public class ProductViewModel : MvxViewModel
{
     public void Init(int productId, int clientId)
     {
          ....
     }
}

With a type-safe mechanism, instead of writing:

ShowViewModel<ProductViewModel>(new { productId = pid, clientId = cid });

we can write:

NavigationHelper.ShowProduct(pid, cid);

where NavigationHelper is a simple utility class:

using Cirrious.CrossCore;
using Cirrious.MvvmCross.ViewModels;
using Cirrious.MvvmCross.Views;
using MyApp.Core.ViewModels;
using Cirrious.MvvmCross.Platform;

namespace MyApp.Core.Common
{
    public static class NavigationHelper
    {
        public static void ShowProduct(int productId, int clientId)
        {
            ShowViewModel<ProductViewModel>(new { productId = productId, clientId = clientId });
        }

        static void ShowViewModel<T>(object parameter) where T : IMvxViewModel
        {
            var viewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
            var request = MvxViewModelRequest.GetDefaultRequest(typeof(T));
            request.ParameterValues = ((object)parameter).ToSimplePropertyDictionary();
            viewDispatcher.ShowViewModel(request);
        }
    }
}

using Cirrious.MvvmCross.Platform; namespace statement is important because it contains the ToSimplePropertyDictionary() extension method implemented by MvvmCross.

Furthermore, instead of using the NavigationHelper class, we can move the ShowViewModel method to a ViewModelBase class:

public class ViewModelBase : MvxViewModel
{
       protected static void ShowViewModel<T>(dynamic parameter) where T : IMvxViewModel        
       {            
           var viewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();            
           var request = MvxViewModelRequest.GetDefaultRequest(typeof(T));            
           request.ParameterValues = ((object)parameter).ToSimplePropertyDictionary();            
           viewDispatcher.ShowViewModel(request);        
       }
}

public class ProductViewModel : ViewModelBase
{
    public static Show(int productId, int clientId)
    {
        ShowViewModel<ProductViewModel>(new { productId = productId, clientId = clientId });
    }

    public void Init(int productId, int clientId)
    {
        ...
    }
}

and we can now write:

ProductViewModel.Show(pid, cid);

Having the Show method near the Init method in the ViewModel class, makes it a bit more easier to maintain the parameters.

Saturday, January 18, 2014

ASP.NET errors

 

#1. There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined

Check .NET Framework version for app pool.

Projects developed in (2.0,3.0,3.5) should run under an Application Pool with .NET FRAMEWORK VERSION v2.0.50727 

Projects developed in 4.0 should run under an Application Pool with .NET FRAMEWORK VERSION v4.0.30319

#2. Could not load file or assembly 'System.Data.SQLite' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Make sure that "Enable 32 - Bit Applications" is set to false or true for the app pool, depending on System.Data.SQLite.dll

System.Data.SQLite.dll is a mixed assembly, i.e. it contains both managed code and native code. Therefore a particular System.Data.SQLite.dll is either x86 or x64, but never both.

#3. HTTP Error 404.3 - Not Found
The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

Requested URL
   http://localdev.site.com/Webservices/UserProviderService.svc

You need to run this: http://enginecore.blogspot.ro/2013/06/svc-handling-in-iis.html

Thursday, January 16, 2014

Invalid markup in XAML in Visual Studio 2013


If you get this error in designer when there’s clearly no error in your XAML:
- close all files open in Visual Studio
- close Visual Studio
- delete the .suo file near the .sln
- delete bin and obj folders

if still doesn't work you can try to delete all directories from:
%LocalAppData%\Microsoft\VisualStudio\12.0\Designer\ShadowCache

More info:
http://stackoverflow.com/questions/12871358/visual-studio-2012-xaml-designer-invalid-markup
http://forums.xamarin.com/discussion/10284/xamarin-new-pcls-w8-wp8-net45-xamarin-ios-xamarin-droid-plus-new-microsoft-bcl-terribly-broken

Monday, January 13, 2014

Installing Windows 8 and customization

1. Use Windows 7 USB/DVD download tool to create a Windows bootable USB drive

2. If you want to create a Windows x64 bootable image, you need to run the Windows 7 USB/DVD download tool from a x64 Windows computer.

3. During the Windows 8 installation if you encounter error while installing \ copying files, and it’s at the same point (same percent), then the USB image is corrupted.

4. If doing Android dev. with Intel x86 Emulator Accelerator (HAXM), you must disable Hyper V in Windows.
disable: bcdedit /set hypervisorlaunchtype off
re-enable (restart): bcdedit /set hypervisorlaunchtype auto
5. For Samsung 840 Pro SSD: Install Samsung Magician
     http://www.samsung.com/global/business/semiconductor/samsungssd/downloads.html
It will start on each Windows start but it’s annoying it shows UAC. One solution is to remove it from startup

C:\users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
6. There are a number of SSD optimizations to be done
http://www.thessdreview.com/ssd-guides/optimization-guides/the-ssd-optimization-guide-ultimate-windows-8-edition/

7. Disable Windows Narrator. it’s annoying it starts anytime Windows Key + Enter are pressed.
http://enginecore.blogspot.ro/2013/09/disable-windows-narator.html
8. Make Word to minimize to tray: right click on tray icon and choose “Hide when minimized
9. Automatically user login
1. run netplwiz 
2. uncheck the “Users must enter a user name and password to use this computer”, click Apply
3. enter username and password
http://www.howtogeek.com/112919/how-to-make-your-windows-8-computer-logon-automatically/
10. Force Visual Studio always run as admin
http://stackoverflow.com/questions/12257110/can-you-force-visual-studio-to-always-run-as-an-administrator-in-windows-8

In Windows 8, you have to right-click devenv.exe and select "Troubleshoot compatibility".
  1. select "Troubleshoot program"
  2. check "The program requires additional permissions"
  3. click "Next", click "Test the program..."
  4. wait for the program to launch
  5. click "Next"
  6. select "Yes, save these settings for this program"
  7. click "Close"
11. On Windows 8, make sure you get the latest Intel x86 Emulator Accelerator (HAXM), At this moment, the one from Android SDK manager is buggy on Windows 8. 
http://software.intel.com/en-us/articles/intel-hardware-accelerated-execution-manager/
 

Sunday, December 29, 2013

Windows Store app development - important things to remember


1. Pages are by default destroyed when navigating from them. This is in contrast to Windows Phone where pages are cached.
2. When your app moves out of the foreground, Windows waits for a few seconds to allow quick switching back to the app, then tries to suspend the app.
This is very nicely described here:
http://blogs.msdn.com/b/windowsappdev/archive/2012/04/10/managing-app-lifecycle-so-your-apps-feel-quot-always-alive-quot.aspx
If your app doesn’t return from its suspending event handler within 5 seconds of receiving the suspending event, Windows will terminate it. It’s important not to do any heavy operations in your suspending event handler. Save your app data and return quickly.
3. When running app with debugger, the application is not suspended by the OS when it’s moving out of foreground. You need to use Suspend \ Resume menu in Visual Studio
More info:
http://msdn.microsoft.com/en-us/library/windows/apps/hh974425.aspx
http://blogs.msdn.com/b/windowsappdev/archive/2012/04/10/managing-app-lifecycle-so-your-apps-feel-quot-always-alive-quot.aspx
4. MVVMCross doesn’t play nicely with the generated SuspensionManager
In App.xaml.cs, I removed SuspensionManager:
private void OnSuspending(object sender, SuspendingEventArgs e)
{
           var deferral = e.SuspendingOperation.GetDeferral();
            //await SuspensionManager.SaveAsync();
            deferral.Complete();
}


5. Windows App Certification Kit fails on Suspend
Following error can be thrown while no real suspend problem is present in the code:

Performance suspendError Found: The performance suspend test collected the following results:
Application Error: Application Suspend was not detected for application App. This could be because your application failed to suspend correctly. Please consider re-running the test avoid interacting with the application while tests are running. 
Make sure you have Visual Studio running under admin privileges.
More info: http://endurasoftware.wordpress.com/2013/09/12/windows-app-certification-kit-fails-on-suspend/

6. Use Simulator to create screenshots for certification and check app in different resolutions.

7. How to create package and send it to customer and let him upload to his Windows Store account
The package needs to be created using the certificate generated with the customer’s Windows Store account.
For this, customer needs to first create a dummy app on his machine using Visual Studio. Suppose the customer reserves the ‘My App’ app name in the Windows Store:
1. Make a Windows Store app in Visual Studio (Blank App is fine), called MyApp
2 Once created, right click on the project and from context menu, choose Store \ Create App Packages ...
3. Choose Yes, click Next and sign in with the Windows Store account
4. In the list with reserved names, select select "My App" name
5. Click Create
6. Close dialog 'Package Creation Completed'
The customer needs to archive and send the whole project folder.








On my machine, in Visual Studio, double click on ‘Package.appxmanifest’ and in ‘Packaging’ tab click on Choose Certificate button and select the .pfx sent by the customer.
Make sure the ‘Package name’ and ‘Publisher display name’ corresponds to those from the app project generated by the customer.

Wednesday, December 11, 2013

Taking a screenshot on different mobile platforms


Button combination Screenshot location Link to official support
iOS Menu button + Power \ Lock Photos app \ depending on device: "Saved Photos" or "Camera Roll
Android Power \ Lock + Volume Down Gallery https://support.google.com/nexus/answer/2811098?hl=en
Windows Phone Start (Windows flag) + Power \ Lock Photos hub \ Screenshots album http://www.windowsphone.com/en-us/how-to/wp8/photos/take-a-screenshot


Emulator/Simulator:

Android
Goto 'C:\Users\[User]\AppData\Local\Android\android-sdk\tools
open ddms.bat or monitor.bat
Select the device.
In DDMS: Use Device menu\Screen Capture
in Monitor: Click on the photo camera icon from the toolbar.