2012-04-21 163 views
0

我开始在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(); 

    } 

XAML

<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> 

回答

1

我不太清楚你想用帆布在这里完成的任务。您可能想要查看其他控件以获取您想要执行的操作。

这就是说,问题在于您错过了颜色中的“Alpha”组件。这使它透明。

变化:

Color c = new Color(); 
    c.R = 255; 
    c.B = 0; 
    c.G = 255; 

    c.A = 255; // this is what you need to add to make it visible. 
+0

谢谢你,你让我很快乐;) 目前,我试图创建一个游戏,我需要,我会很快把一些PNG可拖动的元素。 – 2012-04-21 13:45:02

+0

很高兴帮助。祝你好运! – Robaticus 2012-04-21 14:01:53