简化?不可以。但是您可以在 XAML 中执行的所有操作都可以在代码中执行。例如,这是一个简单的 Windows Ink 绘图应用程序 - 非常简单:

没有保存,没有改变任何东西——但你可以通过代码。
画板.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Ink;
public class Sketchpad : Application {
[STAThread]
public static void Main(){
var app = new Sketchpad();
Window root = new Window();
InkCanvas inkCanvas1 = new InkCanvas();
root.Title = "Skortchpard";
root.ResizeMode = ResizeMode.CanResizeWithGrip;
inkCanvas1.Background = Brushes.DarkSlateBlue;
inkCanvas1.DefaultDrawingAttributes.Color = Colors.SpringGreen;
inkCanvas1.DefaultDrawingAttributes.Height = 10;
inkCanvas1.DefaultDrawingAttributes.Width = 10;
root.Content = inkCanvas1;
root.Show();
app.MainWindow = root;
app.Run();
}
}
画板.csproj
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0">
<PropertyGroup>
<AssemblyName>Simply Sketch</AssemblyName>
<OutputPath>Bin\</OutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Compile Include="SketchPad.cs" />
</ItemGroup>
<Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe">
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>
<Target Name="Clean">
<Delete Files="$(OutputPath)$(AssemblyName).exe" />
</Target>
<Target Name="Rebuild" DependsOnTargets="Clean;Build" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
这就是它所需要的。当然,如果你想避免 XAML,这基本上意味着你将不得不编写一堆备用 .NET 代码,所以这取决于你 - 你想在 XAML 中转储这些东西,还是你愿意而是用代码写?
我认为在 VS 中执行此操作的最大好处是您可以获得良好的设计师支持,因此通常很容易直观地看到所有内容的位置和去向。它也可能更少查找文档,有时不得不做出一些飞跃。
但是没有 XAML 的 WPF是可能的。
您甚至不需要为此安装 Visual Studio,只需 .NET CLI 和开发人员命令提示符即可。将这两个文件放在一个文件夹中并运行msbuild
,然后您可以运行该目录中的Bin
文件。