Skip to content

Commit 844e99a

Browse files
https://github.com/AndreiMisiukevich/HotReload/issues/131
1 parent f95b982 commit 844e99a

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

Reloader/Xamarin.Forms.HotReload.Reloader/HotReloader.cs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public ReloaderStartupInfo Run(Application app, Configuration config = null)
7878
{
7979
_assemblies.Add(app.GetType().Assembly);
8080
}
81-
foreach(var asm in config.AppAssemblies ?? new Assembly[0])
81+
foreach (var asm in config.AppAssemblies ?? new Assembly[0])
8282
{
8383
_assemblies.Add(asm);
8484
}
@@ -203,7 +203,7 @@ public ReloaderStartupInfo Run(Application app, Configuration config = null)
203203
{
204204
try
205205
{
206-
foreach(var asm in _assemblies)
206+
foreach (var asm in _assemblies)
207207
{
208208
HotCompiler.Current.TryLoadAssembly(asm);
209209
}
@@ -354,7 +354,7 @@ private async void InitializeElement(object obj, bool hasCodeGenAttr, bool isInj
354354
item = new ReloadItem { HasXaml = hasCodeGenAttr };
355355
_resourceMapping[className] = item;
356356

357-
if(item.HasXaml)
357+
if (item.HasXaml)
358358
{
359359
var type = obj.GetType();
360360

@@ -426,19 +426,19 @@ private async void InitializeElement(object obj, bool hasCodeGenAttr, bool isInj
426426
item.Objects.Add(obj);
427427
}
428428

429-
if(_ignoredElementInit == obj)
429+
if (_ignoredElementInit == obj)
430430
{
431431
return;
432432
}
433433

434434
if (!item.HasUpdates && !isInjected)
435435
{
436-
OnLoaded(obj);
436+
OnLoaded(obj, false);
437437
}
438438
else
439439
{
440440
var code = item.Code;
441-
if(isInjected)
441+
if (isInjected)
442442
{
443443
code = code?.Replace("HotReloader.Current.InjectComponentInitialization(this)", string.Empty);
444444
}
@@ -475,23 +475,23 @@ private void ReloadElements(string content, string path)
475475
ReloadItem item = null;
476476
string resKey = null;
477477
Type csharpType = null;
478-
478+
479479
var isCss = Path.GetExtension(path) == ".css";
480480
var isCode = Path.GetExtension(path) == ".cs";
481481

482-
if(isCode)
482+
if (isCode)
483483
{
484484
content = content.Replace("InitializeComponent()", "HotReloader.Current.InjectComponentInitialization(this)");
485485
var nameSpace = Regex.Match(content, "namespace[\\s]*(.+\\s)").Groups[1]?.Value?.Trim();
486486
var className = Regex.Match(content, "class[\\s]*(.+\\s)").Groups[1]?.Value?.Split(new char[] { ':', ' ' }).FirstOrDefault()?.Trim();
487487
resKey = $"{nameSpace}.{className}";
488488
csharpType = HotCompiler.Current.Compile(content, resKey);
489-
if(csharpType == null)
489+
if (csharpType == null)
490490
{
491491
return;
492492
}
493493

494-
if(!_resourceMapping.TryGetValue(resKey, out item))
494+
if (!_resourceMapping.TryGetValue(resKey, out item))
495495
{
496496
Device.BeginInvokeOnMainThread(() =>
497497
{
@@ -610,7 +610,9 @@ private void ReloadElement(object obj, ReloadItem reloadItem, Type csharpType =
610610
{
611611
if (!string.IsNullOrWhiteSpace(reloadItem.Code) && csharpType != null)
612612
{
613-
var parameters = (obj as ICsharpRestorable)?.ConstructorRestoringParameters ?? new object[0];
613+
var prop = obj.GetType().GetProperty("HotReloadCtorParams", BindingFlags.Instance | BindingFlags.NonPublic)
614+
?? obj.GetType().GetProperty("HotReloadCtorParams", BindingFlags.Instance | BindingFlags.Public);
615+
var parameters = prop?.GetValue(obj) ?? new object[0];
614616
switch (obj)
615617
{
616618
case Page page:
@@ -688,9 +690,9 @@ private void ReloadElement(object obj, ReloadItem reloadItem, Type csharpType =
688690
Console.WriteLine("### HOTRELOAD ERROR: CANNOT RELOAD C# CODE ###");
689691
}
690692

691-
if(!reloadItem.HasXaml)
693+
if (!reloadItem.HasXaml)
692694
{
693-
OnLoaded(obj);
695+
OnLoaded(obj, csharpType != null);
694696
return;
695697
}
696698

@@ -709,7 +711,7 @@ private void ReloadElement(object obj, ReloadItem reloadItem, Type csharpType =
709711
{
710712
throw rebuildEx;
711713
}
712-
OnLoaded(obj);
714+
OnLoaded(obj, true);
713715
return;
714716
}
715717

@@ -822,7 +824,7 @@ private void ReloadElement(object obj, ReloadItem reloadItem, Type csharpType =
822824
}
823825

824826
SetupNamedChildren(obj);
825-
OnLoaded(obj);
827+
OnLoaded(obj, true);
826828
}
827829

828830
private ReloadItem GetItemForReloadingSourceRes(Uri source, object belongObj)
@@ -1165,7 +1167,24 @@ private string WithoutGeneratedPostfix(string className)
11651167
private bool HasCodegenAttribute(BindableObject bindable)
11661168
=> bindable.GetType().GetCustomAttribute<XamlFilePathAttribute>() != null;
11671169

1168-
private void OnLoaded(object element)
1169-
=> (element as IReloadable)?.OnLoaded();
1170+
private void OnLoaded(object element, bool isReloaded)
1171+
{
1172+
try
1173+
{
1174+
#pragma warning disable
1175+
(element as IReloadable)?.OnLoaded();
1176+
#pragma warning restore
1177+
if (isReloaded)
1178+
{
1179+
var method = element.GetType().GetMethod("OnHotReloaded", BindingFlags.Instance | BindingFlags.NonPublic)
1180+
?? element.GetType().GetMethod("OnHotReloaded", BindingFlags.Instance | BindingFlags.Public);
1181+
method?.Invoke(element, null);
1182+
}
1183+
}
1184+
catch (Exception ex)
1185+
{
1186+
Console.WriteLine(ex);
1187+
}
1188+
}
11701189
}
11711190
}

Reloader/Xamarin.Forms.HotReload.Reloader/ICsharpRestorable.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

Reloader/Xamarin.Forms.HotReload.Reloader/IReloadable.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
namespace Xamarin.Forms
1+
using System;
2+
3+
namespace Xamarin.Forms
24
{
5+
[Obsolete("Obsolete/Deprecated. Just define a method with name OnHotReloaded. Keep in mind that OnHotReloaded will not be called on initial load!")]
36
public interface IReloadable
47
{
58
void OnLoaded();

0 commit comments

Comments
 (0)