关于开发独立分辨率应用的技巧

时间:2010-07-07 09:07:51

标签: c# wpf resolution-independence

找到工作区测量并在代码中设置一些属性以便它可以绑定到xaml中的Control的边距或高度/宽度属性是一个好习惯吗?

我这样做是为了让我的窗口根据可用的工作区调整大小。

const int w = SystemParameters.WorkArea.Width;
const int h = SystemParameters.WorkArea.Height;

public Thickness OuterGridMargin { get; }

MainViewModel()
{
    OuterGridMargin = new Thickness(w/5,h/6,w/5,h/4);
}

XAML:

<Grid Margin="{Binding OuterGridMargin}" />

我为一些外部容器执行此操作,以便布局不会以较低的分辨率混淆。目前我在20英寸的1600x900 res(96 dpi)下工作。我的应用程序是小工具,没有常规窗口。

我想知道是否有其他替代方法。

搜索[wpf]分辨率] 1提出了很多解决类似问题的问题,但我仍然陷入困境,无法得出如何实现良好的分辨率无关布局的结论。

4 个答案:

答案 0 :(得分:48)

有两种方法可以处理WPF中的解析。

一种选择是设计到最低分辨率,并确保所有内容都适当停靠,以便随着窗口分辨率变大,元素变大。这就是有多少人在WinForms中做过的事情,并且仍然可以很好地用于WPF。您可能已经有了一些如何通过设置Horizo​​ntalAlignment,VerticalAlignment和margin来处理这个问题的概念。

在WPF中做的更新,更时尚的事情在WinForms中几乎是不可能的,因为你的应用程序实际上只是放大,所以你的控件会像你的Window那样变大。为此,您将在Window中的某个根元素上应用ScaleTransform,让WPF处理其余部分。真的很酷。

要显示这是什么样的,这就是启动应用程序时窗口的样子,使其变小,并使其变大:http://i.stack.imgur.com/QeoVK.png

这是我制作的小样本应用程序的代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    #region ScaleValue Depdency Property
    public static readonly DependencyProperty ScaleValueProperty = DependencyProperty.Register("ScaleValue", typeof(double), typeof(MainWindow), new UIPropertyMetadata(1.0, new PropertyChangedCallback(OnScaleValueChanged), new CoerceValueCallback(OnCoerceScaleValue)));

    private static object OnCoerceScaleValue(DependencyObject o, object value)
    {
        MainWindow mainWindow = o as MainWindow;
        if (mainWindow != null)
            return mainWindow.OnCoerceScaleValue((double)value);
        else
            return value;
    }

    private static void OnScaleValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        MainWindow mainWindow = o as MainWindow;
        if (mainWindow != null)
            mainWindow.OnScaleValueChanged((double)e.OldValue, (double)e.NewValue);
    }

    protected virtual double OnCoerceScaleValue(double value)
    {
        if (double.IsNaN(value))
            return 1.0f;

        value = Math.Max(0.1, value);
        return value;
    }

    protected virtual void OnScaleValueChanged(double oldValue, double newValue)
    {

    }

    public double ScaleValue
    {            
        get
        {
            return (double)GetValue(ScaleValueProperty);
        }
        set
        {
            SetValue(ScaleValueProperty, value);
        }
    }
    #endregion

    private void MainGrid_SizeChanged(object sender, EventArgs e)
    {
        CalculateScale();
    }

    private void CalculateScale()
    {
        double yScale = ActualHeight / 250f;
        double xScale = ActualWidth / 200f;
        double value = Math.Min(xScale, yScale);
        ScaleValue = (double)OnCoerceScaleValue(myMainWindow, value);
    }
}

和XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    Name="myMainWindow"
    Width="200" Height="250">
<Grid Name="MainGrid" SizeChanged="MainGrid_SizeChanged">
    <Grid.LayoutTransform>
        <ScaleTransform x:Name="ApplicationScaleTransform"
                        CenterX="0"
                        CenterY="0"
                        ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}"
                        ScaleY="{Binding ElementName=myMainWindow, Path=ScaleValue}" />
    </Grid.LayoutTransform>
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center" Height="150">
        <TextBlock FontSize="20" Text="Hello World" Margin="5" VerticalAlignment="Top" HorizontalAlignment="Center"/>
        <Button Content="Button" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
    </Grid>
</Grid>

答案 1 :(得分:16)

JacobJ给出了很好的答案,我尝试了它并且效果很好。

对于任何有兴趣的人,我做了一个附加行为,做同样的事情。我还添加了从XAML指定宽度/高度分母的选项。它可以像这样使用

<Grid Name="MainGrid"
      inf:ScaleToWindowSizeBehavior.Denominators="1000, 700"
      inf:ScaleToWindowSizeBehavior.ParentWindow="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
    <!--...-->
