简体   繁体   中英

C# UWP can't display usercontrol with code

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. I think i forgot something in MainPage.xaml.cs when I create my control.

I would be happy if someone have a solution for my problem: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

The issue is in your MainPage.cs constructor:

public MainPage()
{
   this.InitializeComponent();

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

You create a new instance of MyControl , but you never add this instance to your MainPage instance. After constructing the instance and setting what you need to, you need to add this control to another container.

First, give your Grid element a name in 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 Name="myGrid">
        <!--<local:MyControl/>-->
    </Grid>
 </Page>

Next you can reference this Grid in your code behind and add your new MyControl instance to this 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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