Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Prism.Common | ||
{ | ||
public abstract class ParametersBase : IEnumerable<KeyValuePair<string, object>> | ||
{ | ||
private readonly List<KeyValuePair<string, object>> _entries = new List<KeyValuePair<string, object>>(); | ||
|
||
protected ParametersBase() | ||
{ | ||
} | ||
|
||
protected ParametersBase(string query) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(query)) | ||
{ | ||
int num = query.Length; | ||
for (int i = ((query.Length > 0) && (query[0] == '?')) ? 1 : 0; i < num; i++) | ||
{ | ||
int startIndex = i; | ||
int num4 = -1; | ||
while (i < num) | ||
{ | ||
char ch = query[i]; | ||
if (ch == '=') | ||
{ | ||
if (num4 < 0) | ||
{ | ||
num4 = i; | ||
} | ||
} | ||
else if (ch == '&') | ||
{ | ||
break; | ||
} | ||
i++; | ||
} | ||
string key = null; | ||
string value = null; | ||
if (num4 >= 0) | ||
{ | ||
key = query.Substring(startIndex, num4 - startIndex); | ||
value = query.Substring(num4 + 1, (i - num4) - 1); | ||
} | ||
else | ||
{ | ||
value = query.Substring(startIndex, i - startIndex); | ||
} | ||
|
||
if (key != null) | ||
Add(Uri.UnescapeDataString(key), Uri.UnescapeDataString(value)); | ||
} | ||
} | ||
} | ||
|
||
public object this[string key] => | ||
_entries.FirstOrDefault(x => string.Compare(x.Key, key, StringComparison.Ordinal) == 0); | ||
|
||
public int Count => _entries.Count; | ||
|
||
public IEnumerable<string> Keys => | ||
_entries.Select(x => x.Key); | ||
|
||
public void Add(string key, object value) => | ||
_entries.Add(new KeyValuePair<string, object>(key, value)); | ||
|
||
public bool ContainsKey(string key) => | ||
_entries.ContainsKey(key); | ||
|
||
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() => | ||
_entries.GetEnumerator(); | ||
|
||
public T GetValue<T>(string key) => | ||
_entries.GetValue<T>(key); | ||
|
||
public IEnumerable<T> GetValues<T>(string key) => | ||
_entries.GetValues<T>(key); | ||
|
||
public bool TryGetValue<T>(string key, out T value) => | ||
_entries.TryGetValue(key, out value); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => | ||
GetEnumerator(); | ||
|
||
public override string ToString() | ||
{ | ||
var queryBuilder = new StringBuilder(); | ||
|
||
if (_entries.Count > 0) | ||
{ | ||
queryBuilder.Append('?'); | ||
var first = true; | ||
|
||
foreach (var kvp in _entries) | ||
{ | ||
if (!first) | ||
{ | ||
queryBuilder.Append('&'); | ||
} | ||
else | ||
{ | ||
first = false; | ||
} | ||
|
||
queryBuilder.Append(Uri.EscapeDataString(kvp.Key)); | ||
queryBuilder.Append('='); | ||
queryBuilder.Append(Uri.EscapeDataString(kvp.Value != null ? kvp.Value.ToString() : "")); | ||
} | ||
} | ||
|
||
return queryBuilder.ToString(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
|
||
namespace Prism.Common | ||
{ | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class ParametersExtensions | ||
{ | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static T GetValue<T>(this IEnumerable<KeyValuePair<string, object>> parameters, string key) => | ||
(T)GetValue(parameters, key, typeof(T)); | ||
|
||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static object GetValue(this IEnumerable<KeyValuePair<string, object>> parameters, string key, Type type) | ||
{ | ||
foreach (var kvp in parameters) | ||
{ | ||
if (string.Compare(kvp.Key, key, StringComparison.Ordinal) == 0) | ||
{ | ||
if (kvp.Value == null) | ||
return GetDefault(type); | ||
else if (kvp.Value.GetType() == type) | ||
return kvp.Value; | ||
else if (type.IsAssignableFrom(kvp.Value.GetType())) | ||
return kvp.Value; | ||
else | ||
return Convert.ChangeType(kvp.Value, type); | ||
} | ||
} | ||
|
||
return GetDefault(type); | ||
} | ||
|
||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static bool TryGetValue<T>(this IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value) | ||
{ | ||
foreach (var kvp in parameters) | ||
{ | ||
if (string.Compare(kvp.Key, key, StringComparison.Ordinal) == 0) | ||
{ | ||
if (kvp.Value == null) | ||
value = default; | ||
else if (kvp.Value.GetType() == typeof(T)) | ||
value = (T)kvp.Value; | ||
else if (typeof(T).IsAssignableFrom(kvp.Value.GetType())) | ||
value = (T)kvp.Value; | ||
else | ||
value = (T)Convert.ChangeType(kvp.Value, typeof(T)); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
value = default; | ||
return false; | ||
} | ||
|
||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static IEnumerable<T> GetValues<T>(this IEnumerable<KeyValuePair<string, object>> parameters, string key) | ||
{ | ||
List<T> values = new List<T>(); | ||
|
||
foreach (var kvp in parameters) | ||
{ | ||
if (string.Compare(kvp.Key, key, StringComparison.Ordinal) == 0) | ||
{ | ||
if (kvp.Value == null) | ||
values.Add(default); | ||
else if (kvp.Value.GetType() == typeof(T)) | ||
values.Add((T)kvp.Value); | ||
else if (typeof(T).IsAssignableFrom(kvp.Value.GetType())) | ||
values.Add((T)kvp.Value); | ||
else | ||
values.Add((T)Convert.ChangeType(kvp.Value, typeof(T))); | ||
} | ||
} | ||
|
||
return values.AsEnumerable(); | ||
} | ||
|
||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static bool ContainsKey(this IEnumerable<KeyValuePair<string, object>> parameters, string key) => | ||
parameters.Any(x => string.Compare(x.Key, key, StringComparison.Ordinal) == 0); | ||
|
||
private static object GetDefault(Type type) | ||
{ | ||
if (type.IsValueType) | ||
{ | ||
return Activator.CreateInstance(type); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Source/Xamarin/Prism.Forms/AppModel/AutoInitializeAttribute.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
|
||
namespace Prism.AppModel | ||
{ | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | ||
public sealed class AutoInitializeAttribute : Attribute | ||
{ | ||
public AutoInitializeAttribute(string name) : this(name, false) { } | ||
|
||
public AutoInitializeAttribute(bool isRequired) : this(null, isRequired) { } | ||
|
||
public AutoInitializeAttribute(string name, bool isRequired) | ||
{ | ||
Name = name; | ||
IsRequired = isRequired; | ||
} | ||
|
||
public bool IsRequired { get; } | ||
|
||
public string Name { get; } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Prism.AppModel | ||
{ | ||
public interface IAutoInitialize : IAbracadabra { } | ||
|
||
public interface IAbracadabra { } | ||
} |
41 changes: 41 additions & 0 deletions
41
Source/Xamarin/Prism.Forms/Services/Dialogs/RelativeContentSizeConverter.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Globalization; | ||
using Xamarin.Forms; | ||
|
||
namespace Prism.Services.Dialogs | ||
{ | ||
internal class RelativeContentSizeConverter : IValueConverter | ||
{ | ||
private double relativeSize; | ||
public double RelativeSize | ||
{ | ||
get => relativeSize; | ||
set | ||
{ | ||
if (value == 0) | ||
{ | ||
relativeSize = 1; | ||
} | ||
else if (value > 1) | ||
{ | ||
relativeSize = value / 100; | ||
} | ||
else | ||
{ | ||
relativeSize = value; | ||
} | ||
} | ||
} | ||
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
var pageSize = double.Parse(value.ToString()); | ||
return RelativeSize * pageSize; | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |