以编程方式创建WPF窗口

4
我刚开始学习C#(之前是Java),我喜欢手动开发我的Windows。但我找到的所有MSDN指南都是使用XAML视图。请问有什么教程可以手动实现吗?
编辑:似乎不推荐手动实现,那么如果我要在某个条件为真时添加逻辑,比如游戏循环或者绘画,应该怎么做呢?

@GrantWinney 不,我的意思是完全通过C#完成,不使用任何XAML。 - user4901669
什么框架?如果是WPF,那么XAML是您的最佳选择。 - Mark Hall
@MarkHall 我正在使用WPF,正如我在问题中所述。请看我的编辑。 - user4901669
如果你学习了WPF + C#,你会永远讨厌Java。 - KMarto
3个回答

8

我相信你会喜欢查尔斯•佩特索尔德的Applications = Code + Markup,这本书从C#代码开始讲解,后面才引入XAML。

话虽如此,你不应该这样做。XAML尽管冗长,但它隐藏了很多基础设施,而且常常是以非显而易见的方式进行。像这样的琐碎代码:

<Control Foo="Text" FooExt.Bar="{Binding Text}" Grid.Column="0">

可能会变成这样难以阅读的混乱:

var ctl = new Control();
ctl.BeginInit();
ctl.Foo = "Text";
var prop = FooExt.BarProperty;
var binding = new Binding("Text") { Source = dataContext };
BindingOperations.SetBinding(ctl, prop, binding);
Grid.SetColumn(ctl, 0);
ctl.EndEnit();

你不会喜欢编写这段代码。WPF是设计用来和XAML一起使用的。
WPF不使用"paint"事件或类似事件,在最底层,你只需要"绘制"一次,就会将其存储为矢量图像,并且框架会在必要时重新绘制它。在较高层次上,你可以添加控件、基元等并更改其属性。同样,框架会负责更新视图。在更高的层次上,你只需创建模型和规则来创建控件,然后让框架处理剩下的部分。
WPF与"传统"控件工作方式非常不同。在询问问题之前,你应该阅读一本好书或至少几篇深入的教程,因为你似乎不理解基础知识。

我修改了我的问题,因为这样做是不推荐的。 - user4901669
1
我只想补充一点,尝试通过C#构建WPF应用程序是我看到人们不喜欢WPF的最常见原因之一。你可以这样做,但你会让自己的生活更加困难。学习如何用WPF正确地完成它,你将节省大量时间。 - SomeInternetGuy

3

虽然不太有趣,但是完全可以在运行时构建Windows而不使用XAML。

以下是一个小例子:

调用方法如下:

{
    CrisisWhatCrisis crisisWhatCrisis = new CrisisWhatCrisis();
    crisisWhatCrisis.ShowDialog();
}

enter image description here

The class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Test
{
    public class CrisisWhatCrisis : Window
    {
        public Grid RootGrid { get; private set; }

        public CrisisWhatCrisis()
        {
            this.WindowStyle = WindowStyle.ThreeDBorderWindow;

            this.RootGrid = new Grid()
            { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch };

            // Create a sqare grid with 20 pixel borders 
            this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });
            this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(200) }); 
            this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
            this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });

            this.RootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20) });
            this.RootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(200) });
            this.RootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20) });

            // Create a new Textbox and place it in the middle of the root grid
            TextBox TextBox_Test = new TextBox()
            { Text = "ABC", Background = Brushes.Yellow, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Top };

            Grid.SetColumn(TextBox_Test, 1);
            Grid.SetRow(TextBox_Test, 1);
            this.RootGrid.Children.Add(TextBox_Test);

            Grid GridForButtons = new Grid()
            { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch };

            Button Button_Close = new Button() { Content = "Close" };
            Button_Close.Click += Button_Close_Click;

            // Add the button to the grid which has one cell by default
            Grid.SetColumn(Button_Close, 0);
            Grid.SetRow(Button_Close, 0);
            GridForButtons.Children.Add(Button_Close);

            // add the button grid to the RootGrid
            Grid.SetRow(GridForButtons, 2);
            Grid.SetColumn(GridForButtons, 1);
            this.RootGrid.Children.Add(GridForButtons);

            // Add the RootGrid to the content of the window
            this.Content = this.RootGrid;

            // fit the window size to the size of the RootGrid
            this.SizeToContent = SizeToContent.WidthAndHeight;
        }

        private void Button_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

0

使用C#,您可以选择WPF、WinForm。如果您看到xaml,则是WPF。如果您想在后台代码中创建窗口(而不是xaml),只需执行以下操作。

MainWindow window = new MainWindow();
window.Show();

但我建议使用XAML,因为WPF提供了许多有用的方法。我推荐阅读Adam Nathan的《WPF Unleashed》这本书。


我修改了我的问题,因为这样做是不被推荐的。 - user4901669

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接