首页 > 解决方案 > 我们如何在父窗口的垂直上边缘显示 UWP 用户控件?

问题描述

XAMLMicrosoft 教程中显示父窗口的顶部边缘与UWP user control. 问题:我们如何使用户控件与父窗口的上边缘对齐?备注:下面VerticalAlignment="Top"StackPanel没有帮助。这个问题与这篇文章类似,但在不同的背景下。

<UserControl
    x:Class="ClassLibUWP_inside_WPF.MyUserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ClassLibUWP_inside_WPF"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:winui="using:Microsoft.UI.Xaml.Controls"
    mc:Ignorable="d"
    d:DesignWidth="400" Height="329">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="211*"/>
            <ColumnDefinition Width="189*"/>
        </Grid.ColumnDefinitions>
        <StackPanel Background="LightCoral" Grid.ColumnSpan="2">
            <TextBlock>This is a simple custom UWP control</TextBlock>
            <Rectangle Fill="Blue" Height="93" Width="100"/>
            <TextBlock Text="{x:Bind XamlIslandMessage}" FontSize="50"></TextBlock>
            <winui:RatingControl Height="32" />
        </StackPanel>
    </Grid>
</UserControl>

当您运行上述教程中构建的应用程序时,您会看到显示上述 UWP 用户控件的以下屏幕

在此处输入图像描述

相反,我想按如下方式显示它[注意窗口标题和红色堆栈面板之间没有间隙]:

在此处输入图像描述

标签: wpfxamluwpxaml-islands

解决方案


UserControl具有小于窗口高度的固定高度,因此在窗口中垂直居中。设置应该VerticalAlignment="Top"WindowsXamlHost你想要的。


推荐阅读