Skip to content

Instantly share code, notes, and snippets.

@jsuarezruiz
Created August 20, 2018 17:09
Show Gist options
  • Save jsuarezruiz/caf985260e7fbe1042004917fdb9b747 to your computer and use it in GitHub Desktop.
Save jsuarezruiz/caf985260e7fbe1042004917fdb9b747 to your computer and use it in GitHub Desktop.
LiveSharp with Xamarin.Forms
Install LiveSharp extension: https://marketplace.visualstudio.com/items?itemName=ionoy.LiveSharp
Install LiveSharp NuGet package: https://www.nuget.org/packages/livesharp
1) add [assembly:LiveSharpInjectRuleBaseClass("Xamarin.Forms.ContentPage", "Xamarin.Forms.ContentView")] attribute to App.xaml.cs
2) add [LiveSharpStart] attribute to App constructor
3) add this code to the end of App constructor
LiveSharpContext.AddUpdateHandler(ctx => {
var instances = ctx.UpdatedMethods
.SelectMany(method => method.Instances)
.Distinct()
.ToArray();
Device.BeginInvokeOnMainThread(() => {
foreach (var instance in instances) {
try {
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
if (instance is ContentPage page && instance.GetType().GetMethod("BuildContent", bindingFlags) is MethodInfo mi)
page.Content = (View)mi.Invoke(instance, null);
else if (instance is ContentView view && instance.GetType().GetMethod("BuildContent", bindingFlags) is MethodInfo mi2)
view.Content = (View)mi2.Invoke(instance, null);
} catch (TargetInvocationException e) {
var inner = e.InnerException;
while (inner is TargetInvocationException tie)
inner = tie.InnerException;
if (inner != null)
throw inner;
throw;
}
}
});
});
4) Add public View BuildContent() method to your Page
5) Call Content = BuildContent(); from Page constructor
6) now you can define your UI in BuildContent method and update it in runtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment