我刚开始学习C#(之前是Java),我喜欢手动开发我的Windows。但我找到的所有MSDN指南都是使用XAML视图。请问有什么教程可以手动实现吗?
编辑:似乎不推荐手动实现,那么如果我要在某个条件为真时添加逻辑,比如游戏循环或者绘画,应该怎么做呢?
编辑:似乎不推荐手动实现,那么如果我要在某个条件为真时添加逻辑,比如游戏循环或者绘画,应该怎么做呢?
我相信你会喜欢查尔斯•佩特索尔德的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();
虽然不太有趣,但是完全可以在运行时构建Windows而不使用XAML。
以下是一个小例子:
调用方法如下:
{
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();
}
}
}
使用C#,您可以选择WPF、WinForm。如果您看到xaml,则是WPF。如果您想在后台代码中创建窗口(而不是xaml),只需执行以下操作。
MainWindow window = new MainWindow();
window.Show();
但我建议使用XAML,因为WPF提供了许多有用的方法。我推荐阅读Adam Nathan的《WPF Unleashed》这本书。