【问题标题】:MyImage.Source index is not changing with the listMyImage.Source 索引不随列表变化
【发布时间】:2021-11-28 16:10:31
【问题描述】:

有人可以帮我解决索引问题吗? MyImage.Source 行已添加,它似乎没有更新列表中的图像。文字更新得很好。如何从 GrapplingGuard[ndx].ImageA 获取索引以更新图像?

最初 GrapplingGuard[ndx].ImageAGrapplingGuard[0] 但我认为我需要指出它应该与 [ndx] 相关。 ImageAI 试图在绑定上下文之后移动该行,但这并没有改变任何东西。我需要在 OnButtonClicked 部分中包含一些内容吗?

public partial class BGGrappling : ContentPage
{
    List<GrapplingGuard> GrapplingGuard { get; set; }

    int ndx = 0;

    public BGGrappling()
    {
        InitializeComponent();

        GrapplingGuard = new List<GrapplingGuard>
        {
            new GrapplingGuard
            {
                TitleText = "Long Guard",
                EngText = "Eng Text 1",
                ItText = "IT Text 1",
                PageNo = "1/4",
                ImageA = "HemaSwordFiore.Images.BGGrapple.LongGuard.png"
            },
            new GrapplingGuard
            {
                TitleText = "Boar's Tooth",
                EngText = "Eng Text 2",
                ItText = "IT Text 2",
                PageNo = "2/4",
                ImageA = "HemaSwordFiore.Images.BGGrapple.BoardsTooth.png"
            }
        };

        //[ndx] not changing image from list
        MyImage.Source = ImageSource.FromResource(GrapplingGuard[ndx].ImageA, typeof(BGGrappling).GetTypeInfo().Assembly);

        BindingContext = GrapplingGuard[ndx];
    }
    async void OnButtonClicked(object sender, EventArgs e)
    {
        ndx++;

        if (ndx < GrapplingGuard.Count && ndx <= 3)
        {
            BindingContext = GrapplingGuard[ndx];
        }
        else
        {
            await Navigation.PopAsync();
        }
    }

【问题讨论】:

  • MyImage控件是直接设置源还是在xaml中设置源绑定?如果使用绑定,是否使用 INotifyPropertyChanged 进行更新?如果直接设置源,是否将构建动作设置为嵌入资源?
  • 嗨@WendyZang-MSFT,我在xaml 端有一个绑定(x:Name="MyImage")。我已经添加了 INotifyPropertyChanged 但它需要设置到列表 List GrapplingGuard 因为这是更新但我不断收到一个错误,即 PropertyChange?.Invoke 必须在 += 或之前-+
  • 您能否提供更多详细信息以及代码让我重现?
  • 嗨@WendyZang-MSFT,我为 MyImage 字符串做了以下操作,因为这是指南在 MS 站点上的 Databinding 上所做的,但没有运气:** public string MyImage { get => MyImage ;设置 { MyImage = 值; var args = new PropertyChangedEventArgs(nameof(MyImage)); PropertyChanged?.Invoke(this, args); } }**
  • 如果可以的话,能否提供代码示例让我复现?您可以上传到 github 或其他驱动器并给我链接。

标签: c# image xamarin.forms indexing


【解决方案1】:

我找到了解决方案,我必须在递增部分添加MyImage.Source

async void OnButtonClicked(object sender, EventArgs e)
{
    ndx++;

    if (ndx < GrapplingGuard.Count && ndx <= 3)
    {
        BindingContext = GrapplingGuard[ndx];
    }
    else
    {
        await Navigation.PopAsync();
    }
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多