Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Since I'm blind and Visual Studio isn't accessible, can I create C# WPF applications just with plain code?

I am totally blind, I use NVDA screen reader to operate my system, and I am trying to create a Windows desktop application. Visual Studio isn't screen-reader friendly, at least not the forms designer part. VS Code is pretty much accessible, although powershell's protools' designer isn't.

Whenever I see a question about writing GUI in WPF with pure code I find a lot of "why in the name of God would you want to do that? Use VS!" answers. Well, here is the reason, VS form designer is out of reach to blind programmers, so the least practical approach will have to do.

So, can it be done? Would you suggest any resources or approaches to this?

like image 896
Victor Caparica Avatar asked Sep 05 '21 23:09

Victor Caparica


1 Answers

I'm going to try and build up from the other example to get you compile-able code. To get WPF working, you'll need a project that builds with WPF support.

The simplest way is to create a .csproj ourself and use the previously posted code-only solution.

So, start with a minimal App.csproj file as so:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>

Then the app & window code in App.cs as before. I've added a couple controls to it, as you'll need a container anyway (I used StackPanel) if you want to start adding more stuff later.

using System;
using System.Windows;
using System.Windows.Controls;

public class App : Application {
    protected override void OnStartup(StartupEventArgs e)     {
        base.OnStartup(e);
        new MainWindow().Show();
    }

    [STAThread]
    public static void Main() => new App().Run();
}

public class MainWindow : Window {
    protected override void OnInitialized(EventArgs e) {
        base.OnInitialized(e);

        var panel = new StackPanel();
        this.Content = panel;

        var label = new Label { Content = "Click me:" };
        panel.Children.Add(label);
        
        var closeButton = new Button { Content = "Close", Height = 100 };
        closeButton.Click += (sender, args) => this.Close();
        panel.Children.Add(closeButton);

        (this.Width, this.Height) = (200, 200);
    }
}

You then build the whole project by using dotnet build. And you can run it with dotnet run or invoking the exe directly.

Alternatively, if you'd like to explore a WPF project with separate .xaml and .xaml.cs code, you can create your project using dotnet new wpf -o MyProjectName. This will produce a project, a xaml-based App, and a xaml-based form.

like image 131
NPras Avatar answered Sep 24 '22 02:09

NPras