窗口大小调整大小的OOB Silverlight和内容比例

时间:2015-06-16 11:47:38

标签: silverlight resize

早上好,

当OOB Silverlight应用程序的窗口调整大小时,对内容执行scaleTransform时遇到问题。代码看起来很简单

XAML

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="Aquamarine" RenderTransformOrigin="0,0">
        <Rectangle Fill="Aqua" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />

        <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>
</UserControl>

C#

public MainPage()
{
    InitializeComponent();
    SizeChanged += MainPage_SizeChanged;
}

void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
{
    ScaleTransform scaleTransform = new ScaleTransform();
    scaleTransform.ScaleX = (Application.Current.MainWindow.Width / 500f);
    scaleTransform.ScaleY = (Application.Current.MainWindow.Height / 500f);
    scaleTransform.CenterX = 0;
    scaleTransform.CenterY = 0;

    // this.RenderTransform doesn't work either.
    LayoutRoot.RenderTransform = scaleTransform;
}

在调整主窗口大小时,内容会缩放,但由于某种原因,我会在缩小时看到窗口的白色背景。看起来内容缩小的速度比窗口快,我不知道我做错了什么。代码看似合乎逻辑,应该有效,不应该吗?

1 个答案:

答案 0 :(得分:0)

LayoutTransformer节省了我的一天。我已经更改了代码和xaml,如下所示,现在一切正常。代码可能需要稍微调整一下。

XAML

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:layout="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="500" d:DesignWidth="500">

    <layout:LayoutTransformer x:Name="LayoutTransformer">
        <layout:LayoutTransformer.Content>
            <Grid x:Name="LayoutRoot" Background="AntiqueWhite">
                <Rectangle Fill="Aqua" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                <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>
        </layout:LayoutTransformer.Content>
        <layout:LayoutTransformer.LayoutTransform>
            <ScaleTransform x:Name="ContentScale" CenterX="0" CenterY="0" ScaleX="1" ScaleY="1" />
        </layout:LayoutTransformer.LayoutTransform>
    </layout:LayoutTransformer>
</UserControl>

C#

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        SizeChanged += MainPage_SizeChanged;
    }

    void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        double yScale = ActualHeight / 500f;
        double xScale = ActualWidth / 500f;
        double value = GetScale(xScale, yScale);

        ContentScale.ScaleX = value;
        ContentScale.ScaleY = value;

        LayoutTransformer.ApplyLayoutTransform();
    }

    private double GetScale(double xScale, double yScale)
    {
        return Math.Max(0.1, Math.Min(xScale, yScale));
    }
}