繁体   English   中英

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

[英]C# UWP can't display usercontrol with code

我用 C# UWP 做了一个用户控件,所以如果我在我的 MainPage.ZA02CFBCZF797A3D713869Z 中用 Xaml 创建我的用户控件,可以看到它A71389A9。 但是,如果我使用 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();
        }
    }
}

主页.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>

主页.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;
        }
    }
}

我用 C# UWP 做了一个用户控件,所以如果我在我的 MainPage.ZA02CFBCZF797A3D713869Z 中用 Xaml 创建我的用户控件,可以看到它A71389A9。 但是如果我在我的 MainPage.xaml.cs 中使用 C# 代码创建它,我看不到它

问题出在您的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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM