无法将行为添加到 .Net Maui 中的 StateView。

huangapple go评论63阅读模式
英文:

Unable to add behaviour to StateView in .Net Maui

问题

我有一个Xamarin.forms应用程序,在其中我能够向StateView添加行为。现在我正在将项目迁移到.NET Maui。迁移项目后,我无法再向StateView添加行为了。似乎StateView在CommunityToolkit中变成了一个静态类。我该如何解决这个问题?

我如何向StateView添加行为

static void OnAttachBehaviorChanged(BindableObject view, object oldValue, object newValue)
{
    if (!(view is StateView stateView))
    {
        return;
    }

    bool attachBehavior = (bool)newValue;
    if (attachBehavior)
    {
        stateView.Behaviors.Add(new EntranceTransitionBehavior());
    }
    else
    {
        var toRemove = stateView.Behaviors.FirstOrDefault(b => b is EntranceTransitionBehavior);
        if (toRemove != null)
        {
            stateView.Behaviors.Remove(toRemove);
        }
    }
}

但现在在Maui中,我收到错误消息

'StateView'不包含对'Behaviors'的定义,也找不到接受类型为'StateView'的第一个参数的可访问扩展方法'Behaviors'(是否缺少使用指令或程序集引用?)

任何帮助都将不胜感激。

英文:

I had a Xamarin.forms app in which I am able to add. behaviour to a StateView. Now I am migrating the project into .Net Maui. After migrating the project I cant able to add the Behaviour to StateView anymore. It seems StateView became a static class in CommunityToolkit. How can I resolve this?

How I added the Behaviour to the StateView

static void OnAttachBehaviorChanged(BindableObject view, object oldValue, object newValue)
		{
			if (!(view is StateView stateView))
			{
				return;
			}

			bool attachBehavior = (bool)newValue;
			if (attachBehavior)
			{
				stateView.Behaviors.Add(new EntranceTransitionBehavior());
			}
			else
			{
				var toRemove = stateView.Behaviors.FirstOrDefault(b => b is EntranceTransitionBehavior);
				if (toRemove != null)
				{
					stateView.Behaviors.Remove(toRemove);
				}
			}
		}

But Now in Maui, I am getting error like

> 'StateView' does not contain a definition for 'Behaviors' and no
> accessible extension method 'Behaviors' accepting a first argument of
> type 'StateView' could be found (are you missing a using directive or
> an assembly reference?

Any help is appriciated.

答案1

得分: 1

在MAUI中,StateView属于StateContainer,StateView的类型是IList<View>,它是可用作状态模板的视图元素。这可能与Xamarin社区工具包的StateLayout有些不同。更多信息,请参考.NET MAUI Community Toolkit StateContainer

英文:

In the MAUI, the StateView belongs to StateContainer and the type of StateView is IList<View> and it is the available View elements to be used as state templates.
It might be a little different from the Xamarin Community Toolkit StateLayout.

More information you can refer to the .NET MAUI Community Toolkit StateContainer.

huangapple
  • 本文由 发表于 2023年5月24日 17:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定