请原谅我对这个问题的无知,但我没有受过异步编程的教育。但是,我相信我的代码已接近实现我需要它做的事情。
基本上下面的代码仅适用于第一个
C61 = new BridgeViewModel("C61");
await C61.Initialize();
语句。没有其他项目将任何数据返回到绑定的.xaml,我不知道为什么。每个人都需要在自己的Initialize函数中吗?
非常感谢任何帮助。
代码:
namespace SentinelApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new MainViewModel();
DataContext = vm;
vm.Initialize();
}
}
public class MainViewModel
{
public readonly string[] Bridges = { "C61", "C62", "C63", "C68", "C69", "K71", "K72", "K73", "K74", "T91", "GN01", "GE01", "GA01" };
public async Task Initialize()
{
C61 = new BridgeViewModel("C61");
await C61.Initialize();
C62 = new BridgeViewModel("C62");
await C62.Initialize();
C63 = new BridgeViewModel("C63");
await C63.Initialize();
C68 = new BridgeViewModel("C68");
await C68.Initialize();
C69 = new BridgeViewModel("C69");
await C69.Initialize();
K71 = new BridgeViewModel("K71");
await K71.Initialize();
K72 = new BridgeViewModel("K72");
await K72.Initialize();
K73 = new BridgeViewModel("K73");
await K73.Initialize();
K74 = new BridgeViewModel("K74");
await K74.Initialize();
T91 = new BridgeViewModel("T91");
await T91.Initialize();
GA01 = new BridgeViewModel("GA01");
await GA01.Initialize();
GE01 = new BridgeViewModel("GE01");
await GE01.Initialize();
GN01 = new BridgeViewModel("GN01");
await GN01.Initialize();
}
public BridgeViewModel C61 { get; set; }
public BridgeViewModel C62 { get; set; }
public BridgeViewModel C63 { get; set; }
public BridgeViewModel C68 { get; set; }
public BridgeViewModel C69 { get; set; }
public BridgeViewModel K71 { get; set; }
public BridgeViewModel K72 { get; set; }
public BridgeViewModel K73 { get; set; }
public BridgeViewModel K74 { get; set; }
public BridgeViewModel T91 { get; set; }
public BridgeViewModel GA01 { get; set; }
public BridgeViewModel GE01 { get; set; }
public BridgeViewModel GN01 { get; set; }
}
public class BridgeViewModel : ViewModel
{
private readonly ClientWrapper _client;
private readonly string _bridge;
private readonly Timer _timer = new Timer();
public BridgeViewModel(string bridge)
{
_client = new ClientWrapper();
_bridge = bridge;
}
public async Task Initialize()
{
await _client.Connect(_bridge);
await _client.SendMessage(new SessionStart("3", "25").CreateBridgeMessage());
_timer.Interval = 1000;
_timer.Elapsed += Update;
_timer.Start();
}
private async void Update(object sender, ElapsedEventArgs e)
{
try {
var response = await _client.SendMessage("BS~RESERVE~STATS~REQ~" + _bridge + "~0");
var split = response.Split('~');
Timestamp = split[4].Substring(0, 2) + ":" + split[4].Substring(2, 2) + ":" + split[4].Substring(4, 2);
FreePorts = split[6];
LongestHold = TimeSpan.FromSeconds(int.Parse(split[15])).ToString("hh:mm");
Bells = split[12];
Signals = split[8];
} catch { }
}
private string _timestamp;
public string Timestamp
{
get { return _timestamp; }
set { _timestamp = value; RaisePropertyChanged(); }
}
private string _bells;
public string Bells
{
get { return _bells; }
set { _bells = value; RaisePropertyChanged(); }
}
private string _signals;
public string Signals
{
get { return _signals; }
set { _signals = value; RaisePropertyChanged(); }
}
private string _freeports;
public string FreePorts
{
get { return _freeports; }
set { _freeports = value; RaisePropertyChanged(); }
}
private string _longesthold;
public string LongestHold
{
get { return _longesthold; }
set { _longesthold = value; RaisePropertyChanged(); }
}
}
}
答案 0 :(得分:4)
不要在构造函数中使用异步方法,而是在docs事件中注册并使用它await
:
public MainWindow()
{
InitializeComponent();
Loaded += InitializeOnLoaded;
}
public async void InitializeOnLoaded(object sender, RoutedEventArgs e)
{
var vm = new MainViewModel();
DataContext = vm;
await vm.InitializeAsync();
}