Xamarin表单如何在变量被设置为不同的值后更新屏幕上的图像?

bfrts1fy  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(109)

我想在后台代码更改了ImageSource值后更新显示在屏幕上的图像。我试过使用INotifyPropertyChnage,但如果只有我一个人,这似乎不起作用。

public string ImageSource
{
    get => _ImageSource;
    set
      {
         if (_ImageSource == value)
             return;

           _ImageSource = value;
           PropertyChanged(this, new PropertyChangedEventArgs(nameof(ImageSource)));
       }
 }

这是my GameLoop方法,用于将

private async Task GameLoop(int maxWordsInCurrentGame)
{
     for (int i = 1; i <= maxWordsInCurrentGame; i++)
     {
          var gameResources = GetSourcesFromConfig(CurrentGame, i, true, true, true, true, true, true, false);
          _ImageSource = gameResources["image"];
          _MediaSource = gameResources["video"];
           await Task.Delay(10000);
      }

   CurrentGame += 1;
   StartGame();
 }

我目前只有它等待10秒来改变图像,但它没有发生。
我将DataBinding用于我的XAML前端代码

<Image Source="{Binding ImageSource}"
               BackgroundColor="White"
               Grid.Row="1"
               VerticalOptions="Start"
               HeightRequest="75"
               Margin="0, 20, 0, 0"
               Aspect="AspectFill"/>

感谢您提前回答。

jljoyd4f

jljoyd4f1#

因此,您更新的属性是错误的:

public string ImageSource
{
    get => _ImageSource;
    set
      {
         if (_ImageSource == value)
             return;

           _ImageSource = value;
           PropertyChanged(this, new PropertyChangedEventArgs(nameof(ImageSource)));
       }
 }

您可以像这样编写代码,然后很明显更新“ImageSource”而不是“***_imageSource***"。

string _imageSource
public string ImageSource
{
    get => _imageSource;
    set
      {
         if (_imageSource == value)
             return;

           _imageSource = value;
           PropertyChanged(this, new PropertyChangedEventArgs(nameof(_imageSource)));
       }
 }

相关问题