以编程方式绘制时,形状不会显示在画布上

时间:2014-01-12 13:34:29

标签: c# xaml windows-phone

我正在尝试为我的Windows Phone设备创建一个简单的应用程序,我可以在编程上在画布上绘制一些形状。当我编写代码来定义xaml中的形状时,一切看起来都很棒但是当我尝试在代码中绘制相同的形状时,画布上没有任何内容。

Xaml代码

<phone:PhoneApplicationPage
x:Class="TestApp.ResultPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<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 Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="TestApp" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Margin="14,10,10,86" Grid.Row="1">
        <Canvas x:Name="resultCanvas" Margin="10,10,10,0" Background="White" MaxHeight="590" VerticalAlignment="Center" Height="590" Width="436" MaxWidth="436" HorizontalAlignment="Center">
            <Canvas.Clip>
                <RectangleGeometry Rect="0, 0, 436, 590"/>
            </Canvas.Clip>
        </Canvas>
    </Grid>
    <Button x:Name="drawButton" Content="Return" HorizontalAlignment="Left" Margin="14,615,0,0" Grid.Row="1" VerticalAlignment="Top" Height="81" Width="456" Click="drawButton_Click"/>
</Grid>

绘制线的C#代码

public void Draw()
{
   Line line = new Line()
   {
     StrokeThickness = 2,
     Stroke = new SolidColorBrush(Colors.Blue),
     X1 = 100,
     X2 = 200,
     Y1 = 100,
     Y2 = 200,
    };
    resultCanvas.Children.Add(line);
}

private void drawButton_Click(object sender, RoutedEventArgs e)
    {
        Draw();
    }

0 个答案:

没有答案