Skip to content

Instantly share code, notes, and snippets.

@ionoy
Last active August 20, 2018 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ionoy/2dd0f0b69317f65abf6b99246a5becf3 to your computer and use it in GitHub Desktop.
Save ionoy/2dd0f0b69317f65abf6b99246a5becf3 to your computer and use it in GitHub Desktop.
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