Skip to content

Commit

Permalink
fixed UWP (Xamarin Forms) crash on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
ionoy committed Aug 15, 2017
1 parent 66ce7da commit b782a23
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 47 deletions.
26 changes: 1 addition & 25 deletions Ammy.Tests.sln
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26403.3
VisualStudioVersion = 15.0.26430.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ammy.Test.Wpf", "test\Ammy.Test.Wpf\Ammy.Test.Wpf.csproj", "{1E37D588-ECDB-48A0-9658-9DF5BA5F5AC1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ammy.Test.Workbench", "test\Ammy.Test.Workbench\Ammy.Test.Workbench.csproj", "{32CAA982-C97F-473E-8D8F-D1689582869E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ammy.Test.Host", "test\Ammy.Test.Host\Ammy.Test.Host.csproj", "{6157E904-A45F-45C6-BC79-D57C284318DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ammy.Test.Uwp", "test\Ammy.Test.Uwp\Ammy.Test.Uwp.csproj", "{98301D94-7BBC-4953-B66F-EC761EF2EFBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AmmysStackExchangeAggregator", "src\Samples\AmmysStackExchangeAggregator\AmmysStackExchangeAggregator.csproj", "{B9CD4210-E8E4-463E-9CD0-93A63E84D6AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataGridSample", "src\Samples\DataGridSample\DataGridSample.csproj", "{5B915277-BF63-4431-B1DC-C094158E2BC2}"
Expand Down Expand Up @@ -77,28 +75,6 @@ Global
{6157E904-A45F-45C6-BC79-D57C284318DA}.Release|x64.Build.0 = Release|Any CPU
{6157E904-A45F-45C6-BC79-D57C284318DA}.Release|x86.ActiveCfg = Release|Any CPU
{6157E904-A45F-45C6-BC79-D57C284318DA}.Release|x86.Build.0 = Release|Any CPU
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|Any CPU.ActiveCfg = Debug|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|Any CPU.Build.0 = Debug|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|Any CPU.Deploy.0 = Debug|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|ARM.ActiveCfg = Debug|ARM
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|ARM.Build.0 = Debug|ARM
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|ARM.Deploy.0 = Debug|ARM
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|x64.ActiveCfg = Debug|x64
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|x64.Build.0 = Debug|x64
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|x64.Deploy.0 = Debug|x64
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|x86.ActiveCfg = Debug|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|x86.Build.0 = Debug|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Debug|x86.Deploy.0 = Debug|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|Any CPU.ActiveCfg = Release|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|ARM.ActiveCfg = Release|ARM
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|ARM.Build.0 = Release|ARM
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|ARM.Deploy.0 = Release|ARM
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|x64.ActiveCfg = Release|x64
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|x64.Build.0 = Release|x64
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|x64.Deploy.0 = Release|x64
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|x86.ActiveCfg = Release|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|x86.Build.0 = Release|x86
{98301D94-7BBC-4953-B66F-EC761EF2EFBA}.Release|x86.Deploy.0 = Release|x86
{B9CD4210-E8E4-463E-9CD0-93A63E84D6AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9CD4210-E8E4-463E-9CD0-93A63E84D6AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9CD4210-E8E4-463E-9CD0-93A63E84D6AB}.Debug|ARM.ActiveCfg = Debug|Any CPU
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion build/ammy-host.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Ammy.Host</id>
<version>1.2.77</version>
<version>1.2.83</version>
<authors>Ion OÜ</authors>
<owners>Ion OÜ</owners>
<licenseUrl>https://creativecommons.org/licenses/by-nc/3.0/</licenseUrl>
Expand Down
4 changes: 2 additions & 2 deletions build/ammy-uwp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Ammy.UWP</id>
<version>1.2.77</version>
<version>1.2.83</version>
<authors>Ion OÜ</authors>
<owners>Ion OÜ</owners>
<licenseUrl>https://creativecommons.org/licenses/by-nc/3.0/</licenseUrl>
Expand All @@ -13,7 +13,7 @@
<copyright>Copyright 2017</copyright>
<tags>xaml uwp</tags>
<dependencies>
<dependency id="Ammy" version="1.2.77" />
<dependency id="Ammy" version="1.2.83" />
</dependencies>
</metadata>
<files>
Expand Down
4 changes: 2 additions & 2 deletions build/ammy-wpf.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Ammy.WPF</id>
<version>1.2.77</version>
<version>1.2.83</version>
<authors>Ion OÜ</authors>
<owners>Ion OÜ</owners>
<licenseUrl>https://creativecommons.org/licenses/by-nc/3.0/</licenseUrl>
Expand All @@ -13,7 +13,7 @@
<copyright>Copyright 2017</copyright>
<tags>xaml wpf</tags>
<dependencies>
<dependency id="Ammy" version="1.2.77" />
<dependency id="Ammy" version="1.2.83" />
</dependencies>
</metadata>
<files>
Expand Down
4 changes: 2 additions & 2 deletions build/ammy-xamarinforms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Ammy.XamarinForms</id>
<version>1.2.77</version>
<version>1.2.83</version>
<authors>Ion OÜ</authors>
<owners>Ion OÜ</owners>
<licenseUrl>https://creativecommons.org/licenses/by-nc/3.0/</licenseUrl>
Expand All @@ -13,7 +13,7 @@
<copyright>Copyright 2017</copyright>
<tags>xaml xamarin</tags>
<dependencies>
<dependency id="Ammy" version="1.2.77" />
<dependency id="Ammy" version="1.2.83" />
</dependencies>
</metadata>
<files>
Expand Down
2 changes: 1 addition & 1 deletion build/ammy.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Ammy</id>
<version>1.2.77</version>
<version>1.2.83</version>
<authors>Ion OÜ</authors>
<owners>Ion OÜ</owners>
<licenseUrl>https://creativecommons.org/licenses/by-nc/3.0/</licenseUrl>
Expand Down
4 changes: 2 additions & 2 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
##
## [1.2.83] - 14.06.2017

* fixed bug #91 (combine not working)
* Fixed UWP startup crash issue for Xamarin Forms

## [1.2.74] - 14.06.2017

Expand Down
8 changes: 8 additions & 0 deletions src/Platforms/Ammy.Sidekick.XamarinForms/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,18 @@ public static class ReflectionExtensions
{
public static MethodInfo GetMethod(this Type type, string name, bool isInstance, Type[] parameterTypes = null)
{
// Search for public methods only
var method = type.GetTypeInfo()
.GetDeclaredMethods(name)
.FirstOrDefault(mi => mi.IsPublic && (isInstance ? !mi.IsStatic : mi.IsStatic) && HasMatchingParameterTypes(mi, parameterTypes));

// Search for any methods
if (method == null) {
method = type.GetTypeInfo()
.GetDeclaredMethods(name)
.FirstOrDefault(mi => isInstance ? !mi.IsStatic : mi.IsStatic && HasMatchingParameterTypes(mi, parameterTypes));
}

if (method == null)
throw new Exception("Method `" + name + "` on type `" + type.Name + "` not found");

Expand Down
93 changes: 81 additions & 12 deletions src/Platforms/Ammy.Sidekick.XamarinForms/KnownTypes.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Xamarin.Forms;

namespace AmmySidekick
{
Expand All @@ -12,32 +14,99 @@ public static class KnownTypes

static KnownTypes()
{
Assemblies = GetAssemblies();
try {
Assemblies = GetAssemblies();
} catch {
Debug.WriteLine("Failed to load assemblies");

Assemblies = new Assembly[] {
typeof(Element).GetTypeInfo().Assembly,
typeof(string).GetTypeInfo().Assembly
};
}
}

public static Type FindType<T>()
public static Tuple<Type, FieldInfo> FindShortTypeWithProperty(string typeName, string property)
{
return typeof(T);
EnsureTypesLoaded();

foreach (var kvp in TypesByFullName) {
if (kvp.Key.EndsWith("." + typeName)) {
var foundProperty = FindBindablePropertyField(kvp.Value, property);
if (foundProperty != null)
return Tuple.Create(kvp.Value, foundProperty);
}
}

return null;
}

public static Type FindType(string typeName)
{
if (TypesByFullName.Count == 0)
{
EnsureTypesLoaded();

Type ret;
if (TypesByFullName.TryGetValue(typeName, out ret))
return ret;

// If searching assemblies failed, try to resolve type manually
return ManualFindType(typeName);
}

private static void EnsureTypesLoaded()
{
if (TypesByFullName.Count == 0) {
var allTypes = Assemblies.Where(a => a != null)
.SelectMany(a => a.DefinedTypes);

foreach (var type in allTypes)
TypesByFullName[type.FullName] = type.AsType();
}
}

Type ret;
if (TypesByFullName.TryGetValue(typeName, out ret)) return ret;
private static Type ManualFindType(string typeName)
{
var uwpNetAssemblies = new[] {
"System.Net.Sockets, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Net.Sockets, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Net.Primitives, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Net.Primitives, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Net.Primitives, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
};

if (string.IsNullOrWhiteSpace(typeName))
throw new InvalidOperationException("Type name shouldn't be empty or null");

if (typeName.StartsWith("System.Net", StringComparison.OrdinalIgnoreCase)) {
foreach (var netAssembly in uwpNetAssemblies) {
try {
var type = Type.GetType(typeName + ", " + netAssembly);
if (type != null)
return type;
} catch {
// do nothing if load failed
}
}
}

return null;
}

public static FieldInfo FindBindablePropertyField(Type type, string propertyName)
{
var typeInfo = type.GetTypeInfo();

foreach (var declaredField in typeInfo.DeclaredFields)
if (declaredField.IsStatic && declaredField.Name == propertyName + "Property")
return declaredField;

if (typeInfo.BaseType == null)
return null;

throw new InvalidOperationException("Couldn't find type: " + typeName);
return FindBindablePropertyField(typeInfo.BaseType, propertyName);
}

public static ConstructorInfo GetConstructor(Type type, Type[] parameters)
public static ConstructorInfo GetConstructor(Type type, params Type[] parameters)
{
var ctor = type.GetConstructors()
.FirstOrDefault(ci => {
Expand All @@ -49,8 +118,8 @@ public static ConstructorInfo GetConstructor(Type type, Type[] parameters)
});

if (ctor == null)
throw new InvalidOperationException("Couldn't find constructor for " + type +
" with parameters: " +
throw new InvalidOperationException("Couldn't find constructor for " + type +
" with parameters: " +
string.Join(", ", parameters.Select(t => t.Name)));

return ctor;
Expand All @@ -63,7 +132,7 @@ private static Assembly[] GetAssemblies()
var type = assembly.GetType("System.AppDomain");
var currentDomainProp = type.GetRuntimeProperty("CurrentDomain");
var getMethod = currentDomainProp.GetMethod;
var currentdomain = getMethod.Invoke(null, new object[] {});
var currentdomain = getMethod.Invoke(null, new object[] { });
var getassemblies = currentdomain.GetType().GetRuntimeMethod("GetAssemblies", new Type[] { });
return getassemblies.Invoke(currentdomain, new object[] { }) as Assembly[];
}
Expand Down

0 comments on commit b782a23

Please sign in to comment.