Binding not updating textbox in Avalonia for web

37 Views Asked by At

I am trying to create an application in Avalonia that allows the user to switch between states using a selection of buttons. I want the current state to be displayed in a textbox at the bottom of the app. Currently, this textbox displays and updates the correct name of the current state in the desktop version of my application. In the web version, however, the textbox initializes to the first state, but never updates when I change the state.

I currently have a Model View ViewModel pattern, based on the Avalonia cross platform mvvm template. Within my view I create a textbox like this:

<TextBlock Text="{Binding State}" />

The viewmodel is connected to the view from the view's code-behind file as such:

    _viewModel = new ViewModels.MainViewModel();
    DataContext = _viewModel;

Within the viewmodel I then have my State:

    private int _state;
    public int State
    {
        get => _state;
        set => this.RaiseAndSetIfChanged(ref _state, value);
    }

Which is simply set using a setState function called from within each button:

    public void setState(int state)
    {
        if (state == 2 || State == 2)
        {
            State = state;
        }
        else if ((state == 3 && State == 6) || (state == 6 && State == 3))
        {
            State = state;
        }
        else
        {
            Console.WriteLine("Invalid state transition");
        }
    }

This approach seems to work fine when running on a desktop environment, as I can see the state changing and the textblock updating. When running the same application in a browser, however, the state still updates, but the textblock always says "1". Which is what it is initialized to when initializing my viewmodel.

    public MainViewModel()
    {
        State = 1;
    }

I am new to Avalonia and data binding in general, so please let me know if I am doing something incorrectly.

0

There are 0 best solutions below