Samuel
Samuel
CC#
Created by Samuel on 12/6/2024 in #help
Decimal in clamped TextBox
I'm implementing a numerical TextBox that may clamp entered values between a min and max value. I achieved this by overriding the MetaData for the Text property with a TwoWay binding by default and an OnTextChanged handler. One of the hurdles along the way was partially entered values such as -, 0., 1.3, etc. I overcame this with a regex. The issue I have is that I'm unable to enter a decimal into the TextBox despite being allowed by the regex ^(-?\d+\.|-0?)$. Here is my on changed handler.
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var s = (string)e.NewValue;
var regex = new Regex(@"^(-?\d+\.|-0?)$");
if (string.IsNullOrEmpty(s) || regex.Match(s).Success)
return;

if (double.TryParse((string)e.NewValue, out var oldValue))
{
if (!(bool)d.GetValue(EnforceRangeProperty))
return;

var min = (double)d.GetValue(MinProperty);
var max = (double)d.GetValue(MaxProperty);

var newValue = Math.Max(min, oldValue);
newValue = Math.Min(max, newValue);

// ReSharper disable once CompareOfFloatsByEqualityOperator
if (newValue == oldValue)
return;

d.SetValue(TextProperty, newValue.ToString(CultureInfo.InvariantCulture));
return;
}

// If we can't parse the text, revert to the old value.
d.SetValue(TextProperty, (string)e.OldValue);
}
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var s = (string)e.NewValue;
var regex = new Regex(@"^(-?\d+\.|-0?)$");
if (string.IsNullOrEmpty(s) || regex.Match(s).Success)
return;

if (double.TryParse((string)e.NewValue, out var oldValue))
{
if (!(bool)d.GetValue(EnforceRangeProperty))
return;

var min = (double)d.GetValue(MinProperty);
var max = (double)d.GetValue(MaxProperty);

var newValue = Math.Max(min, oldValue);
newValue = Math.Min(max, newValue);

// ReSharper disable once CompareOfFloatsByEqualityOperator
if (newValue == oldValue)
return;

d.SetValue(TextProperty, newValue.ToString(CultureInfo.InvariantCulture));
return;
}

// If we can't parse the text, revert to the old value.
d.SetValue(TextProperty, (string)e.OldValue);
}
What changes can I make to allow for the entry of a decimal point? Edit: I've also realised that I cannot enter -0, subsequently -0.
10 replies
CC#
Created by Samuel on 7/9/2024 in #help
Embedding a 3D view with OpenGL backend within a WPF view
I'm not looking for detailed steps. I'm just need to know how to render directly to a control within a WPF application. I have played around with Silk.NET which I have loved. Is there a way I can use this with a view within WPF?
7 replies
CC#
Created by Samuel on 10/17/2023 in #help
✅ Using a scoped service within an IHostedService
I have a IHostedService implementation that sends a Mediatr request every hour. The handler uses a DbContext to carry out its work. When sending the request, I get the following exception:
Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Cannot resolve 'MediatR.IRequestHandler`1[AuctionData.Application.Features.ProcessAuctionData+ProcessAuctionDataCommand]' from root provider because it requires scoped service 'AuctionData.Application.Data.AuctionDbContext'.'
Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Cannot resolve 'MediatR.IRequestHandler`1[AuctionData.Application.Features.ProcessAuctionData+ProcessAuctionDataCommand]' from root provider because it requires scoped service 'AuctionData.Application.Data.AuctionDbContext'.'
I have tried to create an async scope, but I still get the exception.
private async void RequestAuctionDataProcessing(object? state)
{
await using (var scope = _serviceProvider.CreateAsyncScope())
{
await _mediator.Send(new ProcessAuctionDataCommand(1305));
}
}
private async void RequestAuctionDataProcessing(object? state)
{
await using (var scope = _serviceProvider.CreateAsyncScope())
{
await _mediator.Send(new ProcessAuctionDataCommand(1305));
}
}
I have listed the project in GitHub. Here is a link to where the functionality lies https://github.com/Scharps/AuctionData/blob/Scharps/issue15/AuctionData.ApplicationCore/Features/ProcessAuctionData.cs Thank you for reading.
8 replies
CC#
Created by Samuel on 2/24/2023 in #help
❔ ✅ Splitting string without allocation
I'm writing a JsonConverter for Vector3 and I'd like to do this without allocating too much memory. I've written this extension method to split the string as ReadOnlyMemory. I wanted to use ReadOnlySpan but I'm unable to use this type in IEnumerable<T>.
public static IEnumerable<ReadOnlyMemory<T>> Split<T>(this ReadOnlyMemory<T> memory, T separator)
where T: IEquatable<T>
{
int start = 0;
int end = 0;
while (end < memory.Length)
{
if (memory.Span[end].Equals(separator) || end == memory.Length)
{
yield return memory.Slice(start, end - start);
start = end + 1;
}
end++;
}

}
public static IEnumerable<ReadOnlyMemory<T>> Split<T>(this ReadOnlyMemory<T> memory, T separator)
where T: IEquatable<T>
{
int start = 0;
int end = 0;
while (end < memory.Length)
{
if (memory.Span[end].Equals(separator) || end == memory.Length)
{
yield return memory.Slice(start, end - start);
start = end + 1;
}
end++;
}

}
This is my implementation of Read for JsonConverter<Vector3>
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
ReadOnlyMemory<char> vectorString = reader.GetString().AsMemory();
var vectorValues = vectorString.Split(',').Select(s => float.Parse(s)).ToArray(); // Unable to parse `s` as it's not a string. What do?
return new Vector3 { X = vectorValues[0], Y = vectorValues[1], Z = vectorValues[2] };
}
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
ReadOnlyMemory<char> vectorString = reader.GetString().AsMemory();
var vectorValues = vectorString.Split(',').Select(s => float.Parse(s)).ToArray(); // Unable to parse `s` as it's not a string. What do?
return new Vector3 { X = vectorValues[0], Y = vectorValues[1], Z = vectorValues[2] };
}
I'd like to know if there are any ways to make my Split method better, and to somehow get my Read method to work. Thanks for reading
31 replies