Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/141-propertysetterscontextextens…
Browse files Browse the repository at this point in the history
…ion-binding-multibinding-and-dynamicresource-support' into develop
  • Loading branch information
gonultasmf committed Oct 19, 2024
2 parents 9308667 + 9b619ce commit 40a7784
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 105 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,20 @@ public class NumericValidationTriggerAction : TriggerAction<Entry>
}
```

And here is the definition of the `NumericValidationTriggerAction` class:

```csharp
public class NumericValidationTriggerAction : TriggerAction<Entry>
{
protected override void Invoke(Entry entry)
{
double result;
bool isValid = Double.TryParse(entry.Text, out result);
entry.TextColor = isValid ? Colors.Black : Colors.Red;
}
}
```

# .NET built-in Hot-Reload

Additionally, the FmgLib.MauiMarkup library includes hot reload support to make the development process faster and more efficient.
Expand Down Expand Up @@ -1134,20 +1148,6 @@ public partial class ExamplePage : ContentPage, IFmgLibHotReload
}
```

And here is the definition of the `NumericValidationTriggerAction` class:

```csharp
public class NumericValidationTriggerAction : TriggerAction<Entry>
{
protected override void Invoke(Entry entry)
{
double result;
bool isValid = Double.TryParse(entry.Text, out result);
entry.TextColor = isValid ? Colors.Black : Colors.Red;
}
}
```

# Extensions for 3rd Party Controls

FmgLib.MauiMarkup library can also generate extension methods for controls from third-party libraries. To achieve this, you should utilize the `MauiMarkupAttribute` provided by FmgLib.MauiMarkup.
Expand Down
145 changes: 73 additions & 72 deletions sample/MyOrderApp/MyOrderApp.csproj
Original file line number Diff line number Diff line change
@@ -1,84 +1,85 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0-android</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('osx'))">$(TargetFrameworks);net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-ios;net8.0-maccatalyst;net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<TargetFrameworks>$(TargetFrameworks);net8.0</TargetFrameworks>
<OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>
<PropertyGroup>
<TargetFrameworks>net8.0-android</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('osx'))">$(TargetFrameworks);net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-ios;net8.0-maccatalyst;net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<TargetFrameworks>$(TargetFrameworks);net8.0</TargetFrameworks>
<OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>

<!-- .NET MAUI -->
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<!-- .NET MAUI -->
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>

<!-- Project Options -->
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MyOrderApp</RootNamespace>
<!-- Project Options -->
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MyOrderApp</RootNamespace>

<!-- Display name -->
<ApplicationTitle>MyOrderApp</ApplicationTitle>
<!-- Display name -->
<ApplicationTitle>MyOrderApp</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.companyname.myorderapp</ApplicationId>
<ApplicationIdGuid>0f1f5ccb-47f5-4b4b-8927-96cc6840eea4</ApplicationIdGuid>
<!-- App Identifier -->
<ApplicationId>com.companyname.myorderapp</ApplicationId>
<ApplicationIdGuid>0f1f5ccb-47f5-4b4b-8927-96cc6840eea4</ApplicationIdGuid>

<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>

<!-- Target Platform Options -->
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<!-- Target Platform Options -->
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>

<!-- Minimum Target OS Version for Windows Platform -->
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>
<!-- Minimum Target OS Version for Windows Platform -->
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" Color="#512BD4" />
<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512BD4" BaseSize="128,128" />
<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" Color="#512BD4" />
<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512BD4" BaseSize="128,128" />
<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<None Remove="MyOrderApp.code-workspace" />
<None Remove="Resources\Images\background.jpg" />
<None Remove="Resources\Images\basket.png" />
<None Remove="Resources\Images\categories.png" />
<None Remove="Resources\Images\chevron.png" />
<None Remove="Resources\Images\favorite.png" />
<None Remove="Resources\Images\favorites.png" />
<None Remove="Resources\Images\home.png" />
<None Remove="Resources\Images\not_favorite.png" />
<None Remove="Resources\Images\products.png" />
<None Remove="Resources\Images\remove.png" />
<None Remove="Resources\Images\white_bread.png" />
<None Remove="Resources\Raw\completed.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="FmgLib.MauiMarkup" Version="8.8.1" />
<PackageReference Include="LiteDB" Version="5.0.20" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.*" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.40" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.40" />
<PackageReference Include="CommunityToolkit.Maui" Version="9.0.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="SkiaSharp.Extended.UI.Maui" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<None Remove="MyOrderApp.code-workspace" />
<None Remove="Resources\Images\background.jpg" />
<None Remove="Resources\Images\basket.png" />
<None Remove="Resources\Images\categories.png" />
<None Remove="Resources\Images\chevron.png" />
<None Remove="Resources\Images\favorite.png" />
<None Remove="Resources\Images\favorites.png" />
<None Remove="Resources\Images\home.png" />
<None Remove="Resources\Images\not_favorite.png" />
<None Remove="Resources\Images\products.png" />
<None Remove="Resources\Images\remove.png" />
<None Remove="Resources\Images\white_bread.png" />
<None Remove="Resources\Raw\completed.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\FmgLib.MauiMarkup\FmgLib.MauiMarkup.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="LiteDB" Version="5.0.20" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.*" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.40" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.40" />
<PackageReference Include="CommunityToolkit.Maui" Version="9.0.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="SkiaSharp.Extended.UI.Maui" Version="2.0.0" />
</ItemGroup>
</Project>
6 changes: 2 additions & 4 deletions src/FmgLib.MauiMarkup/Core/Builder/PropertyBindingBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ public bool Build()
if (path != null)
{
Context.BindableObject.SetBinding(Context.Property, new Binding(path, bindingMode, converter, converterParameter, stringFormat, source)
{
FallbackValue = fallbackValue,
TargetNullValue = targetNullValue
});
.FallbackValue(fallbackValue)
.TargetNullValue(targetNullValue));
return true;
}

