帮助:为什么我的第一个WPF应用程序不工作?

帮助:为什么我的第一个WPF应用程序不工作?,wpf,Wpf,我的第一个WPF应用程序,但它不工作。救命啊 xaml: <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="308" Width="527">

我的第一个WPF应用程序,但它不工作。救命啊

xaml:
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="308" Width="527">
    <Grid Name="canvas">
        <Canvas></Canvas>
    </Grid>
</Window>
xaml:
代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Line line = new Line();
            line.X1 = 0;
            line.Y1 = 100;
            line.X2 = 0;
            line.Y2 = 100;
            line.Stroke = Brushes.Red;
            line.StrokeThickness = 1; // Note1
            canvas.Children.Insert(0, line);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
行=新行();
line.X1=0;
线Y1=100;
line.X2=0;
第2.Y2行=100;
line.Stroke=画笔.Red;
line.StrokeThickness=1;//注1
canvas.Children.Insert(0,行);
}
}
}

我看到的是你的第一个X,Y坐标和第二个是相同的。所以画的线在同一点上

line.X1 = 0;
line.Y1 = 100;
line.X2 = 0;
line.Y2 = 100;

// Change too this and that will will draw straight over 100 pixels.
line.X1 = 0;
line.Y1 = 100;
line.X2 = 100;
line.Y2 = 100;

我看到的是你的第一个X,Y坐标和第二个是一样的。所以画的线在同一点上

line.X1 = 0;
line.Y1 = 100;
line.X2 = 0;
line.Y2 = 100;

// Change too this and that will will draw straight over 100 pixels.
line.X1 = 0;
line.Y1 = 100;
line.X2 = 100;
line.Y2 = 100;
它确实有效

但您要创建的是一个点而不是一条线,并将其添加到栅格中,而不是画布中。事实上,我不认为你会看到一个起点和终点相同的点

将X2更改为300,您将看到一条红线

塞吉奥确实管用

但您要创建的是一个点而不是一条线,并将其添加到栅格中,而不是画布中。事实上,我不认为你会看到一个起点和终点相同的点

将X2更改为300,您将看到一条红线


SergioL

您的X1/Y1值与X2/Y2值相同。如果更改line.X2=0;至第2.X2行=50;,你会看到你的台词的

但是,如果您的行不是动态的,通常最好直接在XAML中执行大多数可视化操作,如下所示:

    <Grid Name="canvas">
    <Line X1="0" Y1="100" X2="50" Y2="100" StrokeThickness="1" Stroke="Red" />
</Grid>

希望这有帮助,
Andy

您的X1/Y1值与X2/Y2值相同。如果更改line.X2=0;至第2.X2行=50;,你会看到你的台词的

但是,如果您的行不是动态的,通常最好直接在XAML中执行大多数可视化操作,如下所示:

    <Grid Name="canvas">
    <Line X1="0" Y1="100" X2="50" Y2="100" StrokeThickness="1" Stroke="Red" />
</Grid>

希望这有帮助,
安迪

请说得具体一点,你有什么问题?请说得具体一点,你有什么问题?