tips on developing resolution independent application

前端 未结 4 2118
时光说笑
时光说笑 2020-12-04 12:59

Is it a good practice to find the workarea measurement and set some properties in code so that it could be bound to Control\'s margin or height/Width properties in xaml?

4条回答
  •  有刺的猬
    2020-12-04 13:31

    going with what mr JacobJ did I made my own spinoff. I made a Interface on wich we base change

    public interface IResolutionDecorator
    {
        double ActualWidth { get; }
        double ActualHeight { get; }
        double ResolutionHeight { get; set; }
        double ResolutionWidth { get; set; }
        object CurentContent { get; }
    }
    

    and for this Interface i have extension

        #region IResolutionDecorator
        private static double OnCoerceScaleValue(double value)
        {
            return double.IsNaN(value) ? 1d : Math.Max(0.1, value);
        }
    
        public static void UpdateScale(this IResolutionDecorator source)
        {
            if (source.CurentContent is Visual visual)
            {
                double yScale = source.ActualHeight / source.ResolutionHeight;
                double xScale = source.ActualWidth / source.ResolutionWidth;
                double value = Math.Min(xScale, yScale);
                double ScaleValue = (double)OnCoerceScaleValue(value);
                visual.SetValue(Grid.LayoutTransformProperty, new ScaleTransform(ScaleValue, ScaleValue, 0, 0));
            }
        }
        #endregion
    

    now we only need add Parameters that we lack in main window and set this.UpdateScale() in event sizeChanged in main grid

    public partial class MainWindow : Window,  IResolutionDecorator
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Height = ResolutionHeight;
            this.Width = ResolutionWidth;
        }
        #region IResolutionDecorator
        public object CurentContent { get{ return this.Content; } }
        public double ResolutionHeight { get; set; } = 400d;
        public double ResolutionWidth { get; set; } = 800d;
        #endregion
    
        private void MainGrid_SizeChanged(object sender, EventArgs e)
        {
            this.UpdateScale();
        }
    }
    

提交回复
热议问题