Last active
August 20, 2018 17:06
-
-
Save ionoy/2dd0f0b69317f65abf6b99246a5becf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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