以编程方式创建 WPF 窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30363088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically creating WPF windows
提问by Athari
I am just starting to learn C# (Coming from Java) and I prefer to develop my Windows manually, but all of the MSDN guides I have found involve using the XAML view. Where can I find a tutorial that explains how to do this manually?
我刚开始学习 C#(来自 Java),我更喜欢手动开发我的 Windows,但我发现的所有 MSDN 指南都涉及使用 XAML 视图。在哪里可以找到解释如何手动执行此操作的教程?
Edit: it appears that this is not recommended, so how would I add logic such as a game loop or drawing while a certain condition is true?
编辑:这似乎是不推荐的,那么当某个条件为真时,我将如何添加诸如游戏循环或绘图之类的逻辑?
回答by Athari
You would totally love Applications = Code + Markupby Charles Petzold. He starts with C# code and only later introduces XAML.
你会非常喜欢Charles Petzold 的Applications = Code + Markup。他从 C# 代码开始,后来才介绍了 XAML。
That being said, you shouldn't do that. XAML, even with all its verbosity, hides a lot of infrastracture, often in non-obvious ways. Trivial code like this:
话虽如此,你不应该那样做。XAML,即使它的所有冗长,隐藏了很多基础设施,通常以不明显的方式。像这样的简单代码:
<Control Foo="Text" FooExt.Bar="{Binding Text}" Grid.Column="0">
can become unreadable mess like this:
可能会变得像这样无法阅读:
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();
You won't enjoy writing this code. WPF is designed to be used together with XAML.
您不会喜欢编写此代码。WPF 旨在与 XAML 一起使用。
WPF doesn't use "paint" events or similar, on the lowest level you "draw" something once, it's stored as a vector image and the framework takes care of redrawing it when necessary. On higher level, you add controls, primitives etc. and change their properties. Again, the framework takes care of updating the view. On even higher level, you just create models and rules for creating controls and let the framework do the rest.
WPF 不使用“绘制”事件或类似事件,在您“绘制”一次的最低级别上,它存储为矢量图像,并且框架会在必要时负责重绘它。在更高级别上,您可以添加控件、基元等并更改它们的属性。同样,框架负责更新视图。在更高的层次上,您只需创建用于创建控件的模型和规则,其余的交给框架来完成。
WPF is quite different from "traditional" way of working with controls. You should read a good book or at least several thorough tutorials before asking questions, because you don't seem to understand the basics.
WPF 与使用控件的“传统”方式截然不同。在提出问题之前,您应该阅读一本好书或至少几本详尽的教程,因为您似乎不了解基础知识。
回答by marsh-wiggle
It's not fun, but it's possible to build a Windows completely at runtime without XAML.
这并不有趣,但可以在运行时完全构建 Windows,而无需 XAML。
Here's a little example:
这是一个小例子:
To be called with:
被称为:
{
CrisisWhatCrisis crisisWhatCrisis = new CrisisWhatCrisis();
crisisWhatCrisis.ShowDialog();
}
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();
}
}
}
回答by serendip
Using C#, You can select WPF, WinForm. If you saw xaml, It's WPF. If you want to create window in code behind(not xaml), just do below.
使用C#,可以选择WPF、WinForm。如果你看到 xaml,那就是 WPF。如果您想在后面的代码(不是 xaml)中创建窗口,请执行以下操作。
MainWindow window = new MainWindow();
window.Show();
but I recommand to use xaml, because WPF give many useful methods. I recommend book that wpf unleashed by Adam Nathan
但我建议使用 xaml,因为 WPF 提供了许多有用的方法。我推荐亚当·内森 (Adam Nathan) 出版的 wpf 的书


