Coder Social home page Coder Social logo

xamlator's Introduction

Build Status

XAMLator live previewer for Xamarin.Forms

XAMLator is a live XAML previewer for Xamarin.Forms with partial Hot Reload. Change a XAML view, its code behind or a CSS style sheet in the IDE and you preview it live in your application. It works on iOS simulators, Android emulators and Android real devices!

monkeys

Features:

  1. Works with any kind of Xamarin Forms project and MVVM frameworks.
  2. Live preview in Android and iOS emulators
  3. Live preview in real devices (Android only)
  4. Preview in several devices at the same time.
  5. XAML live updates
  6. CSS live updates
  7. Code behind live updates
  8. Customizable previewer
  9. Support for dummy data bindings

"But... Wait! Visual Studio has already a XAML previewer, why do I need XAMLator?"

Visual Studio already has a XAML previewer, but it has some limitations. The previewer only reads a XAML and tries to render it, whithout further context. If your view relies on any static initialization of the application, you are welcomed with a beatiful exception.

XAMLator works on a very different way, the code updates are sent to the device where the application is running and the application itself renders the view, where the application is now correctly initialized. Another benefit is that you preview it on a real device and you can even preview it in several devices at the same time: an Android tablet, an iPhone X simulator, an iPad simulator, a desktop app... as many as you want!

How does XAMLator compare to existing solutions?

XAMLator is like Live XAML but open source, hence free, and with live code behind updates!

Visual Studio plugin Installation

macOS

Install the XAMLator add-in for VisualStudio for Mac in the Add-in Manager:

  1. Open Visual Studio->Extensions
  2. Search for XAMLator
  3. Install it!

The Add-in store is down, meanwhile you can install the add-in from the CI build artifact

Windows

XAMLator doens't have yet a plugin for Visual Studio, we are looking for contributors to create it!

Creating the plugin should be fairly simple following the vs4mac example

Installation

  1. Add the XAMLator nuget package to the application project, the Android, iOS, UWP or macOS project. If you are using a netstandard library to shared code, don't add the nuget to that project.

  2. Initialize the server in the app initialization.

AppDelegate.cs in iOS

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
  global::Xamarin.Forms.Forms.Init();
  LoadApplication(new App());
#if DEBUG
  XAMLator.Server.PreviewServer.Run();
#endif
  return base.FinishedLaunching(app, options);
}

AppDelegate.cs in macOS

public override void DidFinishLaunching(NSNotification notification)
{
  Forms.Init();
  LoadApplication(new App());
#if DEBUG
  XAMLator.Server.PreviewServer.Run();
#endif
  base.DidFinishLaunching(notification);
}

MainActivity.cs in Android

protected override void OnCreate(Bundle bundle)
{
  TabLayoutResource = Resource.Layout.Tabbar;
  ToolbarResource = Resource.Layout.Toolbar;

  base.OnCreate(bundle);

  global::Xamarin.Forms.Forms.Init(this, bundle);
  LoadApplication(new App());
#if DEBUG
  XAMLator.Server.PreviewServer.Run();
#endif
}

iOS additional setup

For iOS applications you have to pass the --enable-repl option to the mtouch additional arguments. enable-repl

Android Emulator additional setup

To run it in the android emulator you will have to reverse the TCP port 8488 so the emulator can reach the IDE when connection to localhost:8488

$ adb reverse tcp:8488 tcp:8488

How to use it

Run your application in debug mode, it should start as usual.

To preview a Xamarin Forms View or Page open in the editor the .xaml or xaml.cs file.

In XAML views, changes are applied when you save the file.

In Code Behind, changes are applied as you type and one the IDE as finished analyzing the class

For CSS update, once the CSS has been modified you have to open the view you want to preview for changes to be applied (in future versions this will be automatic)

Previewing in multiple devices

To use the previewer in several devices/simulators at the same time you only need to start the application several times, one for each platform and device you want to preview. You can easilly do it using the "Run Item" option in the project's menu.

multiple-devices

Customization

XAMLator uses modal navigation to preview pages since it's the only kind of navigation that works globally in all platforms. Hierachical navigation requires the navigation to be performed from a NavigationPage, that it's not always available in all apps and it might vary depending on the MVVM framework you use. For XAMLator, it's also impossible to know how you are performing a navigation for a given page as you could do it hierarchical, or modal, so if you happen to use this page with an hierarchical navigation, the NavigationBar will not be visible.

The navigation can be easily customized with a new Previewer:

