I have Xamarin forms project that gets compiled and run in visual studio 2017(windows) but not with visual studio for Mac. It is only happening when I try to run the android project but it. With a Xamarin.Forms WPF backend available, user interfaces are reusable in both Visual Studio Mac and Visual Studio Windows. By using XAML to build MFractors UIs, I can use MFractor to build itself; an awesome process of dogfooding to accelerate product development. Discussions about building Mac apps with Xamarin.Mac This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use. The default template of Xamarin Forms project will create projects 3 Projects one PCL/shared, One for iOS and Android projects Xamarin Studio automatically checks for updates of Nuget packages added to project and shows it in the solution explorer. View the latest Visual Studio for Mac release notes on docs.microsoft.com..NET Core 2 support Visual Studio for Mac supports building.NET Core 2.0 applications, including libraries, console apps, as well as web applications and services with ASP.NET Core.
- How To Create Xamarin Forms Project In Visual Studio Mac
- Visual Studio Xamarin Sample Applications
- Visual Studio For Mac Download
- How To Create A Project In Visual Studio
- Visual Studio With Xamarin
Xamarin is a cross-platform technology that makes it possible to build native mobile apps for Android, iOS, and Windows Phone using C# and a shared codebase. Like its younger siblings NativeScript and React Native, it allows development teams to build mobile applications using the skills they already have, and spend less time writing code for each platform.
If you haven’t tried Xamarin yet, now is a great time to get started! Earlier this year, Microsoft bought Xamarin and made it free (and open-source). You can build Xamarin projects on Windows (using Visual Studio), or Mac/Linux (using Xamarin Studio).
I’m excited to dig into Xamarin because mobile apps need authentication and authorization, which Stormpath makes easy. We already have rich SDKs for .NET and ASP.NET, as well as SDKs for iOS and Android separately, but a Xamarin-specific SDK could provide even more value and make it super simple to secure your apps. It’s something I’m currently digging into, so stay tuned!
In this tutorial, I’ll show you how to use Visual Studio and Xamarin to build a basic app for iOS and Android — even if you’ve never done any app development before!
Setting Up Visual Studio and Xamarin
If you don’t have Visual Studio 2015 installed, download the free Community Edition from Microsoft. If you already have Visual Studio, make sure you have the latest update (Update 3 at the time of writing).
You’ll also need to install some optional components for Visual Studio. If you’re setting up Visual Studio from scratch, make sure these items are selected:
If you have an existing installation, you can verify that these components are installed by opening the Control Panel, choosing Uninstall or change a program, and selecting Microsoft Visual Studio 2015. Follow the installation wizard to make sure the above items (at a minimum) are checked.
Once you have the tools set up, you’re ready to create a Xamarin project!
Xamarin vs. Xamarin.Forms
The Xamarin SDK provides bindings to the platform-specific APIs on each mobile platform, so you can call Android or iOS APIs from C# code. This allows you to build native apps using C#, but you still need to design the UI separately for each platform.
Xamarin.Forms is an additional layer on top of the Xamarin SDK that makes it possible to build your UI once (in XAML markup) and let Xamarin do the hard work of translating it into the appropriate UI elements on the target platform. You can drop down to the Xamarin SDK level and interact with the platform APIs if you need to.
Should you use “raw” Xamarin, or Xamarin.Forms? It depends on what you are building:
If you’re building an app that needs little platform-specific functionality or custom UI, go with Xamarin.Forms. This is a good choice for straightforward readonly='><?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='HelloWorldApp.HelloWorldPage'> <ContentPage.Padding> <OnPlatform x:TypeArguments='Thickness' iOS='20, 40, 20, 20' Android='20, 20, 20, 20' WinPhone='20, 20, 20, 20' /> </ContentPage.Padding> <ContentPage.Content> <StackLayout VerticalOptions='FillAndExpand' HorizontalOptions='FillAndExpand' Orientation='Vertical' Spacing='15'> <Label Text='Enter your name:' /> <Entry x:Name='NameEntry' Text='Jane Doe' /> <Button x:Name='SayHelloButton' Text='Say Hello' Clicked='SayHelloButton_OnClicked' /> </StackLayout> </ContentPage.Content></ContentPage>2 4 6 8 10 12 14 16 18 20 22 | <ContentPage xmlns='http://xamarin.com/schemas/2014/forms' xmlns:x='http://schemas.microsoft.com/winfx/2009/xaml' <ContentPage.Padding> iOS='20, 40, 20, 20' WinPhone='20, 20, 20, 20'/> <ContentPage.Content> HorizontalOptions='FillAndExpand' Spacing='15'> <Entryx:Name='NameEntry'Text='Jane Doe'/> <Buttonx:Name='SayHelloButton'Text='Say Hello'Clicked='SayHelloButton_OnClicked'/> </ContentPage.Content> |
This XAML code creates a basic layout containing Label, Entry (text box), and Button controls. The control names (specified with
x:Name
) will be used to refer to the controls in code.The
Clicked=
attribute on the Button element wires up the button click event to a handler called SayHelloButton_OnClicked
, which doesn’t exist yet (but it’s about to!)Open up the code-behind for the XAML file by expanding it in the Solution Explorer and double-clicking on the
HelloWorldPage.xaml.cs
file.Replace the generated C# code with the following:
2 4 6 8 10 12 14 16 18 20 | using Xamarin.Forms; namespaceHelloWorldApp publicpartial classHelloWorldPage:ContentPage publicHelloWorldPage() InitializeComponent(); privateasync voidSayHelloButton_OnClicked(objectsender,EventArgse) varname=NameEntry.Text; await DisplayAlert('Greeting',$'Hello {name}!','Howdy'); } |
Looks familiar, doesn’t it? The
SayHelloButton_OnClicked
method will run when the SayHelloButton
is clicked on the XAML page. First, the value of the textbox is assigned to the name
variable, and then the DisplayName
method is called to display a modal popup on the device.There’s one more thing to do before you’re done: telling the app to use the new page. In
App.cs
, replace the constructor method with this:2 4 | { } |
That’s it! Your new Xamarin app is ready to go.
Testing Your Xamarin App on Android
If you have the Visual Studio Android Emulator installed, testing the Android version of your Xamarin app is simple. In the Visual Studio toolbar, pick the HelloWorldApp.Droid project and choose an Android device to emulate. Then, click the green Play button to start the emulator.
The Android emulator can be slow to load, so give it some time. If everything builds properly, you should see your app running on Android.
Testing Your Xamarin App on iOS
Testing your Xamarin app on iOS is a little trickier, because it requires a Mac to provide the emulator. If you have a Mac handy, follow the official instructions to set up the Mac agent and connect it to Visual Studio. Then, pick the HelloWorld.iOS project, and switch the architecture to iPhone Simulator. Choose a device version and click Play.
After the project builds, the simulator will launch on the Mac.
Next steps
This tutorial only scratches the surface. There’s plenty more you can do with Xamarin! Here’s some further reading:
If you’ve built something cool with Xamarin, let me know in the comments or on Twitter @nbarbettini!
Learn to use Xamarin.Forms to build user interfaces for your Visual Studio Mac extensions.
? ? ? Important ? ? ?
MFractor has removed all Xamarin.Forms user interfaces as of October 2018.
Unfortunately, using Xamarin.Forms inside Visual Studio Mac caused instability in both MFractor and Visual Studio Mac itself.
In the end, we decided that this technique was not commercially viable. If you choose to use Xamarin.Forms to build UIs, proceed with caution.
At MFractor, we recommend using XWT to build user interfaces for Visual Studio Mac.
If you would like guidance on this, please email matthew@mfractor.com. We provide consulting services to assist the development of Visaul Studio Mac extensions and would be happy to help.
Introduction
Ever since I commercialised MFractor in June 2017, I've been pulled to the idea of using XAML and Xamarin.Forms to build user interfaces for Visual Studio Mac extensions.
For MFractor, developing tools like the Image Wizard or localisation wizard cost days to weeks of engineering effort. As a bootstrapped business, this time-cost makes it prohibitively expensive to develop tools that are UI-centric.
Therefore, there are compelling reasons to use Xamarin.Forms to build Visual Studio Mac extensions:
- XAML is much, much easier to work with than XWT, Visual Studio Macs UI framework. This dramatically lowers the barrier to entry for developing Visual Studio Mac extensions.
- We can use code and can also make use of value converters, triggers and behaviours.
- With a Xamarin.Forms WPF backend available, user interfaces are reusable in both Visual Studio Mac and Visual Studio Windows.
- By using XAML to build MFractors UIs, I can use MFractor to build itself; an awesome process of dogfooding to accelerate product development.
There are huge productivity gains here!
![Studio Studio](/uploads/1/2/6/2/126256535/974754788.png)
To prove that this technique is valid for production-ready tooling and is not just a toy, we'll be building an image asset browser you can use to visually explore images inside a solution:
How To Create Xamarin Forms Project In Visual Studio Mac
So, read on to learn how to use Xamarin.Forms inside Visual Studio Mac to build rich user interfaces for your tooling.
Using Xamarin.Forms Inside Visual Studio Mac
Let's get started!
First things first, you must have version 1.4.2 of the Addin Maker installed into Visual Studio Mac. Based on my many, many failed attempts at getting this to work, AddinMaker v1.4.2 is the one that works.
Next, you'll need to create a new Visual Studio Mac extension that is an SDK style project and references the NuGet MonoDevelop.Addins v0.4.4. I've found that the Xamarin.Forms bootstrapping process does not work in Visual Studio Mac extensions that are not SDK style projects.
If you have an existing extension, you'll need to upgrade your main extensions project to an SDK style project and reference NuGet MonoDevelop.Addins v0.4.4. The best way to do this is to create a new extension project within the existing solution and then copy paste all the code files from the old project into the new project.
After you've setup your project, it's time to set up Xamarin.Forms inside our extension.
Firstly, we need to add the
Xamarin.Forms.Platform.GTK
nuget into our project. At the time of writing, this is only available within the Xamarin.Forms nightly builds. You can find instructions to add the nightly builds feed here.In the solution explorer panel, locate your extensions project, open Dependencies and then double click on the NuGet item.
Once the package explorer has opened, search for Xamarin.Forms.Platform.GTK and then add it.
Before we can show any user interfaces built using Xamarin.Forms, we need to startup Xamarin.Forms by calling
Forms.Init()
.We do this by creating a
CommandHandler
that invokes Forms.Init()
within its Run()
method like so:InitXamarinFormsCommand.cs
And then in our
Manifest.addin.xml
we insert our InitXamarinFormsCommand
into the /MonoDevelop/Ide/StartupHandlers
extension point:Manifest.addin.xml
Visual Studio Xamarin Sample Applications
When the IDE opens, the
Run()
method of InitXamarinFormsCommand
will be invoked and it will startup Xamarin.Forms!Next, it's time to create our user interface.
As we are now using Xamarin.Forms, we can create our view in XAML and also create an accompanying view model.
To display the user interface for the Image Asset Browser, we use native embedding to convert our Xamarin.Forms UI into a conrol that can be used inside a GTK window. As we working with GTK, we call the extension method
.CreateContainer()
to convert a Xamarin.Forms.Page
into a Gtk.Container
class that can be inserted into a Gtk.Window
.Our
ImageAssetBrowserWindow
looks like this:ImageAssetBrowserWindow.cs&
This creates a window that specifically shows the
ImageAssetBrowserView
and binds it to the ImageAssetBrowserViewModel
.Lastly, we create a new
CommandHandler
to show our user interface:BrowseImageAssetsCommand.cs
And then we expose the
BrowseImageAssetsCommand
through the tools menu by adding the command into the extensions manifest:Manifest.addin.xml
Voila! Now we have a working image browser that's driven using Xamarin.Forms!
Summary
Visual Studio For Mac Download
Creating our user interfaces in Xamarin.Forms for our Visual Studio Mac extension has huge benefits. It's much to build UIs with XAML, we get to used lovely MVVM architecture and Xamarin.Forms is much better documented than Xwt.
How To Create A Project In Visual Studio
Unfortunately, this methodology is not without it's issues. As more extension developers start using this technology, it's that many extension developers will bundling and loading their own Xamarin.Forms binaries, resulting in assembly loading race conditions and versioning mis-matches. This causes big problems issues in Visual Studio for Mac, often causing instability in the core product.
Visual Studio With Xamarin
In the future, we will need an official Visual Studio Mac Xamarin.Forms extension to prevent multiple calls to Forms.Init() and potential assembly version conflicts.