首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MessagingCenter不订阅

MessagingCenter不订阅
EN

Stack Overflow用户
提问于 2021-05-27 14:01:55
回答 1查看 182关注 0票数 1

我对Xamarin很陌生(而且在编码方面也是新的)。

我在ListView中使用ListView来尝试获取LongPress菜单。

因为TouchEffect.LongPressCommand是一个命令,所以我只能出于某种原因将它绑定到模型页面。所以..。我正试图通过MessagingCenter将信息发送到代码隐藏。

我的问题是没有收到信息。

我读了很多,并试图弄清楚它,我想要能够接收到消息,订阅者需要先实例化/初始化。

我的主要问题是..。我不知道怎么做,哈哈。

还是我想做的一切都错了?

我会添加一些代码,但如果还需要什么,请告诉我。

注意:加载页面(当应用程序启动时)是一个GroupPage(运行良好),问题在于ItemsPage。

非常感谢大家。

ItemsPage.xaml

代码语言:javascript
复制
<ContentPage.BindingContext>
    <localvm:ItemViewModel/>
</ContentPage.BindingContext>

<ListView ItemsSource="{Binding Items, Mode=TwoWay}" x:Name="lstView" 
                  AbsoluteLayout.LayoutBounds="0,0,1,1" 
                  AbsoluteLayout.LayoutFlags="All"
                  SelectedItem="{Binding SelectedItem}" HasUnevenRows="True" RowHeight="50">

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>

                <Grid Padding="0,0,8,0" Margin="4,0,4,0" xct:TouchEffect.LongPressCommand="{Binding LongPressItemCommand}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="7*"/>
                    </Grid.ColumnDefinitions>

                    <Label Text="{Binding ItemName}" TextColor="Black" Grid.Column="1" FontSize="Medium"></Label>

                    <Label Text="{Binding ItemDescription}" Grid.Column="1" VerticalTextAlignment="End"/>

                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ItemsPage.cs

代码语言:javascript
复制
namespace MobileApp2
{
    public partial class ItemsPage : ContentPage
    {
        public ItemsPage()
        {
            InitializeComponent();
            MessagingCenter.Subscribe<Item, Guid>(this, "PopupMenuItemMsg",
                 (page, itemId) =>
                 {
                     Main_PopupMenu(itemId);
                 });
        }
        public async void Main_PopupMenu(Guid itemId)
        {
            DisplayActionSheet("Test", "Test", "OK");
        }
    }
}

Items.cs (模型)

代码语言:javascript
复制
namespace MobileApp2
{
    public class Item : INotifyPropertyChanged
    {
        public Command LongPressItemCommand { get; }

        public Guid ItemId { get; set; }

        public Guid GroupId { get; set; }

        private string itemName = string.Empty;
        public string ItemName
        {
            get { return itemName; }
            set
            {
                if (value != null) itemName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ItemName"));
            }
        }

        private string itemDescription = string.Empty;
        public string ItemDescription
        {
            get
            {

                return itemDescription.Trim();
            }
            set
            {
                if (value != null) itemDescription = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ItemDescription"));
            }
        }


        public Item(string itemName, string itemDescription)
        {
            ItemName = itemName;
            ItemDescription = itemDescription;
        }

        public Item()
        {
            LongPressItemCommand = new Command(() =>
            {
                MessagingCenter.Send<Item, Guid>(this, "PopupMenuItemMsg", ItemId);
            });
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-06-03 08:03:55

您可以使用命令的相对绑定来检查下面的代码。

型号:

代码语言:javascript
复制
public class Item
{
    public string ItemName { get; set; }
    public string ItemDescription { get; set; }
   

}

ViewModel:

代码语言:javascript
复制
public class ItemViewModel
{
    public ICommand LongPressItemCommand { get; set; }
    public Guid ItemId { get; set; }
    public ObservableCollection<Item> Items { get; set; }
    public ItemViewModel()
    {
         LongPressItemCommand = new Command(() =>
        {

            MessagingCenter.Send<ItemViewModel, Guid>(this, "PopupMenuItemMsg", ItemId);
        });
        
        CreateCollection();
    }



    public void LongPress()
    {

    }
    public void CreateCollection()
    {
        Items = new ObservableCollection<Item>()
        {
            new Item(){ ItemName="A", ItemDescription="AA"},
            new Item(){ ItemName="B", ItemDescription="BB"},
            new Item(){ ItemName="C", ItemDescription="CC"},
        };

    }
}

Xaml:

代码语言:javascript
复制
  <ContentPage.BindingContext>
    <localvm:ItemViewModel></localvm:ItemViewModel>
</ContentPage.BindingContext>
<StackLayout>
    <ListView ItemsSource="{Binding Items, Mode=TwoWay}" x:Name="lstView" 
              AbsoluteLayout.LayoutBounds="0,0,1,1" 
              AbsoluteLayout.LayoutFlags="All"
              SelectedItem="{Binding SelectedItem}" HasUnevenRows="True" RowHeight="50">

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="0,0,8,0" Margin="4,0,4,0" xct:TouchEffect.LongPressCommand="{Binding Path=BindingContext.LongPressItemCommand, Source={x:Reference Name=lstView}}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <Label Text="{Binding ItemName}" TextColor="Black" Grid.Column="0" FontSize="Medium"></Label>

                        <Label Text="{Binding ItemDescription}" Grid.Column="1" VerticalTextAlignment="End"/>

                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
   
</StackLayout>

代码背后:

代码语言:javascript
复制
 MessagingCenter.Subscribe<ItemViewModel, Guid>(this, "PopupMenuItemMsg",
           (page, itemId) =>
           {
               Main_PopupMenu(itemId);
           });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67723762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档