M.
M.
Explore posts from servers
CDCloudflare Developers
Created by M. on 10/14/2024 in #general-help
Miss click
okay, thanks
3 replies
CC#
Created by M. on 1/15/2024 in #help
✅ How to Retrieve the Selected Item Value from a ComboBox in C# (AvaloniaUI)
thank you very much, I will take a look
22 replies
CC#
Created by M. on 1/15/2024 in #help
✅ How to Retrieve the Selected Item Value from a ComboBox in C# (AvaloniaUI)
Ok thank you and optimization level does it change anything?
22 replies
CC#
Created by M. on 1/15/2024 in #help
✅ How to Retrieve the Selected Item Value from a ComboBox in C# (AvaloniaUI)
Thank You, i succeeded. if it can help anyone, I did it like this:
c#
private ComboBox ComboBoxOutputFormat;


private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
ComboBoxOutputFormat = this.FindControl<ComboBox>("ComboBoxOutputFormat");
ComboBoxOutputFormat.SelectionChanged += ComboBoxOutputFormat_SelectionChanged;
}

private void ComboBoxOutputFormat_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

string valeurSelectionnee = (ComboBoxOutputFormat.SelectedItem as ComboBoxItem)?.Content?.ToString();

if (!string.IsNullOrEmpty(valeurSelectionnee))
{
Console.WriteLine($"Option sélectionnée : {valeurSelectionnee}");
}
}
c#
private ComboBox ComboBoxOutputFormat;


private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
ComboBoxOutputFormat = this.FindControl<ComboBox>("ComboBoxOutputFormat");
ComboBoxOutputFormat.SelectionChanged += ComboBoxOutputFormat_SelectionChanged;
}

private void ComboBoxOutputFormat_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

string valeurSelectionnee = (ComboBoxOutputFormat.SelectedItem as ComboBoxItem)?.Content?.ToString();

