【问题标题】:Xamarin Forms Binding not populated until reloadXamarin 表单绑定直到重新加载才填充
【发布时间】:2020-06-19 19:11:49
【问题描述】:

我有一个 Xamarin Forms 应用程序,该应用程序显示绑定到类 (RandomChallenge) 的图片。该类在视图模型中生成并分配。该类有一个 Uri 属性,我们希望将其绑定到视图中的图像。

当应用加载时,它不会显示任何图片,即使 RandomChallenge 不为空。如果我在调试时更改 UI 代码并将其保存,则当调试器重新加载时,图像是可见的。好像 RandomChallenge 类在页面呈现后被填充?如果我要在 RandomChallenge = await ChallengeDataStore.GetRandomChallenge(); RandomChallenge 已填充。

视图模型

public class MainMenuViewModel : BaseViewModel
{
    public ICommand OpenWebCommand { get; }

    public IdentificationChallenge RandomChallenge { get; set; }

    public Command LoadChallengeCommand { get; set; }

    public MainMenuViewModel()
    {
        Title = "Main Menu";

        LoadChallengeCommand = new Command(async () => await ExecuteLoadChallengeCommand());

        this.LoadChallengeCommand.Execute(null);
    }

    async Task ExecuteLoadChallengeCommand()
    {
        if (IsBusy)
            return;

        try
        {
            RandomChallenge = await ChallengeDataStore.GetRandomChallenge();

            Debug.WriteLine("TEst");
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        finally
        {
            IsBusy = false;
        }
    }
}

用户界面

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:microcharts="clr-namespace:Microcharts.Forms;assembly=Microcharts.Forms"
         mc:Ignorable="d"
         x:Class="HOG.MobileApp.Views.MainMenuPage"
         xmlns:vm="clr-namespace:HOG.MobileApp.ViewModels"
          xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"    
         Title="{Binding Title}">





<ContentPage.Resources>
    <ResourceDictionary>
        <Color x:Key="Primary">#72a230</Color>
        <Color x:Key="Accent">#72a230</Color>
        <Color x:Key="LightTextColor">#72a230</Color>
    </ResourceDictionary>
</ContentPage.Resources>

<ContentPage.ToolbarItems>
    <ToolbarItem Text="Help" />
</ContentPage.ToolbarItems>

<ContentPage.Content>
    <Image Source="{ Binding RandomChallenge.Picture }" HeightRequest="450" WidthRequest="450" />
</ContentPage.Content>

【问题讨论】:

  • RandomChallenge 的 setter 不调用 PropertyChanged
  • @Jason 你有一个如何实现这个的例子吗?
  • 现有数以千计的关于如何使用 INotifyPropertyChanged 的​​帖子、文章、博客、教程等
  • 我建议您使用框架来帮助您实现 MVVM 标准,例如 Prism。使用 RaisePropertyChanged () 方法会很有用。

标签: xamarin xamarin.forms


【解决方案1】:

所以答案是在分配 RandomChallenge 时调用 OnPropertyChanged。 ViewModel 确实在其父类上实现了 INotifyPropertyChanged。

async Task ExecuteLoadChallengeCommand()
    {
        if (IsBusy)
            return;

        try
        {
            RandomChallenge = await ChallengeDataStore.GetRandomChallenge();

            OnPropertyChanged("RandomChallenge");

            Debug.WriteLine("TEst");
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        finally
        {
            IsBusy = false;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多