Posts

Xamarin.Forms: Apply a global style to any page

The ApplyDerivedTypes attribute of the Style is key in order to have a global style for a ContentPage for example. In order to have a global style for any Page derived class, use the TargetType="Page": <Style TargetType="Page" ApplyToDerivedTypes="True"> <Setter Property="BackgroundColor" Value="White" /> </Style>

Edit or clear the recent projects and files in Visual Studio 2019

Image
I've recently experienced an weird issue with Visual Studio 2019 . The start dialog where you select the projects to open, froze for few seconds every time I opened Visual Studio. I was able to find a workaround, by peeking under the hood of how Visual Studio 2019 keeps the recent project list stored. So here we go: VS 2019 keeps an XML with settings in the following location: %LocalAppData%\Microsoft\VisualStudio\16.0_50e1ff9d Note 16.0_50e1ff9d is the current Visual Studio version. Within the XML file, the following node contains a JSON with an array of items, where each item is a description of a recent project you opened: collection   name = "CodeContainers.Offline" > < value   name = "value" >JSON with recent list of projects </ value > Here's an example of an item structure in the JSON:

IconFont2Code, my open-source tool in order to view icons in font files and create C# class with the glyph constants

Image
As an app developer, it's impossible not to have the task to display an icon in the app. One of the common ways to display icons , instead of using images or SVGs , is by using font files (.ttf, .otf or .woff). Although font files are usually used to display text with a specific font, but they can also be used to display icons . Instead of containing glyphs with letters , they contain glyphs with icons : There are different types of font technologies but the most common ones today are the TrueType (.ttf), OpenType (.otf) or the WOFF (.woff) font files. The way they store and render the glyphs is using a collection of line and curve commands as well as a collection of hint , which allows them to provide both relatively fast drawing speed and true device independence . The  device independence is a key aspect given the myriad of display sizes and technologies which exist today. Referring to an icon inside of a icon font file  works by knowing the hexadecimal val...

Xamarin Forms: Embedding native controls and still have XAML compilation for the Page

Image
Embedding native controls Skip this part if you know what the embedding native controls is In Xamarin Forms, with native control embedding, you can put native controls (i.e. native Android or iOS controls) on your Xamarin Forms pages. It’s very simple and the official Xamarin Forms documentation (link here ) explains how to achieve it. Here’s an example of using native embedding in XAML: < ContentPage xmlns="http://xamarin.com/schemas/2014/forms"         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"         xmlns:ios="clr-namespace:UIKit;assembly=Xamarin.iOS;targetPlatform=iOS"         xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"         xmlns:androidLocal="clr-namespace:MyApp.Droid;assembly=MyApp.Droid;targetPlatform=Android"         x:Class=" MyApp.MyContentPage " >            ...

Xamarin.Forms XmlnsDefinition attribute: All of your namespaces become one

Image
When working on a mobile app developed with Xamarin Forms ( if  you aren’t, you’re missing all the fun! ) you might have your own awesome separate library project   of custom controls, effects, behaviors, value-converters, constants: You’ve might not been very happy ( to put it nicely ) how you're using your library objects in your app’s XAML, having to deal with all the different XML namespaces and prefixes: So many prefixes and namespaces to handle!  Yikes! The issue is not just about declaration of prefixes. Having separate prefixes bloats the XAML a lot because you have to use a separate prefix for every object you reference from the corresponding namespace. In my example there are 4 prefixes to deal with! As a solution to get rid of all the namespaces, a dark thought might have crossed your mind: remove all the CLR namespaces in the library’s code and just have one namespace! But you love your C# namespaces, they give class scope and separation, it’s...

Bindable layouts, one of my contributions to the Xamarin.Forms framework

Image
Xamarin, Microsoft-owned for few years now, asked me to write on their blog a guest post about bindable layouts , one of my contributions to the Xamarin Forms framework, an awesome open-source cross-platform framework for developing mobile apps. After finishing the implementation, I submitted a pull-request for it and, lo and behold, the Xamarin.Forms team accepted it! Contributing to a large framework as Xamarin.Forms can take lot of blood and sweat sometimes but it’s a lot of fun. Here’s the blog post I wrote on Xamarin's blog which describes the bindable layouts feature: https://blog.xamarin.com/xamarin-forms-3-5-a-little-bindable-love/ ...just posted on the Xamarin Blog, Andrei Nitescu shares one of his contributions to Xamarin.Forms, the bindable layout. Starting with Xamarin.Forms 3.5.0 pre2, we can use the BindableLayout set of attached properties on any Layout<T> view. Learn more: https://t.co/zhLzBKRWPa pic.twitter.com/hVtVjjqN20 — Xamarin (@xamarinhq)...

