C# UWP 无法用代码显示用户控件

C# UWP can't display usercontrol with code

我用 C# UWP 创建了一个用户控件,所以如果我在我的 MainPage.Xaml 中用 Xaml 创建我的用户控件,我可以看到它。 但是,如果我使用 C# 代码在我的 MainPage.xaml.cs 中创建它,我将看不到它。我想我在创建控件时忘记了 MainPage.xaml.cs 中的某些内容。

如果有人能解决我的问题我会很高兴:D

MyControl.xaml:

<UserControl
    x:Class="CustomControlTest.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CustomControlTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="32.06" Width="53.254">

    <Grid>
        <Button Content="Hello" Background="Green" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/>
    </Grid>
</UserControl>

MyControl.xaml.cs:

namespace CustomControlTest
{
    public sealed partial class MyControl : UserControl
    {
        public MyControl()
        {
            this.InitializeComponent();
        }
    }
}

MainPage.xaml:

<Page
    x:Class="CustomControlTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CustomControlTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <!--<local:MyControl/>-->
    </Grid>
</Page>

MainPage.xaml.cs:

namespace CustomControlTest
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            MyControl mc = new MyControl();
            mc.HorizontalAlignment = HorizontalAlignment.Stretch;
            mc.VerticalAlignment = VerticalAlignment.Stretch;
        }
    }
}

I made an Usercontrol with C# UWP, so if i create my Usercontrol with Xaml in my MainPage.Xaml I can see it. But if I create it in my MainPage.xaml.cs with C# code i can't see it

问题出在您的 MainPage.cs 构造函数中:

public MainPage()
{
   this.InitializeComponent();

   MyControl mc = new MyControl();
   mc.HorizontalAlignment = HorizontalAlignment.Stretch;
   mc.VerticalAlignment = VerticalAlignment.Stretch;
}

您创建了 MyControl 的新实例,但从未将此实例添加到您的 MainPage 实例中。构建实例并设置好需要的内容后,需要将此控件添加到另一个容器中。

首先,在 MainPage.xaml 中为您的 Grid 元素命名:

 <Page
    x:Class="CustomControlTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CustomControlTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid Name="myGrid">
        <!--<local:MyControl/>-->
    </Grid>
 </Page>

接下来您可以在您的代码后面引用此 Grid 并将新的 MyControl 实例添加到此 Grid:

public MainPage()
{
   this.InitializeComponent();

   MyControl mc = new MyControl();
   mc.HorizontalAlignment = HorizontalAlignment.Stretch;
   mc.VerticalAlignment = VerticalAlignment.Stretch;

   // Add new instance of MyControl (mc) to grid control
   myGrid.Children.Add(mc);
}