Quantcast
Channel: OpenNETCF Consulting
Viewing all 21 articles
Browse latest View live

Book: Professional Microsoft Embedded CE 6.0

$
0
0

I was browsing around over lunch when I came across an upcoming title from Wrox that I thought might be interesting to someone out there.

The book, being writing by Samuel Phung of ICOP, is titled Professional Microsoft Embedded CE 6.0. A book like this is sorely needed as most of the knowledge exists primarily in the embedded newsgroup or scattered around in a handful of blogs.

The 20-chapter book will cover everything from creating an image to development in C# and VB.NET, as well as C++ and is scheduled to be released in November. Wrox have made a number of chapters available through their Wrox First subscription service.


Chat Transcript: May 13, 2008 - Smart Device Development Chat

$
0
0

A little later than I intended, but here is the transcript to Smart Device Development chat from May 13, 2008: Read it here

HOW-TO: Disable/Enable Network Connections Programmatically under Vista

$
0
0

I got an email last week asking about how to disable a particular network connection under Vista. The specific scenario, how to disable an active 3G connection, is not something I'm going to cover, but what I present below could be used as basis for that scenario.

With Vista, Microsoft introduced two new methods to the Win32_NetworkAdapter class under WMI: Enable and Disable. Before can call either of those methods, we need to know how to enumerate the network connections.

The .NET Framework SDK provides a helpful utility called mgmtclassgen.exe, which can be used to create .NET-friendly wrappers of the WMI classes. Open up a Visual Studio command prompt and enter the following:

mgmtclassgen Win32_NetworkAdapter -p NetworkAdapter.cs

This will generate a file called NetworkAdapter.cs which will contain a C# representation of the WMI Win32_NetworkAdapter class. You can add this source code file to your C# project and then access all the properties without too much extra effort.

To filter and disable the specific adapters, you do something like this:

SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach(ManagementObject result in search.Get())
{
    NetworkAdapter adapter = new NetworkAdapter(result);

    // Identify the adapter you wish to disable here. 
    // In particular, check the AdapterType and 
    // Description properties.

    // Here, we're selecting the LAN adapters.
    if (adapter.AdapterType.Equals("Ethernet 802.3")) 
    {
        adapter.Disable();
    }
}

Don't forget to add a reference to System.Management.dll!

Mark your calendars - Smart Device Development Chat, July 16.

$
0
0

The next Smart Device Development Chat will take place on MSDN at 10am PST on Wednesday, July 16.

Please join experts from the Windows Mobile, Windows CE, SQL Server CE and .NET Compact Framework communities in a chat around application development for smart devices. These chats are a great opportunity to have your questions answered by experts from around the world.

Add to Calendar

