简体   繁体   English

从UWP中的C#代码渲染Xaml控件

[英]Render Xaml Controls from C# code behind in UWP

The Image that I have created from Code behind is visible in my Visual tree but is not rendered on screen when I run my app. 我在后台创建的Image在我的Visual树中可见,但在运行我的应用程序时不会在屏幕上呈现。

This is my Code Behind 这是我的代码背后

public MainPage()
{
    this.InitializeComponent();
    //Image that was created in Xaml
    image.Source = new BitmapImage(new Uri("ms-appx:///testimage.jpg"));

    //Image creation from C# code
    Image image1 = new Image();
    image1.Width = 200;
    image1.Height = 200;
    image1.Margin = new Thickness(50, 50, 0, 0);
    image1.Source = new BitmapImage(new Uri("ms-appx:///download.jpg"));
    //grid is the Grid name in Xaml
    grid.Children.Add(image1);
}

This is my XAML 这是我的XAML

<Page Name="HI"
x:Class="test1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:test1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid x:Name="grid">

    <Image x:Name="image" Visibility="Visible" HorizontalAlignment="Left" Height="200" Margin="10,200,0,0"  VerticalAlignment="Top" Width="200"/>
</Grid>

What am I missing here? 我在这里错过了什么?

Your Xaml isn't complete. 你的Xaml不完整。 You are missing a closing tag for Page. 您缺少Page的结束标记。

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

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