【问题标题】:Change Xaml-Image.Source from a new Class (Xamarin)从新类 (Xamarin) 更改 Xaml-Image.Source
【发布时间】:2020-12-04 10:12:31
【问题描述】:

我是 Xamarin 的新手,我尝试通过新类从 Xaml.Page 更改 Image.Source。也许有人可以帮我解决这个问题。

例如 我有一个 PageX.xaml 和一个 Image x:Name="ImageX" 和一个新的 ClassY

现在我想在 Z 情况发生时将 ImageX.SourcePageX 更改。

对我的代码的一些见解:

Xaml:

<Image
   x:Name="ImageNonBossAnimation"
   BackgroundColor="Transparent"
   Grid.Row="18"
   Grid.RowSpan="10"
   Grid.Column="20"
   Grid.ColumnSpan="7"
/>

新类:

public static async void AttackSkills(string pressedSkill, string monsterType)
{
   if (pressedSkill == "SkillX")
      {
         if (monsterType == "nonBoss")
            {
               for (int animCounter = 1; animCounter <= 6; animCounter++)
                   {
                       PageX.ImageNonBossAnimation.Source = "anim_stroke" + animCounter.ToString();
                       await Task.Delay(30);
                   }
                   Page.ImageNonBossAnimation.Source = null;
            }
            --another occasion takes place here--
      }
}

我真的很感激任何帮助。

【问题讨论】:

  • 您好,欢迎您。您是否使用 MVVM 概念构建代码,这是 Xamarin 应用程序的首选方法?
  • 当 Z 情况发生时你在 PageY 并且你希望 PageX.Image 改变?
  • @ChrisBD 感谢您的回答。我想我没有使用 MVVM 概念。我正在使用普通的 Xamarin ContentPage,您可以在其中获得链接的 .cs。
  • @ShubhamTyagi 我在 PageX 上,正在调用一个新的 classY。我想用 classY 中的函数更改 PageX.Image。希望这能回答你的问题。感谢您的提问。

标签: c# xamarin xamarin.forms xamarin.android


【解决方案1】:

ImageNonBossAnimation 不是 PageX 中的静态 Item,所以不能像 PageX.ImageNonBossAnimation 那样访问它 将 Image 作为引用传递给 AttackSkills 这个方法,然后更改图像源。 喜欢

public static async void AttackSkills(string pressedSkill, string monsterType, ref Image ximgae)
{
   if (pressedSkill == "SkillX")
      {
         if (monsterType == "nonBoss")
            {
               for (int animCounter = 1; animCounter <= 6; animCounter++)
                   {
                       ximage.Source = "anim_stroke" + animCounter.ToString();
                       await Task.Delay(30);
                   }
                   ximage.Source = null;
            }
            --another occasion takes place here--
      }
}

并调用类似AttackSkills(parem1,parem2,ref ImageNonBossAnimation);的方法

【讨论】:

  • 感谢您的澄清,如果您使用“async”,您可以使用“ref”
【解决方案2】:

在@CodeSeekers 的帮助下,我找到了解决方案。

这相对容易,您可以从绑定到您的 .xaml 的 .cs 中调用新的 class Y

.cs 中的代码

ClassY.AttackSkills(passedSkill, monsterType, ImageX);

Y 类代码

public static async void AttackSkills(string pressedSkill, string monsterType, Image ImageNonBossAnimation)
    {
        if (pressedSkill == "SkillX")
        {
            if (monsterType == "nonBoss")
            {
                for (int animCounter = 1; animCounter <= 6; animCounter++)
                {
                    ImageX.Source = "anim_stroke" + animCounter.ToString();
                    await Task.Delay(30);
                }
                ImageX = null;
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多