Xamarin Forms: Lazy load the tabs in TabbedPage

Too much fun with Xamarin Forms

Image
I had a blast playing with Xamarin Forms and SVGs which end up with the following which works on iOS, Android and Windows UWP, checkout the videos for Android and iOS apps: Credit for original design idea goes to Mikael Ainalem: https://twitter.com/mikaelainalem/status/963076814183772160 The SVG is created dynamically, based on the position of the text-boxes and the button, so it will work on any number of text-boxes and buttons. The actual SVG for the login screen in the videos, looks like this:

Debugging HTML5 running in iOS from Windows with Chrome dev tools!

Image
In order to debug HTML5 running in Safari on iOS, I used to connect the iOS device to my Mac, open Safari, go to Developer menu(needs to be activated from Safari Preferences) and open the page. You can find detailed steps how to do this on internet. I tried to remote debug HTML5 with other tools (Telerik has one), but the best way is with an utility called  ios-webkit-debug-proxy  which uses Chrome dev tools! You can find it here  https://github.com/google/ios-webkit-debug-proxy  and its Windows port: https://github.com/artygus/ios-webkit-debug-proxy-win32 It allows you inspect the WebKit (either the Safari or UIWebViews) running on the iOS device from your Windows machine with Chrome dev tools. This utility connects to the WebKit for inspection. Steps (most of these are written in https://github.com/google/ios-webkit-debug-proxy ): Go to  https://github.com/artygus/ios-webkit-debug-proxy-win32 You can either get the compiled version or build from the source. I ...

GridSplitter control for Xamarin Forms

Image
I created a GridSplitter control for Xamarin Forms, which works on iOS and Android. You can find the full description of how it works and how to include it in your app: https://github.com/andreinitescu/GridSplitterApp Here are some screenshots of the demo app: and a Grid with both horizontal and vertical splitters: The sample app also shows a technique to create reusable custom controls which you can style easily, very similar to how it works in WPF/UWP.

Easiest way to know which w3wp.exe PID corresponds to which application pool

Image
I keep forgetting this. Open Task Manager and have it show the Command Line column You can see for the w3p.exe processes the name of the app pool in the command line parameters

Xamarin Forms style resets

There are some default styles which you might want to reset in your Xamarin Forms apps. For example, some container controls have default padding and spacing for their child views. In a more complicated UI, sometimes these default styles can become an issue. Because the UI you build is complex, you can forget about these default values and you wonder why some views are not positioned the way you want. I created a small XAML 'resets' snippet, which can be added to the App.xaml: https://gist.github.com/andreinitescu/69e8afcad1ed9de69b76

Add App.xaml in your Xamarin Forms project

In the current version of Xamarin tools, the default Xamarin Forms project templates in both Visual Studio and Xamarin Studio do not generate App.xaml along with the App class that derives from Application and provides an entry point where you can add initialization code.  The support for App.xaml is briefly mentioned in Xamarin Forms documentation  but without giving the exact steps. These steps are: 1. Right click on the PCL project and choose to add a new file 2. In Visual Studio, choose the "Forms Xaml Page" item     In Xamarin Studio, choose the "Forms ContentPage Xaml" item 3. Write "App" as name You will get a App.xaml and App.xaml.cs created. You need to do some small modifications in each of these: 1. in App.xaml, replace the XAML with the following: < Application xmlns = "http://xamarin.com/schemas/2014/forms" xmlns:x = "http://schemas.microsoft.com/winfx/2009/xaml" x: Class = "MyApp.App" > <...

IconView control for Xamarin Forms

Image
Someone was asking on the forum how to draw a colored icon. I created an IconView control which does this: https://github.com/andreinitescu/IconApp/ The control takes a local image and applies a color on it. This is useful when you want to color images on the fly, without the need to have multiple images for different colors. At this moment the implementation is for Android and iOS. Contributions for Windows support are welcome! Usage An example of a Page using the IconView control: <? xml version = " 1.0 " encoding = " UTF-8 " ?> < ContentPage xmlns = " http://xamarin.com/schemas/2014/forms " xmlns:x = " http://schemas.microsoft.com/winfx/2009/xaml " x:Class = " IconApp.MyPage " xmlns:controls = " clr-namespace:IconApp;assembly=IconApp " > < controls:IconView Source = " monkey " Foreground = " Red " WidthRe...

