wpf解决方案独立性

时间:2013-11-05 18:56:11

标签: c# wpf resolution

我一直在使用this方法来扩展尺寸应用程序,以便它适合多种分辨率。我已经使用我的计算机测试了它,通过将我的显示器的分辨率更改为多个不同的分辨率,它似乎每次都有效。当我们部署应用程序进行测试时 - 它不起作用。我可以添加代码,但它与链接中的代码几乎相同。

主窗口在大多数情况下都没有适当缩放,它是被抛出的高度。

enter image description here

<Grid Name="MainGrid" SizeChanged="MainGrid_SizeChanged" Width="1889" Height="1013">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1716*" />
            <ColumnDefinition Width="173*" />
        </Grid.ColumnDefinitions>
        <Grid.LayoutTransform>
            <ScaleTransform x:Name="ApplicationScaleTransform"
                        CenterX="0"
                        CenterY="0"
                        ScaleX="{Binding ElementName=HomeWindow, Path=ScaleValueX}"
                        ScaleY="{Binding ElementName=HomeWindow, Path=ScaleValueY}" />
        </Grid.LayoutTransform>
... <!--The rest of my window here -->
</Grid>

背后的代码

#region ScaleValue Dependency Property
private System.Drawing.Size PerfectResolution = new System.Drawing.Size( 1920, 1080 );
private System.Drawing.Size Resolution = System.Windows.Forms.SystemInformation.PrimaryMonitorSize;

public static readonly DependencyProperty ScaleValuePropertyX = DependencyProperty.Register( "ScaleValueX", typeof( double ), typeof( MainWindow ), new UIPropertyMetadata( 1.0, new PropertyChangedCallback( OnScaleValueChanged ), new CoerceValueCallback( OnCoerceScaleValue ) ) );
public static readonly DependencyProperty ScaleValuePropertyY = DependencyProperty.Register( "ScaleValueY", typeof( double ), typeof( MainWindow ), new UIPropertyMetadata( 1.0, new PropertyChangedCallback( OnScaleValueChanged ), new CoerceValueCallback( OnCoerceScaleValue ) ) );

private double CalculateYProportions()
{
    return Convert.ToDouble( Resolution.Height ) / Convert.ToDouble( PerfectResolution.Height );
}

private double CalculateXProportions()
{
    return Convert.ToDouble( Resolution.Width ) / Convert.ToDouble( PerfectResolution.Width );
}

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

bool ScaledWindow = false;
protected virtual void OnScaleValueChanged( double oldValue, double newValue )
{
    if (!ScaledWindow)
    {
        this.Height = this.Height * CalculateYProportions();
        this.Width = this.Width * CalculateXProportions();

        this.MinHeight = this.HomeWindow.Height;
       // this.MaxHeight = this.HomeWindow.Height;
       // this.MaxWidth = this.HomeWindow.Width;
        this.MinWidth = this.HomeWindow.Width;


        ScaledWindow = true;
        this.Top = 0;
        this.Left = 0;
        this.Show();
    }
}

public double ScaleValueX
{
    get
    {
        return (double)GetValue( ScaleValuePropertyX );
    }
    set
    {
        SetValue( ScaleValuePropertyX, value );
    }
}

public double ScaleValueY
{
    get
    {
        return (double)GetValue( ScaleValuePropertyY );
    }
    set
    {
        SetValue( ScaleValuePropertyY, value );
    }
}


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

private void CalculateScale()
{
    double yScale = CalculateYProportions();
    double xScale = CalculateXProportions();

    ScaleValueX = (double)OnCoerceScaleValue( HomeWindow, xScale );
    ScaleValueY = (double)OnCoerceScaleValue( HomeWindow, yScale );
}
#endregion

正如你所看到的那样,右侧会被切断而你无法分辨,但是臀部会被切断。

0 个答案:

没有答案