如何在WPF中实现以下功能

时间:2016-12-25 15:57:17

标签: c# .net wpf xaml

首先,抱歉这个蹩脚的问题。但是,我自己学习WPF。 我现在正在学习import java.util.Scanner; public class cs{ public static void main(String[] args)throws Exception{ System.out.println("Starting number: "); Scanner s = new Scanner(System.in); int a = s.nextInt(); System.out.print(" " + a); while (a > 1 && (a % 2) != 0){ a /= 2; System.out.print(" " + a); } } 课程。要了解这一点,我将浏览此MSDN网页。 MSDN描述使用代码创建Panel的方式吸引了我很多。例如

Panel

我想以页面上显示的方式创建// Create the application's main window mainWindow = new Window (); mainWindow.Title = "Canvas Sample"; // Create the Canvas myParentCanvas = new Canvas(); myParentCanvas.Width = 400; myParentCanvas.Height = 400; // Define child Canvas elements myCanvas1 = new Canvas(); myCanvas1.Background = Brushes.Red; Canvas.SetLeft(myCanvas1, 0); . . . myParentCanvas.Children.Add(myCanvas3); // Add the parent Canvas as the Content of the Window Object mainWindow.Content = myParentCanvas; mainWindow.Show (); (布局),但我很困惑应该在哪里编写(复制 - 粘贴)此代码。我很熟悉使用Panel

创建Layout

XAML我们有以下代码行

filename.xaml.cs

我试着把这个(MSDN的)代码行放在public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } 方法中,但它对我帮助不大。根据我的小知识,我们应该有一个类似MainWindow的方法,这应该是mainWindowContent()或类似的调用(我可能错了)。

请以正确的方式指导我实现这一目标。

1 个答案:

答案 0 :(得分:1)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Title = "Canvas Sample";

        // Create the Canvas
        var myParentCanvas = new Canvas();
        myParentCanvas.Width = 400;
        myParentCanvas.Height = 400;

        // Define child Canvas elements
        var myCanvas1 = new Canvas();
        myCanvas1.Background = Brushes.Red;
        myCanvas1.Height = 100;
        myCanvas1.Width = 100;
        Canvas.SetTop(myCanvas1, 0);
        Canvas.SetLeft(myCanvas1, 0);

        var myCanvas2 = new Canvas();
        myCanvas2.Background = Brushes.Green;
        myCanvas2.Height = 100;
        myCanvas2.Width = 100;
        Canvas.SetTop(myCanvas2, 100);
        Canvas.SetLeft(myCanvas2, 100);

        var myCanvas3 = new Canvas();
        myCanvas3.Background = Brushes.Blue;
        myCanvas3.Height = 100;
        myCanvas3.Width = 100;
        Canvas.SetTop(myCanvas3, 50);
        Canvas.SetLeft(myCanvas3, 50);

        // Add child elements to the Canvas' Children collection
        myParentCanvas.Children.Add(myCanvas1);
        myParentCanvas.Children.Add(myCanvas2);
        myParentCanvas.Children.Add(myCanvas3);

        // Add the parent Canvas as the Content of the Window Object
        this.Content = myParentCanvas;
    }
}

相当于

<Window Title="Canvas Sample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Canvas Height="400" Width="400">
    <Canvas Height="100" Width="100" Top="0" Left="0" Background="Red"/>
    <Canvas Height="100" Width="100" Top="100" Left="100" Background="Green"/>
    <Canvas Height="100" Width="100" Top="50" Left="50" Background="Blue"/>
  </Canvas>
</Window>

如果在MainWindow代码隐藏本身内完成。

然后在启动时你可以调用类

// Create the application's main window
var mainWindow = new MainWindow ();
mainWindow.Show ();