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

时间:2016-06-01 13:36:52

标签: c# uwp-xaml

我从Visual后面创建的图像在我的Visual树中可见,但在运行我的应用程序时不会在屏幕上呈现。

这是我的代码背后

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);
}

这是我的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>

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

你的Xaml还没有完成。您缺少Page的结束标记。