If you cannot attend the MSDN Chat, there is a permanent chat room available on the web and via IRC (irc://irc.freenode.net/mobiledev). Engage with your peers online today!

OpenNETCF is now a Microsoft Gold Partner

$
0
0

Over the last few months we've been working hard to attain the Gold Level partner status in the Microsoft Partner Program and I'm extremely pleased to say that we've done it! For our customers, this is an assurance that by choosing to work with OpenNETCF products and our consultants, you really are choosing a high calibre company.

Massive amounts of credit for Mark for driving this internally. Good job!

Lambda expressions in .NET Compact Framework 2.0

$
0
0

There's been a lot of internal discussion today on how to leverage C# 3.0 features without requiring .NET Compact Framework 3.5. Mark has the low down on using extension methods with a little compiler misdirection hack.

The discussions kicked off because I discovered that you can use lambda expressions with .NET Compact Framework 2.0 projects in Visual Studio 2008. I even twittered about it yesterday. (Note: it didn't take me too long to realize it's not a bug, but due to Visual Studio 2008 using the C# 3.0 compiler.) Code like the following just works -- no hacks required:

delegate double PowerOf(double x, double y);

static void Main() 
{
    PowerOf pwr = (x, y) => Math.Pow(x, y);
    double result = pwr(2, 3);
}

If you are using Visual Studio 2008 and still need to target .NET Compact Framework 2.0, the little things like this go a long way to creating and maintaining reusable code.

HOWTO: Prevent ResolveAssemblyReferences warnings in Visual Studio 2008

$
0
0

When you reference an assembly compiled against .NET Compact Framework 2.0 in a project targeting .NET Compact Framework, it's quite likely you will receive a number of ResolveAssemblyReferences warnings, similar to this:

ResolveAssemblyReferences:
  Consider app.config remapping of assembly "System.Windows.Forms, Culture=neutral, 
  PublicKeyToken=969db8053d3322ac, Retargetable=Yes" from Version "2.0.0.0" [] to 
  Version "3.5.0.0" [C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\
  WindowsCE\System.Windows.Forms.dll] to solve conflict and get rid of warning.

This warning occurs when the compiler has loaded the .NET Compact Framework 3.5 base class libraries (BCLs) and the referenced assembly, but the metadata contained in the referenced assembly references the .NET Compact Framework 2.0 BCLs.

To remove the warnings you can add an App.Config to your project and redirect the assembly bindings from the 2.0 versions to the 3.5 equivalents. Below is an example of the XML required in the App.Config to redirect the 2.0 version of System.dll to the 3.5 version.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentassembly>
        <assemblyidentity name="System" culture="neutral" publickeytoken="969db8053d3322ac" />
        <bindingredirect  newVersion="3.5.0.0" oldVersion="2.0.0.0" />
      </dependentassembly>
    </assemblybinding>
  </runtime>
</configuration>

You will need to add a dependentassembly element for each of the ResolveAssemblyReferences warnings that you receive.

Make sure the Build Action property of the App.Config is set to Content and the Copy To Output Directory property is set to Only if newer. Then, rebuild your solution and the warnings should not appear.

ANN: Smart Device Framework 2.3

$
0
0

Today, we released Smart Device Framework 2.3. This is the long awaited release of the Smart Device Framework for Visual Studio 2008.

It's been a painful path to get to this point, what with contending with frustrating bugs in Visual Studio 2008 forms designer and all, but we finally got there. Thanks for you patience and understanding.

Along with the new version is a new naming convention. We've dropped the name Smart Device Framework Extensions for Visual Studio with its Standard and Premium editions. Instead, say hello to Smart Device Framework Standard Edition and Professional Edition. Be sure to check out the new feature matrix to find out how each edition differs.

We've got some pretty cool stuff features in the libraries, notably OpenNETCF.Core which contains a dozen or so extension methods for common scenarios, as well as OpenNETCF.Net.Mail, for sending mail via SMTP directly from the device -- this eliminates the need to configure a mail account on the device.


HOWTO: Build the Smart Device Framework source code

$
0
0

Note: This blog post only applies to Smart Device Framework Standard or Professional Edition customers.
>

If you open the SDF Public Source solution that ships with Smart Device Framework 2.3 and then build the source, you'll come across the following error:

Friend access was granted to 'OpenNETCF,PublicKey=...', but the output assembly is 
named 'OpenNETCF, Version=2.3.0.21, Culture=neutral, PublicKeyToken=null'. Try 
adding a reference to 'OpenNETCF,PublicKey=...' or changing the output assembly name 
to match.

(Note that I've replaced the actual public key with ellipses in the above. You will see an extraordinarily long hexadecimal number in your errors list in Visual Studio.)

The easiest way to remedy this is through a quick search & replace. Hit Ctrl-H to bring up the Find & Replace window. In the Find what text box, enter the following:

,PublicKey=00240000048000009400000006020000002400005253413100040000010001002beeba3
bfe7c548e085cffb8c2b6fd61ddd02b06d70864bb7de8bb22473edf5ab4b2196ff98e232c3e87f11fd
7986b743d5d3fdd6ecaf624bacfed116e1cefa50cd652365371d0ebd2702eb1084fed46df79ac0f59f4
d66c547918613d56dcf106843f3458516d3cd26f057a346d9f645fc24a7410a095c754835916e13cdbe

Make sure the Replace with text box is empty and the Look in combobox is showing Entire Solution. Click the Replace All button. When the search & replace has completed, build the solution again and it will succeed.

If you wish to do this manually (you may only want to use 1 or 2 of the 16 projects), you will need to modify the AssemblyInfo.cs file for the following projects:

  • OpenNETCF
  • OpenNETCF.Configuration
  • OpenNETCF.Net
  • OpenNETCF.Windows.Forms
>

Get Padarn for free!

$
0
0

Last week we quietly launched a fantastic new promotion for our Padarn Web Server product:

Padarn is a lightweight, single-purpose web server that supports a growing subset of ASP.NET. It is written entirely in C# and can be used as stand-alone web server, or embedded into an existing application. Padarn can be used to create elegant, data-driven web sites using SQL Server Compact Edition, or interface with a whole myriad of hardware peripherals, such as web cams, sensors, controllers, etc.

If you would like more information on Padarn, please send an email to padarn@opennetcf.com.

HOWTO: Determine whether or not a library exports a function

$
0
0

Back in May 2008, I wrote a blog post about checking for Win32 methods. I was recently reminded of this when a customer emailed us about a scenario that exactly matched the intended use case.

The customer was using the Smart Device Framework with a Windows CE 5.0 device and was receiving a MissingMethodException "Can't find an Entry Point 'xxx' in a PInvoke DLL 'yyy'". The exception message is detailed enough to explain the situation -- since Windows CE is modular, whoever created the OS image had not added a required component, resulting in the native functions to be missing from coredll.dll. This is not an uncommon scenario in the Windows CE world.

Below is an example of how you can gracefully handle calling the PlaySound function from coredll.dll even on devices that don't support the function. The important part is the call to Device.Win32Library("coredll").HasMethod("PlaySound"). This is return false if coredll.dll does not exist or if coredll.dll does not export the PlaySound function. Of course, you could raise a PlatformNotSupportedException or try another mechanism for playing the sound file instead of just returning without calling the native PlaySound function. The important point is that you should code in a way that can handle scenarios where the native function is not available and adapt accordingly.

Imports System.Runtime.InteropServices
Imports OpenNETCF.Reflection

Partial Public Class MainForm
  Friend Class NativeMethods
    Private Const SND_ASYNC As Integer = &H1
    Private Const SND_FILENAME As Integer = &H20000

    Public Shared Sub SafelyPlaySound(ByVal soundFile As String)
      If Not Device.Win32Library("coredll").HasMethod("PlaySound") Then
        Exit Sub ' Function is not exported so we silently return
      End If

      ' Coredll exists and the PlaySound function has been exported
      ' so we can safely make the P/Invoke call
      PlaySound(soundFile, IntPtr.Zero, SND_ASYNC And SND_FILENAME)
    End Sub

     _
    Private Shared Function PlaySound(ByVal szSound As String, _
                                      ByVal hMod As IntPtr, _
                                      ByVal flags As Integer) As Integer
    End Function

  End Class
End Class

Getting Mono Process Info from a Mono App

$
0
0

Since a large amount of the work I tend to do is for embedded devices, and since I don’t like to have to visit deployed devices to restart an app whenever it may crash, a pretty common pattern I use is to create a watchdog application that periodically checks to see if the actual application is running and start it if it’s not.  It a bit more complex than that because I typically have support for intentional shutdowns and I like to log all restarts for diagnostics, but the general premise is pretty simple.  The app should always be running.  If it’s not, start it again.

In Mono (under Linux anyway) that task turns out to be a bit of a challenge.  Process.GetProcesses doesn’t work because rather than giving the actuall Application name like the .NET Framework does under Windows, Mono simply returns a Process with a ProcessName of “mono-sgen” for all of the Mono apps running.  I can’t differentiate between the Watchdog app, the target app and any other app that may or may not be running.

I ended up creating a new class I called LinuxProcess (for lack of a better name).  Ideally it would be a Process derivative, or even rolled back into the Mono source, but for now it’s stand-alone and feature-limited to what I needed for a Watchdog.  Full source is below (sorry about the length, but I prefer this over a zip, and it’s searchable and indexable).

using System;
using System.IO;
using System.Linq;

using Output = System.Console;
using System.Collections.Generic;

namespace System.Diagnostics
{
	public class LinuxProcess
	{
		private Process m_process;

        private LinuxProcess(string fileName)
        {
            m_process = Process.Start(fileName);
        }

		private LinuxProcess(int pid)
		{
			Id = pid;
			m_process = Process.GetProcessById(pid);

			if (m_process == null)
			{
				Output.WriteLine("GetProcessById returned null");
			}
		}

        public static LinuxProcess Start(string fileName)
        {
            return new LinuxProcess(fileName);
        }

        public bool HasExited
        {
            get { return m_process.HasExited; }
        }

		public void Kill()
		{
			m_process.Kill();
		}

		public static LinuxProcess[] GetProcessesByName(string processName)
		{
			return GetProcesses().Where(p => p.ProcessName == processName).ToArray();
		}

		public static LinuxProcess[] GetProcesses()
		{
			var list = new List<LinuxProcess>();

			foreach (var path in Directory.GetDirectories("/proc")) 
			{
				var d = Path.GetFileName(path);
				int pid;

				if (!int.TryParse(d, out pid))
				{
					continue;
				}
					
				// stat
				var stat = GetStat(pid);
				if (stat == null) continue;

				var proc = new LinuxProcess(stat.PID);
				proc.ProcessState = stat.State;

				// look for mono-specific processes
				if (stat.FileName == "(mono)")
				{
					// TODO: handle command-line args to the Mono app
					var cmdline = GetCommandLine(stat.PID);

					// cmdline[0] == path to mono
					// cmdline[1] == mono app
					// cmdline[1+n] == mono app args
					proc.ProcessName = Path.GetFileName(cmdline[1]);
				}
				else
				{
					// trim out the parens
					proc.ProcessName = stat.FileName.Trim(new char[] { '(', ')' });
				}

				list.Add(proc);
			}

			return list.ToArray();
		}

		private static Stat GetStat(int pid)
		{
			try
			{
				var statDir = string.Format("/proc/{0}/stat", pid);
				if (!File.Exists(statDir))
					return null;

				var proc = new LinuxProcess(pid);

				using (var reader = File.OpenText(statDir))
				{
					var line = reader.ReadToEnd();
					return new Stat(line);
				}
			}
			catch (Exception ex)
			{
				Output.WriteLine("Stat Exception: " + ex.Message);
				return null;
			}
		}

		private static string[] GetCommandLine(int pid)
		{
			// The command line arguments appear in this file as a set of null-separated strings, with a further null byte after the last string. 
			using (var reader = File.OpenText(string.Format("/proc/{0}/cmdline", pid)))
			{
				string contents = reader.ReadToEnd();
				var args = contents.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
				return args;
			}
		}

		public int Id { get; private set; }
		public string ProcessName { get; private set; }

		public ProcessState ProcessState { get; private set; }
	}

	internal class Stat
	{
		internal Stat(string procLine)
		{
			try
			{
				var items = procLine.Split(new char[] { ' ' }, StringSplitOptions.None);

				PID = Convert.ToInt16(items[0]);
				FileName = items[1];

				switch (items[2][0])
				{
					case 'R':
						State = ProcessState.Running;
						break;
					case 'S':
						State = ProcessState.InterruptableWait;
						break;
					case 'D':
						State = ProcessState.UninterruptableDiskWait;
						break;
					case 'Z':
						State = ProcessState.Zombie;
						break;
					case 'T':
						State = ProcessState.Traced;
						break;
					case 'W':
						State = ProcessState.Paging;
						break;
				}
			}
			catch (Exception ex)
			{
				Output.WriteLine("Stat parse exception: " + ex.Message);
			}
		}

		public int PID { get; private set; }
		public string FileName { get; private set; }
		public ProcessState State { get; private set; }
	}

	public enum ProcessState
	{
		Running, // R
		InterruptableWait, // S
		UninterruptableDiskWait, // D
		Zombie, // Z
		Traced, // T
		Paging // W
	}
}

Row filtering in the ORM

$
0
0

For a while now we’ve had an unwanted behavior in our Solution Engine product. The larger the on-device database got, the longer the app took to load. To the point that some devices in the field were taking nearly 5 minutes to boot (up from roughly 1 minute under normal circumstances). This morning we decided to go figure out what was causing it.

First, we pulled a database file from a device that is slow to boot. It turns out that the database was largely empty except to about 50k rows in a log table where we record general boot/run information on the device for diagnostics.

At startup the logging service pulls the last hour of log information and outputs it to the console, which has proven to be very helpful in diagnosing bad behaviors and crashes. Looking at the code that gets that last hour of data, we saw the following:

var lastHourEntries = m_store.Select<SFTraceEntry>(a => a.TimeStamp >= selectFromTime);

Now let’s look at this call in the context of having 50k rows in the table. What it effectively says is “Retrieve every row from the SFTraceEntry Table, hydrate a SFTraceEntry class for each row, then walk through that entire list checking the TimeStamp field. If the TimeStamp is less that an hour old, then copy that item to a new list and when you’re done, return the filtered list.” Ouch. This falls into the category of “using a tool wrong”. The ORM supports FilterConditions that, depending on the backing store, will attempt to decipher into a SQL statement, index walk or something more efficient than “return all rows”. In this case, the change was as simple as this:

var dateFilter = new FilterCondition("TimeStamp", selectFromTime, FilterCondition.FilterOperator.GreaterThan);
var lastHourEntries = m_store.Select<SFTraceEntry>(new FilterCondition[] { dateFilter });

Old CoreCon Remote Tool example

$
0
0

I’ve had a request for an old (~2006) CoreCon remote tool sample application Alex Feinman created. Thanks to a VHD backup, I was able to locate it and you can download it here: APP315_Demo

OpenNETCF Scanner Compatibility Library

$
0
0

Some days I think I have too much code “lying around”.  As you would expect from many years as a developer, I have utility libraries for all sorts of tasks.  Generally when I think something is likely to be useful for others I like to make it publicly available for anyone to use – just take a look at the list of Codeplex projects I admin.

This morning I saw a question on StackOverflow about intelligently detecting a platform and loading the proper binaries for it.  In this case it was specific to doing so with Windows Mobile barcode scanners.  I immediately thought, “hey, I have a library for that” and went to answer and give a link.  Except the link didn’t exist.  I never created the open source project for it, so the code has just been sitting here doing nothing.

Yes, this code is probably 5 years or more past it’s prime useful period due to the decline in use of Windows Mobile, but hey, I just used it on a project last week, so it’s got some life left in it.

So, here’s yet another open source library from my archive – the OpenNETCF Barcode Scanner Compatibility Library.


ORM Updates

$
0
0

Generally speaking, the OpenNETCF ORM library gets worked on offline.  We use it extensively in our Solution Engine product and so the “working” copy of the code resides on a separate source tree.  The good part of this is that it’s always getting tested, extended and fixed.  The down side is that I have to manually merge those changes back to the public source tree so it doesn’t happen probably as often as it should.

At any rate, I did a large merge this morning and there are some cool new features that have been added, along with a long list of bug fixes.

The general bug fixes and enhancements are largely SQL Server related, since we did a large SQL Server push for a customer this year, and the ORM benefitted from that.

The new features are twofold:

1. I added some rudimentary Connection Pool support.  The ORM itself can now keep a pool of connections to your target data store and it will re-use them in a round-robin fashion.  It’s nothing overly complex, but if you’ve got a few different threads doing different work on the database, it improves performance.

2. I added what I call a “Recovery Service” because I couldn’t come up with a better name.  This doesn’t restore data, but instead it attempt to recover from failures when it can. It’s really useful for remotely connected data stores.  For example, if you are using a SQL Server or DreamFactory DSP and the server or network goes down, your data actions are going to fail and you have to add in a load of handling logic for that.  The Recovery Service is designed to help you out here.  It will look for those types of failure and cache your operations until the store comes back online, at which time it will then apply the changes.  Sort of a “store-and-forward” type of capability, but at the command level and in RAM.

The new release is over on CodePlex in source and binary format.

Another ORM Update

$
0
0

One of the laws of software development is “A better way of finding bugs than testing is to release” and sure enough, that happened.  I rolled a release of the ORM yesterday, and no sooner than I did, someone hit a bug with the new connection pooling in the Compact Framework.  It turns out that the SqlCeConnection exposes a Disposed event in the CF, but that event never fires.  The connection pooling implementation relied on that event to know when to remove connections from the pool, and voila – a bug is born.  Is it a bug in the ORM, or a bug in the SqlCeConnection?  I’d say that’s debatable both ways, but since I only have control over one of them I made the fix where I could.

There’s a workaround checked into the source tree over on Codeplex (change set 107904).  This fix is *not* yet rolled into a release package (sure would be nice if that were automated).

Google Analytics for Xamarin Forms

$
0
0

I recently needed to add Google Analytics support to a couple Xamarin Forms applications I am working on.  I was a bit surprised that I didn’t find anything that was simply ready-built out in NuGet – it seems like a lot of people would, or at least should, be using some form of analytics for their mobile applications.

Since I’m a fan of generating APIs, and since I like freely sharing things that aren’t part of core business functions, I wrote a simple PCL library, pushed the code out to Github and published it to NuGet.

I didn’t add everything that GA supports – I don’t currently need features for ECommerce or Social Interactions.  What it does provide it the ability to publish Events, Screen Views and Timing information.

First, you’ll need a GA account and then a Tracking ID for your application.  Once that’s done, you just spin up an AnalyticsService instance:

var analytics = new AnalyticsService("UA-XXXXXXXX-X", applicationName: "My Xamarin App");

That’s all there is to it.  You’re now ready to start tracking.  GA has the ability to track things in a “session”, which allows you to get a feel for all of the activities a user did during that session.  I typically start a session when the user opens the app or logs in, and close the session when they leave or log out.  It’s as simple as this:

analytics.TrackSessionStart();

// do stuff you want to track

analytics.TrackSessionEnd();

Tracking a screen view is just as simple:


analytics.TrackScreenView("HomePage");

If you want to track a specific event, let’s say a user clicking on a button click (Help, for example), you’d simply do something along these lines:


analytics.TrackEvent("user action", "button click", label: "help");

If you want to keep track of how long something takes – anything from data loads to how long a user sits on a page or whatever, you can track Timing like this:


analytics.TrackTiming("navigation", "Screen1", 5000);

While these are powerful, they do require you as a developer to do a fair bit of work to track everything you want.  To simplify that, I also added support for GA directly into the OpenNETCF IoC library’s NavigationService.  I’ll do a separate blog entry on how that works, but basically all you have to do is initialize tracking with your Tracking ID and the NavigationService will track all of your screen changes and timings for you with zero added code on your part.

Xamarin Forms Navigation with OpenNETCF IoC

$
0
0

The OpenNETCF IoC library has been around for a long time.  I needed a DI container that was simple and fast that provided some cool features like event aggregation and module plugins.  I still use it heavily in every project I work on.

When I started working with Xamarin Forms, I still needed a DI container.  Now I know that Xamarin provides one, but I already know how to use OpenNETCF’s IoC and the syntax for it is pretty much muscle memory for me at this point.  I didn’t want to learn a new paradigm when the one I have solves the problem just fine and it will let me continue to use the same syntax on Windows desktop, Web APIs, Xamarin and still the occasional Windows CE project.

The lack of Reflection support in a PCL assembly forced me to rework things a bit, so I lost event aggregation and plug-ins, but it’s still a really simple and fast DI container that supports both singletons and named instances.

What was missing, however, was a navigation structure.  So I created one and added it to the Xamarin build of IoC.  Here’s a very, very quick overview of how you can use it.

The NavigationService class is the root of what you’ll you and it’s a static to make it easier to use.  I don’t want or need to create or find an instance of it every time I want to use it.

The first step is to create a View/ViewModel pair.  The View must derive from Page.  The ViewModel must implement the provided IViewModel interface (which simply implements INotifyPropertyChanged and nothing else at this point).  Basically it’s a near drop-in for any Xamarin Forms project.

So let’s assume I have a simple app with two pages: Home and Detail.

I’d create the following classes: HomeView, HomeViewModel, DetailsView and DetailsViewModel.  The Views are just the out-of-the-box Pages.  The ViewModels would simply derive from IViewModel and add in the PropertyChanged event (which you probably have already).

Setting things up for using the Navigation service is straightforward.  You simply register the View/ViewModel types.  Note that you don’t actually create any instances of the Views or ViewModels:


NavigationService.Register<HomeView, HomeViewModel>();
NavigationService.Register<DetailsView, DetailsViewModel>();

This registration tells the NavigationService the relationship so that when you need to display a View, it can ensure that the ViewModel also exists and then it can inject it as the BindingSource for you.

Once we’ve registered our Views and ViewModels, we can start up the app like this:


NavigationService.SetMainView<HomeView>(true);

Things to note here are that we don’t directly set the Application.MainView, we simply tell the navigation service the Type we want to use.  We also pass in true here as a parameter to tell the service we want it to wrap the Page in a NavigationPage for us.  If your MainView already derives from NavigationPage, you’d pass in false. Yes, this could probably be made automatic, but for now it’s not.

Now that you have you main page, you can navigate between pages forward and back.  For example, to go from the MainView to the DetailsView, you’d use this (true telling the NavigationService to show animation for the navigation):

 NavigationService.NavigateForward<DetailsView>(true); 

And if you’re on the DetailsView and you want to go back?  That’s as simple as this:

NavigationService.NavigateBack(true);

IoC Navigation Sample for Xamarin Forms

$
0
0

The OpenNETCF IoC Library provides a simple and lightweight MVVM Navigation framework for Xamarin Forms application navigation (along with a lightweight DI container).  I’ve blogged a couple examples on how it can be used, but I thought that a more complete example, with full downloadable source code would be a bit friendlier and easier to get your brain around.

With that, I decided to build out a very simple framework for a fairly typical application scenario.  I didn’t make the views complex, nor did I do work to add graphics or anything fancy.  I wanted it to show just what is necessary to show how you might go about creating the application framework and all of the navigation that would be associated.

Some highlights of what the application contains:

  • A Login view that accepts a Username and Password
  • A Home view that has
    • A button for a menu (typically it would be a hamburger icon)
    • A Summary data item that when clicked navigates to a Details view
  • A slide-out left Menu that:
    • Is activated by a left-to-right swipe or clicking on the menu button
    • Has a button that, when clicked, navigates over to a Settings view

The application launches to a Login view.  Once logged in, you end up on a Home view that has a slide-out menu in the left and a “summary” item on the main view.

In the menu is a “settings” button that will take you over to a settings screen.  Clicking on the summary item in the Home View takes you over to a details view.

The source code for the example can be found in source control under the Examples/NavigationExample folder.

This sample required a couple updated to the IoC library, so you’d need to have 1.0.16215 as a minimum (which is what is in source control anyway).  You can get the IoC Library right from NuGet.

Viewing all 21 articles
Browse latest View live


Latest Images