我开始在 WindowsPhone 中开发应用程序,但遇到了一些问题。我试图动态添加画布,但它不起作用,我可能忘记了一些事情,你能帮忙发现我的错误吗?下面的代码来自我的学习应用程序,它是基本的 Win Phone 应用程序。
cs代码:
public MainPage()
{
InitializeComponent();
int size = 50;
Canvas myCanvas = new Canvas();
Canvas.SetLeft(myCanvas, 0);
myCanvas.Width = size;
Color c = new Color();
c.R = 255;
c.B = 0;
c.G = 255;
myCanvas.Background = new SolidColorBrush(c);
ContentPanel.Height = 100;
ContentPanel.Width = 100;
ContentPanel.Children.Add(myCanvas);
ApplicationTitle.Text = ContentPanel.ActualHeight.ToString();
}
xml
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
</Grid>
</Grid>