</Grid>

<强> ScaleToWindowSizeBehavior

public static class ScaleToWindowSizeBehavior
{
    #region ParentWindow

    public static readonly DependencyProperty ParentWindowProperty =
        DependencyProperty.RegisterAttached("ParentWindow",
                                             typeof(Window),
                                             typeof(ScaleToWindowSizeBehavior),
                                             new FrameworkPropertyMetadata(null, OnParentWindowChanged));

    public static void SetParentWindow(FrameworkElement element, Window value)
    {
        element.SetValue(ParentWindowProperty, value);
    }

    public static Window GetParentWindow(FrameworkElement element)
    {
        return (Window)element.GetValue(ParentWindowProperty);
    }

    private static void OnParentWindowChanged(DependencyObject target,
                                              DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement mainElement = target as FrameworkElement;
        Window window = e.NewValue as Window;

        ScaleTransform scaleTransform = new ScaleTransform();
        scaleTransform.CenterX = 0;
        scaleTransform.CenterY= 0;
        Binding scaleValueBinding = new Binding
        {
            Source = window,
            Path = new PropertyPath(ScaleValueProperty)
        };
        BindingOperations.SetBinding(scaleTransform, ScaleTransform.ScaleXProperty, scaleValueBinding);
        BindingOperations.SetBinding(scaleTransform, ScaleTransform.ScaleYProperty, scaleValueBinding);
        mainElement.LayoutTransform = scaleTransform;
        mainElement.SizeChanged += mainElement_SizeChanged;
    }

    #endregion // ParentWindow

    #region ScaleValue

    public static readonly DependencyProperty ScaleValueProperty =
        DependencyProperty.RegisterAttached("ScaleValue",
                                            typeof(double),
                                            typeof(ScaleToWindowSizeBehavior),
                                            new UIPropertyMetadata(1.0, OnScaleValueChanged, OnCoerceScaleValue));

    public static double GetScaleValue(DependencyObject target)
    {
        return (double)target.GetValue(ScaleValueProperty);
    }
    public static void SetScaleValue(DependencyObject target, double value)
    {
        target.SetValue(ScaleValueProperty, value);
    }

    private static void OnScaleValueChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
    }

    private static object OnCoerceScaleValue(DependencyObject d, object baseValue)
    {
        if (baseValue is double)
        {
            double value = (double)baseValue;
            if (double.IsNaN(value))
            {
                return 1.0f;
            }
            value = Math.Max(0.1, value);
            return value;
        }
        return 1.0f;
    }

    private static void mainElement_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        FrameworkElement mainElement = sender as FrameworkElement;
        Window window = GetParentWindow(mainElement);
        CalculateScale(window);
    }

    private static void CalculateScale(Window window)
    {
        Size denominators = GetDenominators(window);
        double xScale = window.ActualWidth / denominators.Width;
        double yScale = window.ActualHeight / denominators.Height;
        double value = Math.Min(xScale, yScale);
        SetScaleValue(window, value);
    }

    #endregion // ScaleValue

    #region Denominators

    public static readonly DependencyProperty DenominatorsProperty =
        DependencyProperty.RegisterAttached("Denominators",
                                            typeof(Size),
                                            typeof(ScaleToWindowSizeBehavior),
                                            new UIPropertyMetadata(new Size(1000.0, 700.0)));

    public static Size GetDenominators(DependencyObject target)
    {
        return (Size)target.GetValue(DenominatorsProperty);
    }
    public static void SetDenominators(DependencyObject target, Size value)
    {
        target.SetValue(DenominatorsProperty, value);
    }

    #endregion // Denominators
}

答案 2 :(得分:0)

Fredrik Hedblad答案的小修正:

因为你已经设置了DependencyProperty&#34; Denominators&#34;在Grid元素中:

<Grid Name="MainGrid"
      inf:ScaleToWindowSizeBehavior.Denominators="1000, 700"
    <!--...-->
</Grid>

您必须使用网格调用GetDominator方法。 而不是:

private static void CalculateScale(Window window)
    {
        Size denominators = GetDenominators(window);

你必须使用这样的东西:

private static void mainElement_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            FrameworkElement mainElement = sender as FrameworkElement;
            Window window = GetParentWindow(mainElement);
            CalculateScale(window, mainElement);
        }

private static void CalculateScale(Window window, FrameworkElement mainElement)
        {
            Size denominators = GetDenominators(mainElement);

答案 3 :(得分:-1)

我与JacobJ先生一起做我自己的附属公司。 我建立了一个基于我们更改的接口

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

为此接口我有扩展名

    #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

现在我们只需要添加主窗口中缺少的参数,并在事件sizeChanged中的主网格中设置this.UpdateScale()

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();
    }
}