public class CustomPreviewer : Previewer
	{
		public CustomPreviewer() : base(new Dictionary<Type, object> ())
		{
		}

		protected override Task ShowPreviewPage(Page previewPage)
		{
			return Application.Current.MainPage.Navigation.PushAsync(previewPage, false);
		}

		protected override Task HidePreviewPage(Page previewPage)
		{
			return Application.Current.MainPage.Navigation.PopAsync();
		}
	}

You can use your new previewer in the server initialization

 XAMLator.Server.PreviewServer.Run(previewer:new CustomPreviewer ());

Known issues

  • Code behind updates only works if you edit the xaml.cs file. If you edit any other class, like a ViewModel, you will have to recompile.
  • CSS updates do not apply automatically to the current view, you have to reopen the view in the editor to have them applied.
  • XAMLator doesn't work on iOS devices because dynamic code generation is not supported on iOS.

Contributors

Hacking

Visual Studio for Mac

Hacking and debugging XAMLator's server and its vs4mac add-in is very easy installing the AddinMaker add-in, using the Extension Manager (Visual Studio Community->Extensions...). Once you have the add-in installed, simply run the XAMLator.Client.MonoDevelop project. It will start a new instance of the IDE with the plugin running in it. In this new instance you can reopen the XAMLator.sln solution and run the sample app. Debugging of the client side will happen in the first instance of vs4mac, debugging the server will happen in the second instance of vs4mac. Note that for development you should disable the XAMLator add-in, if you have it already installed, to prevent conflicts between the installed version and the one running in the new instance.

Design choices

Why isn't Xamarin.Server a netstandard2.0 library?

The C# evaluator is implemented using Mono.CSharp which isn't available as a netstandard2.0 library. There is an IEvaluator implementation using Roslyn's Scripting API but it doesn't work on Android and iOS dotnet/roslyn#24442

Why are XAMLator.Server.{iOS|Android|Gtk} built with a single project using multi-targets?

Multi-targets are not fully supported in vs4mac

Attribution

Support for code reloading was achieved thanks to Continous from @praeclarum. It was a great source to understand how Mono's evaluator works and support reloading of code behind. Thanks!

xamlator's People

Contributors

aliyasar avatar davidortinau avatar fredeil avatar jsuarezruiz avatar ylatuya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xamlator's Issues

Remove MonoDevelop dependencies from Core project

I was adding the Visual Studio Windows extension and I found a couple of necessary changes to be able to do it.

We have to remove references to MonoDevelop from the Core project. We can create an IDialog interface (or similar) and implement it in each platform (Windows and macOS).

If you want I can add changes in a PR.

Run on multiple devices

How can I run on multiples devices. Example, run on three android devices at the same time or run on an iphone and android at the same time?

Forms Best coding practices

@ylatuya XAMLator is working fine for small pages. But I have some issues in

  • Custom control preview is not available (e.g Grid Parent)

  • Sometimes page switches but not reloading.

  • Sometimes page reloads but content is not updating.

  • Happens "Internal compiler error: Object reference not set to an instance of an object"

I am greenhorn in Xamarin Forms , So there were some issues in coding like "missing empty constructor"

@ylatuya Could you please give guideline for Xamarin Forms best coding practices for perfect UI rendering & performance.

[iOS] An evaluation error

Getting error on iOS
iOS version 12.1
Xamarin Forms 3.4

Getting Preview Error!
Oh no! An evaluation error!

Below given is the error:

System.NotSupportedException: Specified method is not supported. at Mono.CSharp.Evaluator.ComplileBlock(Mono.CSharp.Class host, Mono.CSharp.Undo undo, Mono.CSharp.Report Report) [0x00060] in /Library/Frameworks/Xamain.iOS.framework/Verions/12.2.1.10/src/Xamrin.iOS/mcs/eval.cs:707

Can't get it work with android emulator on VS 2017 15.9.9

Hello,

firstly thank you for your effort making this plugin. It is what I always missed while developing with Xamarin Forms. Unfortunately I can not get it running. So far I did following steps:

