Browse Source

Started adding general purpose IValueConverters.

pull/297/head
Steven Kirk 11 years ago
parent
commit
30e3b28c93
  1. 4
      samples/XamlTestApplication/XamlTestApplication.csproj
  2. 33
      samples/XamlTestApplicationPcl/StringNullOrEmpty.cs
  3. 2
      samples/XamlTestApplicationPcl/TextBox.paml
  4. 1
      samples/XamlTestApplicationPcl/XamlTestApplicationPcl.csproj
  5. 5
      src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs
  6. 48
      src/Markup/Perspex.Markup/FuncValueConverter.cs
  7. 5
      src/Markup/Perspex.Markup/Perspex.Markup.csproj
  8. 32
      src/Markup/Perspex.Markup/Properties/AssemblyInfo.cs
  9. 21
      src/Markup/Perspex.Markup/StringConverters.cs
  10. 14
      src/Perspex.Base/Utilities/TypeUtilities.cs

4
samples/XamlTestApplication/XamlTestApplication.csproj

@ -84,6 +84,10 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Markup\Perspex.Markup\Perspex.Markup.csproj">
<Project>{6417e941-21bc-467b-a771-0de389353ce6}</Project>
<Name>Perspex.Markup</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\Perspex.Animation\Perspex.Animation.csproj">
<Project>{D211E587-D8BC-45B9-95A4-F297C8FA5200}</Project>
<Name>Perspex.Animation</Name>

33
samples/XamlTestApplicationPcl/StringNullOrEmpty.cs

@ -1,33 +0,0 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Globalization;
using Perspex;
using Perspex.Markup;
namespace XamlTestApplication
{
public class StringNullOrEmpty : IValueConverter
{
public static readonly StringNullOrEmpty Instance = new StringNullOrEmpty();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return true;
}
else
{
var s = value as string;
return s != null ? string.IsNullOrEmpty(s) : PerspexProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

2
samples/XamlTestApplicationPcl/TextBox.paml

@ -27,7 +27,7 @@
<TextBlock Name="watermark"
Opacity="0.5"
Text="{TemplateBinding Watermark}"
IsVisible="{TemplateBinding Path=Text, Converter={Static local:StringNullOrEmpty.Instance}}"/>
IsVisible="{TemplateBinding Path=Text, Converter={Static StringConverters.NullOrEmpty}}"/>
<TextPresenter Name="PART_TextPresenter"
CaretIndex="{TemplateBinding CaretIndex}"
SelectionStart="{TemplateBinding SelectionStart}"

1
samples/XamlTestApplicationPcl/XamlTestApplicationPcl.csproj

@ -41,7 +41,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringNullOrEmpty.cs" />
<Compile Include="ViewModels\MainWindowViewModel.cs" />
<Compile Include="ViewModels\TestItem.cs" />
<Compile Include="ViewModels\TestNode.cs" />

5
src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs

@ -61,8 +61,9 @@ namespace Perspex.Markup.Xaml.Context
var forcedAssemblies = new[]
{
typeof (Control),
typeof(Style)
typeof(Control),
typeof(Style),
typeof(IValueConverter),
}.Select(t => t.GetTypeInfo().Assembly);
foreach (var nsa in

48
src/Markup/Perspex.Markup/FuncValueConverter.cs

@ -0,0 +1,48 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Globalization;
using Perspex.Utilities;
namespace Perspex.Markup
{
/// <summary>
/// A general purpose <see cref="IValueConverter"/> that uses a <see cref="Func{T1, TResult}"/>
/// to provide the converter logic.
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
public class FuncValueConverter<TIn, TOut> : IValueConverter
{
private Func<TIn, TOut> _convert;
/// <summary>
/// Initializes a new instance of the <see cref="FuncValueConverter{TIn, TOut}"/> class.
/// </summary>
/// <param name="convert">The convert function.</param>
public FuncValueConverter(Func<TIn, TOut> convert)
{
_convert = convert;
}
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is TIn || (value == null && TypeUtilities.AcceptsNull(typeof(TIn))))
{
return _convert((TIn)value);
}
else
{
return PerspexProperty.UnsetValue;
}
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

5
src/Markup/Perspex.Markup/Perspex.Markup.csproj

@ -36,6 +36,9 @@
<DocumentationFile>bin\Release\Perspex.Markup.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\Shared\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Data\ExpressionNodeBuilder.cs" />
<Compile Include="Data\ExpressionParseException.cs" />
<Compile Include="Data\ExpressionSubject.cs" />
@ -49,6 +52,8 @@
<Compile Include="Data\PropertyAccessorNode.cs" />
<Compile Include="Data\ExpressionNode.cs" />
<Compile Include="Data\ExpressionObserver.cs" />
<Compile Include="FuncValueConverter.cs" />
<Compile Include="StringConverters.cs" />
<Compile Include="DefaultValueConverter.cs" />
<Compile Include="IValueConverter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

32
src/Markup/Perspex.Markup/Properties/AssemblyInfo.cs

@ -1,32 +1,10 @@
using System.Resources;
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System.Reflection;
using Perspex.Metadata;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Perspex.Markup")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Perspex.Markup")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: XmlnsDefinition("https://github.com/perspex", "Perspex.Markup")]
[assembly: InternalsVisibleTo("Perspex.Markup.UnitTests")]

21
src/Markup/Perspex.Markup/StringConverters.cs

@ -0,0 +1,21 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Globalization;
using Perspex.Utilities;
namespace Perspex.Markup
{
/// <summary>
/// Provides a set of useful <see cref="IValueConverter"/>s for working with string values.
/// </summary>
public static class StringConverters
{
/// <summary>
/// A value converter that returns true if the input string is null or an empty string.
/// </summary>
public static readonly IValueConverter NullOrEmpty =
new FuncValueConverter<string, bool>(x => string.IsNullOrEmpty(x));
}
}

14
src/Perspex.Base/Utilities/TypeUtilities.cs

@ -27,6 +27,17 @@ namespace Perspex.Utilities
{ typeof(short), new List<Type> { typeof(byte) } }
};
/// <summary>
/// Returns a value indicating whether null can be assigned to the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>True if the type accepts null values; otherwise false.</returns>
public static bool AcceptsNull(Type type)
{
var t = type.GetTypeInfo();
return !t.IsValueType || (t.IsGenericType && (t.GetGenericTypeDefinition() == typeof(Nullable<>)));
}
/// <summary>
/// Try to cast a value to a type, using implicit conversions if possible.
/// </summary>
@ -40,9 +51,8 @@ namespace Perspex.Utilities
if (value == null)
{
var t = to.GetTypeInfo();
result = null;
return !t.IsValueType || (t.IsGenericType && (t.GetGenericTypeDefinition() == typeof(Nullable<>)));
return AcceptsNull(to);
}
var from = value.GetType();

Loading…
Cancel
Save