【问题标题】:Xamarin trigger on button not firing in a FreshMvvm / SwipeView context按钮上的 Xamarin 触发器未在 FreshMvvm / SwipeView 上下文中触发
【发布时间】:2021-10-03 19:02:38
【问题描述】:

几乎相同的设置但没有SwipeView 上下文工作正常。 触发器被触发,我做“我的事”。

见以下xaml代码+PageModel.cs代码:

<CollectionView.ItemTemplate>
<DataTemplate>
    <SwipeView BackgroundColor="LightYellow">
        <SwipeView.RightItems>
            <SwipeItems Mode="Execute" SwipeBehaviorOnInvoked="RemainOpen">
                <SwipeItemView>
                    <StackLayout Orientation="Vertical" WidthRequest="60" BackgroundColor="LightGray" Padding="2,5,0,5" >
                        <Button x:Name="Btn_Assign" 
                                Text="{x:Static fa:FontAwesomeIcons.UserPlus}" FontSize="20" FontFamily="FAS" 
                                HorizontalOptions="Center" 
                                Command="{Binding AssignCommand}"/>
                        <Label Text="Assign" FontSize="Subtitle" HorizontalOptions="Center"/>
                    </StackLayout>
                </SwipeItemView>
            </SwipeItems>
        </SwipeView.RightItems>
    </SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>

PageModel.cs

public Command AssignCommand
{
    get
    {
        return new Command(ExecuteUpdateCommand);
    }
}
    

ExecuteUpdateCommand 函数包含要执行的实际代码。 AssignCommand 函数永远不会被访问。

【问题讨论】:

标签: xamarin button triggers swipeview freshmvvm


【解决方案1】:

根据您的描述,您需要使用Xamarin.Forms Relative Bindings为Button的Command绑定命令,我做了一个示例,在Collectionview中使用SwipView,然后删除当前项。

<StackLayout>
        <CollectionView ItemsSource="{Binding models}" SelectedItem="{Binding selecteditem}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <SwipeView>
                            <SwipeView.LeftItems>
                                <SwipeItems>
                                    <SwipeItem
                                        BackgroundColor="LightPink"
                                        Command="{Binding Path=BindingContext.deletecommand, Source={x:Reference page1}}"
                                        CommandParameter="{Binding Name}"
                                        IconImageSource="delete.png"
                                        Text="Delete" />
                                </SwipeItems>
                            </SwipeView.LeftItems>
                            <StackLayout>
                                <Label Text="{Binding Name}" />
                                <Label Text="{Binding Age}" />
                            </StackLayout>
                        </SwipeView>

                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

您需要命名 ContentPage 的名称

<ContentPage
x:Class="FormsSample.collectionviewsample.Page18"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="page1">

 public partial class Page18 : ContentPage
{
   
    public Page18()
    {
        InitializeComponent();
        this.BindingContext = new swipviewmodel();
       
    }      
}
public class swipviewmodel:ViewModelBase
{
    public ObservableCollection<swipmodel> models { get; set; }
    public ICommand deletecommand { get; set; }       
    public swipviewmodel()
    {
        models = new ObservableCollection<swipmodel>()
        {
            new swipmodel(){Name="cherry",Age=28},
            new swipmodel(){Name="wendy",Age=28},
            new swipmodel(){Name="barry",Age=28},
            new swipmodel(){Name="cole",Age=28},
            new swipmodel(){Name="leon",Age=28},
            new swipmodel(){Name="leo",Age=28},
            new swipmodel(){Name="stfan",Age=28}
        };
        deletecommand = new Command((obj)=> {
            if(obj!=null)
            {
                swipmodel selecteditem = models.FirstOrDefault(a => a.Name == (string)obj);
                models.Remove(selecteditem);
            }                            
        });
    }
   
}
public class swipmodel
{
    public string Name { get; set; }
    public int Age { get; set; }
}

截图:

【讨论】:

  • 谢谢Cherry Bu,让我先试试这个。
  • @JoukePostma 好的,期待您的反馈。
  • "您需要命名 ContentPage 的名称" 是的,正确!
  • 然后我做了以下事情: 这是有效的。
  • @JoukePostma 你还有什么问题吗?
猜你喜欢
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 2018-07-09
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多