我正在创建一个天气应用程序,我从 API 获得当前天气状态的响应。因为它是一个字符串,所以我可以使用 [ObservableProperty] 注释将其存储在公共字符串 currentWeather 中。由于我的 Resources/Raw 文件夹中存储有动画,因此我尝试从 xaml 代码将 SKLottieView 绑定到由放置在视图模型中的 [ObservableProperty] 创建的 CurrentWeather 属性。问题是,它确实通过 HeightRequest 和 WidthRequest 占用了我的应用程序中的空间,但它不可见。
XAML code
<skia:SKLottieView
Source="{Binding CurrentWeather}"
RepeatCount="-1"
HeightRequest="300"
WidthRequest="200"
HorizontalOptions="Center" >
</skia:SKLottieView>
ViewModel code
[ObservableProperty]
public string currentWeather;
public YourCurrentLocationForecast(WeatherService weatherService, IConnectivity connectivity, IGeolocation geolocation)
{
this.weatherService = weatherService;
this.connectivity = connectivity;
this.geolocation = geolocation;
title = "Weather";
CurrentWeather = "night_raining.json";
TestingTheDailyStatMethod();
}
正如您所注意到的,我首先尝试对 CurrentWeather 属性进行硬编码,只是为了在创建转换器类以将从 api 接收到的状态字符串转换为 json 动画名称之前检查它是否有效。我想看看它是否有效。您对我应该如何在 xaml 代码中绑定有什么建议吗?也许我没有做正确的事情。
一开始尝试了与基于字符串绑定图像相同的方式,但它仍然不可见。
根据SKLottieView文档,Source属性是
SKLottieImageSource
类型。它无法自动将字符串转换为 SKLottieImageSource
您可以尝试以下方式设置Source属性,
[ObservableProperty]
public SKFileLottieImageSource currentWeather = new SKFileLottieImageSource();
public YourCurrentLocationForecast(WeatherService weatherService, IConnectivity connectivity, IGeolocation geolocation)
{
....
currentWeather.File = "night_raining.json";
}
希望有帮助!