Avalonia UI缩放问题

问题描述

我正在使用Avalonia开发跨平台的桌面MVVM应用程序,而我的问题却很简单:

我想使整个窗口适应目标设备的显示分辨率。例如,UI应始终缩放以覆盖显示的25%到50%,但不应更大。缩放比例应包括所有字体大小,宽度和高度属性等。在此处可视化它是在4K桌面上的外观,即它的外观:

desktop

在分辨率较低的Linux笔记本电脑上,它看起来像这样:

enter image description here

我想做的是根据显示分辨率放大或缩小所有内容,使其看起来不像垃圾,这样字体始终清晰可读(因此字体也需要缩放)。 (可能还有WinAPI要求进行UI缩放,但是该解决方案也应该在linux和OSX上也可以使用)

如何使用Avalonia来实现?还是有实现此目的的首选方法?

请注意,应用程序的宽高比应保持恒定,并且应用程序不应覆盖整个屏幕。

到目前为止我的想法:

  • 我可能可以为每个大小参数创建绑定,并使用某种缩放因子在应用程序启动时重新计算最佳大小,但这将需要大量新代码,并且使用字体来实现会很麻烦大小。

  • 也许可以创建绑定和一堆样式预设,例如CSS媒体查询?不过那也是一堆工作。

有没有更好的方法来达到预期的效果?

解决方法

我可能可以为每个大小参数创建绑定,并使用某种缩放因子在应用程序启动时重新计算最佳大小,但这将需要一堆新代码,并且使用字体大小很难实现。

这最终就是我所做的。我必须创建一个完整的单独的命名空间来容纳执行此操作所需的所有类。因此,如果有人感兴趣,则在此答案but I put my full code on GitHub中放入太多代码。

基本上,一切都归结为像这样遍历Avalonia LogicalTree:

/// <summary>
/// Recursively registers all <see cref="ILogical"/>s in <paramref name="logicals"/> to the <paramref name="bindingContext"/>.
/// </summary>
/// <param name="logicals">A <see cref="Queue{T}"/> containing the root controls that will be recursively registered.</param>
/// <param name="bindingContext">The <see cref="BindingContext"/> the <see cref="ScalableObject"/>s will be registered to.</param>
public static void RegisterControls(Queue<IEnumerable<ILogical>> logicals,BindingContext bindingContext)
{
    while (logicals.Count > 0)
    {
        IEnumerable<ILogical> children = logicals.Dequeue();
        foreach (ILogical child in children)
        {
            logicals.Enqueue(child.GetLogicalChildren());
            if (child is AvaloniaObject avaloniaObject)
            {
                ScalableObject scalableObject = new ScalableObject(avaloniaObject);
                bindingContext.Add(scalableObject);
            }
        }
    }
}

我的ScalableObject的构造函数如下:

/// <summary>
/// Initializes a new <see cref="ScalableObject"/> from the provided <paramref name="avaloniaObject"/>.
/// </summary>
/// <param name="avaloniaObject">The <see cref="AvaloniaObject"/> to be mapped to this new instance of <see cref="ScalableObject"/>.</param>
public ScalableObject(AvaloniaObject avaloniaObject)
{
    if (avaloniaObject is TextBlock textBlock)
    {
        Register(avaloniaObject,TextBlock.FontSizeProperty,textBlock.FontSize);
    }
    if (avaloniaObject is TemplatedControl templatedControl)
    {
        Register(avaloniaObject,TemplatedControl.FontSizeProperty,templatedControl.FontSize);
    }
    if (avaloniaObject is Border border)
    {
        Register(avaloniaObject,Border.CornerRadiusProperty,border.CornerRadius);
    }
    // .... This goes on like this for a while
}

然后我可以对所有已创建的绑定应用新的UI缩放因子,如下所示:

/// <summary>
/// Applies the specified <paramref name="scalingFactor"/> to this <see cref="ScalableObject"/> and all of it's children.
/// </summary>
/// <param name="scalingFactor">The scaling factor to be applied to all <see cref="IScalable"/>s of this <see cref="ScalableObject"/>.</param>
public void ApplyScaling(double scalingFactor)
{
    PreScalingAction?.Invoke();
    foreach (IScalable binding in Bindings.Values)
    {
        binding.ApplyScaling(scalingFactor);
    }
    PostScalingAction?.Invoke();
}

同样,要在此答案中添加太多代码,但希望它能使您对我的解决方案的实现方式有所了解。

这是结果:

enter image description here

可以缩放为此

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...