Giter Club home page Giter Club logo

sevenguis's Introduction

SevenGUIs

This is a practice implementation of 7GUIs. It uses WPF and ReactiveUI as a base.

Counter

Screenshot of #1 - Counter

This is a trivial implementation. ViewModel contains a command that adds 1 to a bindable property every time it's called. The command is bound to the <Button> in the UI.

Temperature Converter

Screenshot of #2 - Temperature Converter

There are several approaches here.

  1. No viewmodel needed. The Fahrenheit TextBox can be relative-bound to to the Celsius TextBox.Text through a converter with Convert() (that does C->F conversion) and ConvertBack() (F->C).

  2. (current implementation) ViewModel stores a Celsius and Fahrenheit bindable properties. Celsius setter updates the Fahrenheit private field and raises PropertyChanged(Fahrenheit).

Setting the private field is preferred to using the public setter directly: Fahrenheit = value * 9.... Calling the Fahrenheit setter would trigger an update to Celsius, triggering a set to Fahrenheit, and so on, leading to an infinite loop.

Having decimal? Celsius/Fahrenheit properties serves several purposes. If the user hasn't entered anything, both TextBoxes would be empty instead of showing 0.0. If the user enters non-numerics, WPF's binding conversion throws an exception due to different types (string in UI, decimal? in VM).

  1. ReactiveUI commands and calculated properties. The viewmodel would look like this:
CalculateCelsiusCommand = ReactiveCommand.Create<decimal, decimal?>(CalculateCelsius);
_celsius = CalculateCelsiusCommand.ToProperty(this, x => x.Celsius);

private static decimal? CalculateCelsius(decimal value)
{
    return (value - 32) * (5m / 9m);
}

The view would invoke the respective commands on KeyUp and would be bound one-way from the Celsius/Fahrenheit source properties.

Celsius.Events().KeyUp
        .Select(ev => ((TextBox)ev.Source).Text)
        .Where(t => decimal.TryParse(t, out _))
        .Select(decimal.Parse)
        .InvokeCommand(ViewModel, vm => vm.CalculateFahrenheitCommand);
<TextBox Grid.Column="0" Text="{Binding Celsius, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" x:Name="Celsius" />

sevenguis's People

Contributors

stannedelchev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.