问题描述:

I've created an sidebar with images using StackPanel WPF control. I've set simulator to 23'' and 1920x1080, the device is 42'' and resolution is 1920x1080 too. In simulator the StackPanel is visible, on the device is not.

XAML code:

<StackPanel Height="1080" Margin="0,0,300,0" VerticalAligment="Top" Margin="1620,0,0,0"/>

...rest of StackPanel XAML code...

Why is so? Why it dissapears?

网友答案:

probably you may need to use some other techniques to align to right as specifying margin, width etc properties may get calculated differently on different devices

read more at DPI and Device-Independent Pixels

you may try

<StackPanel Height="1080" VerticalAligment="Top" Width="300" HorizontalAlignment="Right"/>

perhaps a better approach would be using a Grid to align properly

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="300" />
    </Grid.ColumnDefinitions>
    <!--your content-->
    <StackPanel Grid.Column="1" />
</Grid>

you may also choose to define the size units i.e. inch to keep the size uniform on various devices

eg

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="2in" />
    </Grid.ColumnDefinitions>
    <!--your content-->
    <StackPanel Grid.Column="1" />
</Grid>

so this will ensure that the stack panel is always 2 inch on every device

相关阅读:
Top