UPDATE 1:
The HoldingsValue calculation works if I use a local HoldingsAmount value. How do I get it to take the value from the xaml HoldingsAmount Entry?
View: TickerView.xaml
Added binding to the text property of the entry control:
<Entry Placeholder="Enter holdings"
Text="{Binding Ticker.HoldingsAmount}"
Keyboard="Numeric" />
Models: Tickers.cs
Added HoldingsAmount to the model:
namespace MVVMDemo2.MVVM.Models
{
public class Ticker
{
public decimal HoldingsAmount { get; set; }
public string AskPrice { get; set; }
public string BidPrice { get; set; }
public string HoldingsValue { get; set; }
}
}
ViewModels: TickerViewModel.cs
using Binance.Net.Clients;
using Binance.Net.Objects;
using MVVMDemo2.MVVM.Models;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMDemo2.MVVM.ViewModels
{
[AddINotifyPropertyChangedInterface]
public class TickerViewModel
{
public Ticker Ticker { get; set; }
//public decimal HoldingsAmount { get; set; }
public TickerViewModel()
{
var socketClient = new BinanceSocketClient(new BinanceSocketClientOptions { });
socketClient.SpotStreams.SubscribeToBookTickerUpdatesAsync("BTCUSDT", data => {
decimal HoldingsAmount = 2;
Ticker = new Ticker
{
AskPrice = $"${data.Data.BestAskPrice.ToString("N2")}",
BidPrice = $"${data.Data.BestBidPrice.ToString("N2")}",
HoldingsValue = (HoldingsAmount * data.Data.BestBidPrice).ToString()
};
});
socketClient.UnsubscribeAllAsync();
}
}
}
ORIGINAL:
I'm trying to learn MAUI and MVVM, but I can't seem to read a value from the XAML form to use with data from a WebSocket.
I currently have the following which works:
Views: TickerView.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MVVMDemo2.MVVM.Views.TickerView"
Title="TickerView">
<VerticalStackLayout
VerticalOptions="Center"
HorizontalOptions="Center">
<Label
Text="Holdings"
FontSize="25" />
<Entry Placeholder="Enter holdings"
BackgroundColor="Gray"
Keyboard="Numeric" />
<Label
Text="Ask price"
FontSize="25" />
<Label
Text="{Binding Ticker.AskPrice}"
FontSize="50" />
<Label
Text="Bid price"
FontSize="25" />
<Label
Text="{Binding Ticker.BidPrice}"
FontSize="50" />
<Label
Text="Holdings value"
FontSize="25" />
<Label
Text="{Binding Ticker.HoldingsValue}"
FontSize="50" />
</VerticalStackLayout>
</ContentPage>
Views: TickerView.xaml.cs
using MVVMDemo2.MVVM.Models;
using MVVMDemo2.MVVM.ViewModels;
namespace MVVMDemo2.MVVM.Views;
public partial class TickerView : ContentPage
{
public TickerView()
{
InitializeComponent();
BindingContext = new TickerViewModel();
}
}
Models: Ticker.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMDemo2.MVVM.Models
{
public class Ticker
{
public string AskPrice { get; set; }
public string BidPrice { get; set; }
}
}
ViewModels: TickerViewModel.cs
using Binance.Net.Clients;
using Binance.Net.Objects;
using MVVMDemo2.MVVM.Models;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMDemo2.MVVM.ViewModels
{
[AddINotifyPropertyChangedInterface]
public class TickerViewModel
{
public Ticker Ticker { get; set; }
public TickerViewModel()
{
var socketClient = new BinanceSocketClient(new BinanceSocketClientOptions { });
socketClient.SpotStreams.SubscribeToBookTickerUpdatesAsync("BTCUSDT", data => {
Ticker = new Ticker
{
AskPrice = $"${data.Data.BestAskPrice.ToString("N2")}",
BidPrice = $"${data.Data.BestBidPrice.ToString("N2")}",
};
});
socketClient.UnsubscribeAllAsync();
}
}
}
I want to be able to read the value from the first Entry, so I can perform the following calculation:
BestAskPrice x HoldingsAmount
Once that calculation has been done, it should constantly update the xaml view similar to how the websocket constantly the AskPrice and BidPrice in the view in realtime.
In other words, I have figured out how to get value from a websocket and display it in a xaml view, but I can';t seem to figure out how to get data from a xaml view, process it with data from a websocket, and then in realtime display the data in the xaml view.
Can anyone help?

