我创建了 View 和 ViewModel
public class LoginUserViewModel : ViewModelBase
{
private string _appName;
public string AppName
{
get => _appName;
set => this.RaiseAndSetIfChanged(ref _appName, value);
}
public string AppVersion { get; set; }
public LoginUserViewModel()
{
AppName = LoadConfig().Item1;
AppVersion = LoadConfig().Item2;
// var messageBoxStandardWindow = MessageBox.Avalonia.MessageBoxManager
// .GetMessageBoxStandardWindow("test", AppName + " " + AppVersion);
// messageBoxStandardWindow.Show();
}
(string, string) LoadConfig()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json",true, true);
var configuration = builder.Build();
var name = configuration.GetSection("AppProperties").GetSection("AppName").Value;
var version = configuration.GetSection("AppProperties").GetSection("AppVersion").Value;
return (name, version);
}
}
和查看片段
<TextBlock Text="{Binding AppName}"
FontSize="50"
Width="200"
Height="100"
TextAlignment="Center"
Foreground="White"/>
MessageBox 显示 AppName 包含数据,但是当我运行应用程序时,TextBlock 为空。
有任何想法吗?