-In my .Net Standard typed project in the Android project I opened the references and installed xamlator nuget package.

  • Then I started my emulated device and after starting typed from Extras->Android->adb cmd "adb reverse tcp:8488 tcp:8488" (also tried typing first then starting the emulator device
  • I added the additional lines in the OnCreate class.
  • I build the project and started debugging without erros but nothing happened modifying xaml and saving it.

Am I missing some steps? Did I something wrong?

Yours

The socket has been shut down

Hi @ylatuya in the new versión

When I open a xaml file in vs for mac, the xamlator always shows this error:

screenshot 2019-02-20 at 16 23 06

Versions
vs for mac extension: 1.05
nuget version: 2019.2.20.3

iOS NavigationPage is gone when xaml reloaded

when i making changes to xaml file, NavigationPage is gone. btw i am using prism for mvvm. your tools is awesome, but please fix it. i attached screenshot before and after making changes
simulator screen shot - iphone 8 - 2018-11-05 at 09 59 12
simulator screen shot - iphone 8 - 2018-11-05 at 09 59 28

The reason behind choosing Target framework v4.7

Most projects use .net 4.6.1 sdk as it's the minimum version of .net framework which supports .net standard 2.0
But I can't understand the benefit of choosing .net 4.7
It also prevents people from building project with their visual studio unless they install 4.7 sdk.
Note that 4.6.1 sdk is installed by default.
I'm open to change those configurations and make project built.
I'm willing to help with removing packages.config files too.

[CSS] Including a StyleSheet throws an error on reload

Including a CSS file works on the initial run.

Global.css (EmbeddedResource):

.superLabel {
    color: black;
    background-color: transparent;
    font-size: 64;
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XAMLator.SampleApp"
             x:Class="XAMLator.SampleApp.MainPage">
    
    <ContentPage.Resources>
        <StyleSheet Source="/Global.css"/>
    </ContentPage.Resources>
    
    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="This is my page!"
           TextColor="White"    
           HorizontalOptions="Center"
           BackgroundColor = "Red"
           class="superLabel"
           VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage>

screenshot 2018-09-22 09 36 21

However upon hitting save and triggering a reload, XAMLator throws an eval error.

screenshot 2018-09-22 09 38 54

A member is already defined

The MainPage is work ok, I add next XAML Page and when I try edit it i got error:
Oh no! An evaluation error!
A memebr XAMLatorTest.TestPAge21.TestPage21() is already exists. Rename this member or use different parameters types.

When I change something on this page the number 21 rise.

Cannot update xaml that uses other xaml files

Description

When the xaml references references other xaml files starts to allocate/deallocate memory and after some seconds it breaks. That said tho, that only happens when you made a change and you save. The first time works perfectly. Because the log is too big (18k lines), i uploaded it and added a link. Later, I'll add a repro link.

Steps to Reproduce

  1. Create a Grid in another xaml file, add some dummy text and some bindable properties.
  2. Create a page with a Stacklayout and reference the Grid that has been previously created.
  3. Start the app.
  4. Made a minor change to the Page and save.

Expected Behavior

The xaml page should be updated as expected.

Actual Behavior

The app freezes for about 10-15 seconds and then crashes.

Logs

https://goo.gl/qMsVYA

cannot find XAMlator in VS 2017 Extensions

Hi, i followed the instructions

Install the XAMLator add-in for VisualStudio for Mac in the Add-in Manager:

Open Visual Studio->Extensions
Search for XAMLator
Install it!

but no way to find the package.. there is some problem?

Thank you very much!
Angelo

Preview Error/evaluation error

In my Mac XamForms app I changed a buttons text from "A" to "B" then saw this in the app:

Feature `pattern matching' cannot be used because it is not part of the C# 7.0 language specification

If you need me to get more information somehow let me know, I couldn't see any logs in the application output and I'm not sure where else to look for logs sorry.

Not working on macOS target

Steps to reproduce:

  1. Remove the XAMLator.Server project dependency from your XAMLator.SampleApp.MacOS project.
  2. Add the XAMLator nuget package as a reference.
  3. See it can't see the XAMLator namespace and will not compile.

[Enhancement] Some improvements that may be necessary

Hi @ylatuya,

I would like to say that this library is awesome and I would like to say thank you for that.

ControlTemplate

I am using ControlTemplate to some pages and it seems that it cannot find the getter. The stacktrace i've got in the screen is:

Xamarin.Forms.Xaml.XamlParseException: Position 2:635.
Property ControlTemplate is not found or does not have an accessible getter
at
Xamarin.Forms.Xaml.ApplyPropertiesVisitor.GetPropertyValye(System.Object xamlElement, Xamarin.Forms.Xaml.XmlName propertyName, Xamarin.Forms.Xaml.HydrationContext context, System.Xml.IXmlLineInfo lineInfo, System.Object& targetProperty) [0x00095] in D:\a\1\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:377
at
Xamarin.Forms.Xaml.ApplyPropertiesVisitor.TryAddToProperty (it got cut here)

DesignInstance

Furthermore, I am using Jetbrains Rider in order to get some Xaml Intellisense improvements and while I am setting the BindingContext at runtime(through a ViewModelLocator) i use

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DataContext="{d:DesignInstance typeHere}"
mc:Ignorable="d"

and i get this stacktrace on screen:

Xamarin.Forms.Xaml.XamlParseException: Position 2:774.
MarkupExtension not found for d:DesignIntance
at
Xamarin.Forms.Xaml.ExpandMarkupsVisitor+MarkupExpansionParser.Parse(System.String match, System.String& remaining, System.IServiceProvider serviceProvider) [0x000e4] in D:\a\1\s\Xamarin.Forms.Xaml\ExpandMarkupsVisitor.cs:144
at
Xamarin.Forms.Xaml.ExpandMarkupsVisitor.ParseExpression(System.String& expression, System.Xml.IXmlNamespaceResolver nsResolver, System.Xml.IXmlLineInfo (it got cut here)

FYI, i've got this binding warning with LogWarningsToApplicationOutput = true;

Binding: 'CloseCommand' property not found on 'XAMLator.Server.ErrorViewModel', target property: 'Xamarin.Forms.Button.Command'

I will take a look at the code and may contribute fixing those issues.

Last but not least, I have never worked with VS Extension Development but I would like to start and help to develop the XAMLator VS 2017 Extension, just to make the Xamarin Development much easier 🙂

Thanks again for all your effort

AssemblyWeaver error

Hi, when I try build (clean, new project) on iOS Simulator I got error:
Error MSB4044: The "AssemblyWeaver" task was not given a value for the required parameter "Path". (MSB4044) (XAMLatorTest)

Stuck on evaluation error

Description

Some times (it happens randomly or when there are a lot of files open and you switch on them) the PreviewPage stucks on Evaluation Error: Class, struct or interface method must have a return type. Trying switching to a cs file and back to a xaml one does not fix the issue.

Some times the output prints some gc info and seems that it got the change and trying to preview the file(with no success) and other ones it just does nothing. You have to re-run the application to make it work.

Haven't tried to make a toolbaritem for start and end(if it exists) the preview via an abstraction. Maybe a button on the preview page, like hard reload would resolve this issue.

Expected Behavior

The preview page should detect the focused page on the IDE and display it correctly.

Actual Behavior

The preview page after some time of switching files it gets stuck and does not preview the file.

Not Working In Mac

XAMLator is not working for me. I am using VS2017 Community edition for Mac.

These are the logs I am getting.

Loaded assembly: XAMLator.Server.dll [External]
[Mono] Image addref XAMLator.Server[0x7c337cdb80] -> XAMLator.Server.dll[0x7c337dd000]: 2
[Mono] Prepared to set up assembly 'XAMLator.Server' (XAMLator.Server.dll)
[Mono] Assembly XAMLator.Server[0x7c337cdb80] added to domain RootDomain, ref_count=1
[Mono] AOT: image 'XAMLator.Server.dll.so' not found: dlopen failed: library "/data/app/-1/lib/arm64/libaot-XAMLator.Server.dll.so" not found
[Mono] AOT: image '/Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/sdks/out/android-arm64-v8a-release/lib/mono/aot-cache/arm64/XAMLator.Server.dll.so' not found: dlopen failed: library "/data/app/-1/lib/arm64/libaot-XAMLator.Server.dll.so" not found

[Mono] Config attempting to parse: 'XAMLator.Server.dll.config'.
[Mono] Config attempting to parse: '/Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/sdks/out/android-arm64-v8a-release/etc/mono/assemblies/XAMLator.Server/XAMLator.Server.config'.

[Mono] Assembly Ref addref XAMLator.Server[0x7c337cdb80] -> mscorlib[0x7c473a2480]: 105
[Mono] Assembly Ref addref XAMLator.Server[0x7c337cdb80] -> Xamarin.Forms.Core[0x7c3374ea80]: 25

Not working at all

It's not working with the startup project
Windows 10 updated
VS 2017 Entreprise 15.8.9
Xamarin : 4.11.0.779
Xamarin.Forms :

  • 3.4.0.987044 pre2
  • 3.3.0.967583
  • 3.3.0.912540

I'd try on real device galaxy S8+ (oreo 8.0) and chuwi (lollipop)

I'd updated OnCreate Method
The compilation is good,
I'm on debug mode
The deployment is good
The launch is ok
Modifying XAML or XAML.CS (saved of course) - Nothing append on the UI

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.