Expand Down
149 changes: 149 additions & 0 deletions src/FmgLib.MauiMarkup/Core/Builder/PropertySettersBindingBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System.Globalization;

namespace FmgLib.MauiMarkup;

public sealed class PropertySettersBindingBuilder<T> : IPropertySettersBuilder<T>
{
public class ValueConverter : IValueConverter
{
internal Func<object, T> ConvertFunction;

internal Func<T, object> ConvertBackFunction;

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && ConvertFunction != null)
{
return ConvertFunction(value);
}

return null;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && ConvertBackFunction != null)
{
return ConvertBackFunction((T)value);
}

return null;
}
}

private string path;

private BindingMode bindingMode;

private IValueConverter converter;

private ValueConverter valueConverter;

private string converterParameter;

private string stringFormat;

private object source;

private object fallbackValue;

private object targetNullValue;

public PropertySettersContext<T> Context { get; set; }

public PropertySettersBindingBuilder(PropertySettersContext<T> context)
{
Context = context;
}

public bool Build()
{
if (path != null)
{
Context.XamlSetters.Add(
new Setter
{
Property = Context.Property,
Value = new Binding(path, bindingMode, converter, converterParameter, stringFormat, source)
.FallbackValue(fallbackValue)
.TargetNullValue(targetNullValue)
}
);
return true;
}

return false;
}

public PropertySettersBindingBuilder<T> Path(string path)
{
this.path = path;
return this;
}

public PropertySettersBindingBuilder<T> StringFormat(string stringFormat)
{
this.stringFormat = stringFormat;
return this;
}

public PropertySettersBindingBuilder<T> BindingMode(BindingMode bindingMode)
{
this.bindingMode = bindingMode;
return this;
}

public PropertySettersBindingBuilder<T> Converter(IValueConverter converter)
{
this.converter = converter;
return this;
}

public PropertySettersBindingBuilder<T> Parameter(string converterParameter)
{
this.converterParameter = converterParameter;
return this;
}

public PropertySettersBindingBuilder<T> Source(object source)
{
this.source = source;
return this;
}

public PropertySettersBindingBuilder<T> FallbackValue(object fallbackValue)
{
this.fallbackValue = fallbackValue;
return this;
}

public PropertySettersBindingBuilder<T> TargetNullValue(object targetNullValue)
{
this.targetNullValue = targetNullValue;
return this;
}

public PropertySettersBindingBuilder<T> Convert<Q>(Func<Q, T> convert)
{
if (valueConverter == null)
{
valueConverter = new ValueConverter();
}

valueConverter.ConvertFunction = (object e) => convert((Q)e);
converter = valueConverter;
return this;
}

public PropertySettersBindingBuilder<T> ConvertBack<Q>(Func<T, Q> convert)
{
if (valueConverter == null)
{
valueConverter = new ValueConverter();
}

valueConverter.ConvertBackFunction = (T e) => convert(e);
converter = valueConverter;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace FmgLib.MauiMarkup;

public sealed class PropertySettersDynamicResourcesBuilder<T> : IPropertySettersBuilder<T>
{
public PropertySettersContext<T> Context { get; set; }

string key = null;

public PropertySettersDynamicResourcesBuilder(PropertySettersContext<T> context)
{
Context = context;
}

public bool Build()
{
if (!string.IsNullOrEmpty(key))
{
Context.XamlSetters.Add(new Setter { Property = Context.Property, Value = new Microsoft.Maui.Controls.Internals.DynamicResource(key) });
}

return false;
}

public PropertySettersDynamicResourcesBuilder<T> DynamicResource(string key) { this.key = key; return this; }
}
Loading

0 comments on commit 40a7784

Please sign in to comment.