if (!string.IsNullOrEmpty(valeurSelectionnee))
{
Console.WriteLine($"Option sélectionnée : {valeurSelectionnee}");
}
}
22 replies
CC#
Created by M. on 1/15/2024 in #help
✅ How to Retrieve the Selected Item Value from a ComboBox in C# (AvaloniaUI)
Thanks, I'll look at it all
22 replies
CC#
Created by M. on 1/15/2024 in #help
✅ How to Retrieve the Selected Item Value from a ComboBox in C# (AvaloniaUI)
No description
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
Thank you for your help, I succeeded and it works perfectly
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
MainWindow.axaml.cs
c#
namespace AvaloniaApp.Views
{
public partial class MainWindow : Window
{
static MainWindow Instance { get; private set; }
private readonly INavigationService _navigationService;
public MainWindow()
{
InitializeComponent();
Instance = this;
_navigationService = new AvaloniaNavigationService(Locator.Current.GetService<IServiceProvider>());

NavigateTo<HomeView>();}
private void InitializeComponents()
{
AvaloniaXamlLoader.Load(this);
}
private void NavigateTo<T>() where T : UserControl, new()
{
_navigationService.NavigateToAsync(typeof(T).Name);
}
}
c#
namespace AvaloniaApp.Views
{
public partial class MainWindow : Window
{
static MainWindow Instance { get; private set; }
private readonly INavigationService _navigationService;
public MainWindow()
{
InitializeComponent();
Instance = this;
_navigationService = new AvaloniaNavigationService(Locator.Current.GetService<IServiceProvider>());

NavigateTo<HomeView>();}
private void InitializeComponents()
{
AvaloniaXamlLoader.Load(this);
}
private void NavigateTo<T>() where T : UserControl, new()
{
_navigationService.NavigateToAsync(typeof(T).Name);
}
}
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
QueryPropertyAttribute.cs
c#
namespace AvaloniaApp.Navigation{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class QueryPropertyAttribute : Attribute
{
public QueryPropertyAttribute(string name, string queryId) => (Name, QueryId) = (name, queryId);
public string Name { get; }
public string QueryId { get; }
}
}
c#
namespace AvaloniaApp.Navigation{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class QueryPropertyAttribute : Attribute
{
public QueryPropertyAttribute(string name, string queryId) => (Name, QueryId) = (name, queryId);
public string Name { get; }
public string QueryId { get; }
}
}
Routing.cs
c#
namespace AvaloniaApp.Navigation
{
public static class Routing
{
private static readonly Dictionary<string, Type> _registeredRoutes = new Dictionary<string, Type>();

public static UserControl GetOrCreateContent(string route, IServiceProvider serviceProvider) =>
_registeredRoutes.TryGetValue(route, out var type) ?
serviceProvider.GetService(type) as UserControl ?? throw new InvalidOperationException("") :
throw new InvalidOperationException("");

public static void RegisterRoute(string route, Type type)
{
if (string.IsNullOrEmpty(route) || !type.IsAssignableTo(typeof(UserControl)))
throw new ArgumentException();

_registeredRoutes.Add(route, type);
}
}
}
c#
namespace AvaloniaApp.Navigation
{
public static class Routing
{
private static readonly Dictionary<string, Type> _registeredRoutes = new Dictionary<string, Type>();

public static UserControl GetOrCreateContent(string route, IServiceProvider serviceProvider) =>
_registeredRoutes.TryGetValue(route, out var type) ?
serviceProvider.GetService(type) as UserControl ?? throw new InvalidOperationException("") :
throw new InvalidOperationException("");

public static void RegisterRoute(string route, Type type)
{
if (string.IsNullOrEmpty(route) || !type.IsAssignableTo(typeof(UserControl)))
throw new ArgumentException();

_registeredRoutes.Add(route, type);
}
}
}
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
INavigationService.cs
c#
namespace AvaloniaApp.Navigation
{
public interface INavigationService
{
Task NavigateToAsync(string route, IDictionary<string, object>? parameters = null);
}
}
c#
namespace AvaloniaApp.Navigation
{
public interface INavigationService
{
Task NavigateToAsync(string route, IDictionary<string, object>? parameters = null);
}
}
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
namespace AvaloniaApp.Navigation
{
internal class AvaloniaNavigationService : INavigationService
{
private readonly IServiceProvider _services;
private UserControl? _view;
private bool _clearNavigationStack;

public AvaloniaNavigationService(IServiceProvider services)
{
_services = services;
}

public Task NavigateToAsync(string route, IDictionary<string, object>? parameters = null)
=> OnNavigateAsync(route, parameters);

private async Task OnNavigateAsync(string route, IDictionary<string, object>? parameters = null)
{
await Task.Yield();

var window = (Window)Avalonia.Application.Current.Windows[0]; // Use the appropriate index for your main window
bool clearNavigationStack = route.StartsWith("//") ? _clearNavigationStack = true : false;

var view = Routing.GetOrCreateContent(route.Substring(2), _services);
if (parameters != null) ApplyQueryParameters(view, parameters);

window.Content = _view = (UserControl)view;
_clearNavigationStack = clearNavigationStack;
}

private void ApplyQueryParameters(object instance, IDictionary<string, object> parameters)
{
var t = instance.GetType();

foreach (var attr in t.GetCustomAttributes<QueryPropertyAttribute>())
{
if (parameters.TryGetValue(attr.QueryId, out var value))
{
var prop = t.GetProperty(attr.Name);
if (prop != null && prop.CanWrite && prop.PropertyType.IsAssignableFrom(value.GetType()))
prop.SetValue(instance, value);
}
}

if (instance is IQueryAttributable attributable)
attributable.ApplyQueryAttributes(parameters);
}
}
}
namespace AvaloniaApp.Navigation
{
internal class AvaloniaNavigationService : INavigationService
{
private readonly IServiceProvider _services;
private UserControl? _view;
private bool _clearNavigationStack;

public AvaloniaNavigationService(IServiceProvider services)
{
_services = services;
}

public Task NavigateToAsync(string route, IDictionary<string, object>? parameters = null)
=> OnNavigateAsync(route, parameters);

private async Task OnNavigateAsync(string route, IDictionary<string, object>? parameters = null)
{
await Task.Yield();

var window = (Window)Avalonia.Application.Current.Windows[0]; // Use the appropriate index for your main window
bool clearNavigationStack = route.StartsWith("//") ? _clearNavigationStack = true : false;

var view = Routing.GetOrCreateContent(route.Substring(2), _services);
if (parameters != null) ApplyQueryParameters(view, parameters);

window.Content = _view = (UserControl)view;
_clearNavigationStack = clearNavigationStack;
}

private void ApplyQueryParameters(object instance, IDictionary<string, object> parameters)
{
var t = instance.GetType();

foreach (var attr in t.GetCustomAttributes<QueryPropertyAttribute>())
{
if (parameters.TryGetValue(attr.QueryId, out var value))
{
var prop = t.GetProperty(attr.Name);
if (prop != null && prop.CanWrite && prop.PropertyType.IsAssignableFrom(value.GetType()))
prop.SetValue(instance, value);
}
}

if (instance is IQueryAttributable attributable)
attributable.ApplyQueryAttributes(parameters);
}
}
}
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
Hi, So I took an example from your code, and I just have two errors that I can't resolve in my AvaloniaNavigationService.cs - Cannot resolve symbol 'MainWindow' | Line : var window = (Window)Avalonia.Application.Current.MainWindow; - Cannot resolve symbol 'IQueryAttributable' | Line: if (instance is IQueryAttributable attributable) - 📂 Navigation - AvaloniaNavigationService.cs - INavigationService.cs - QueryPropertyAttribute.cs - Routing.cs My codes : AvaloniaNavigationService.cs
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
Thank you for everything, I will try to implement that.
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
Oh, sorry. Do you have the part of the code that corresponds to what I want to do, or it's okay, I'll look for it.
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
Because my issue is simply that I can't change the displayed user control from another user control that is already displayed. If I click on buttons that change the user control directly from the main window, it works perfectly.
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
Sorry, but that doesn't help me much.
22 replies
CC#
Created by M. on 1/12/2024 in #help
✅ Issue with Triggering View Change in Avalonia using C#
Thank you, I'll look into that right away.
22 replies