CallDataMethod trigger in Xamarin Forms

Here’s a very simple implementation of a CallDataMethod trigger in Xamarin Forms: public class CallDataMethod: TriggerAction < VisualElement > { public string Method { get ; set ; } #region implemented abstract members of TriggerAction protected override void Invoke (VisualElement sender) { MethodInfo method = sender.GetType ().GetRuntimeMethod (Method, new Type[ 0 ]); if (method != null ) { ParameterInfo[] parameters = method.GetParameters (); if (parameters.Length == 0 ) method.Invoke (sender, null ); } } #endregion } For a demo, let’s say we have two text-boxes (Entry control in Xamarin Forms) and you want to enable the second one when the first has ten characters: <? xml version = " 1.0 " encoding = " UTF-8 " ?> < ContentPage xmlns = " http://xamarin.com/schemas/2014/forms ...

Manually install Xamarin Studio addins

Just copy the .dll file to (note the Xamarin Studio version, it might be different today): on Windows : %LocalAppData%\XamarinStudio-5.0\LocalInstall\Addins on Mac : /Users/[YourUser]/Library/Application Support/XamarinStudio-5.0/LocalInstall/Addins Source: http://forums.xamarin.com/discussion/comment/6356/#Comment_6356

Add ImageFailed and ImageOpened to the Xamarin Forms Image control

Currently the Image control in Xamarin Forms does not have an event to handle when image could or couldn't open. I created a way to do this by extending the implementation for the Image control and ImageSource. Check out the source code and a demo app on my GitHub: https://github.com/nitescua/ImageExtensionsApp I implemented two ways to handle image loaded status. You could either:  1. Use the Image derived class ImageEx which exposes ImageOpened and ImageFailed events: < controls:ImageEx Source = " http://dummyimage.com/100x100/000/fff " ImageFailed = " Image_ImageFailed " ImageOpened = " Image_ImageOpened " /> 2. Handle the status change right on the ImageSource instance: ImageSourceExtensions.SetStatusChangedHandler (MyImage.Source, (sender, status) => { Debug.WriteLine( " Image status: {0} " , status); }); where MyImage is an Image control in your Page To add the implementation in your apps, you need to: 1. Add ImageSour...

Cross platform splash screen in Xamarin Forms

If the splash screen is more than just an image, you can treat the splash screen as a Xamarin Page. To make this there are some steps: Step 1 . set the App’s MainPage to the splash screen page: public partial class App : Application { public App() { InitializeComponent(); Setup.Init(); // with XLabs, you would do (Page)ViewFactory.CreatePage<SplashViewModel>(); MainPage = new SplashPage(); } Step 2 .  In SplashPage, after some logic is done, you will need to “navigate” to your first app’s page. This means you just need to set again the App’s MainPage, i.e”: void ShowMainPage() { MainPage = new SplashPage(); } The issue is how do you call this. If you are not doing any logic in the splash screen, you can just do something like this: public class SplashPage : Page { async protected override void OnAppearing() { ...

Configure app which requires admin rights to run at startup with task scheduler

  App which requires elevated admin rights can’t just be set to run at startup by adding it to the registry key ( HKLM\Software\Microsoft\Windows\CurrentVersion\Run) because it mail fail to start since it requires the UAC prompt. Windows will silently fail to display the UAC prompt. One solution is to make the app a Windows Service, but depending on the technology used, this might not be possible. For example, a .NET Windows Service which is using .NET API printing (System.Printing) is not recommended (see http://blogs.msdn.com/b/dsui_team/archive/2013/06/24/printing-from-a-windows-service.aspx or see ‘Caution’ in http://msdn.microsoft.com/en-us/library/system.drawing.aspx ). So another solution is to use task scheduler. When configuring the scheduled task, you need to make sure: 1. In the task properties popup, "Run with highest privileges" option is checked. 2. In the "Edit Action" popup, "Start in" is filled with the directory path (Do not include ...

smallest EXEcutable

This is an interesting page discussing techniques to achieve the smallest EXE possible. http://www.phreedom.org/solar/code/tinype/ More, it says that if you add an UNC path as a name of an imported DLL in an executable, Windows will download and execute that file ! the server needs to be a WebDAV server.