gpt4 book ai didi

c# - 上下文菜单继续使用Mvvm获取错误的对象

转载 作者:行者123 更新时间:2023-12-03 10:28:27 24 4
gpt4 key购买 nike

我正在制作winodws 8电话应用程序,并尝试从Windows Phone工具包中获得上下文菜单。

我一直在关注此tutorial,但不是使用列表框,而是使用WP8内置的长列表选择器

<DataTemplate x:Key="GroceryListItemTemplate">
<StackPanel Grid.Column="1" Grid.Row="1">
<TextBlock x:Name="tbName" TextWrapping="Wrap" Text="{Binding Name}" FontSize="32"/>
<TextBlock x:Name="tbProductInfo" TextWrapping="Wrap" Text="{Binding ProductInfoLabel}" HorizontalAlignment="Left"/>
</StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Edit"
Command="{Binding GroceryItemsVm.EditGroceryItemCmd, Source={StaticResource Locator}}"
CommandParameter="{Binding}"/>
<toolkit:MenuItem Header="Delete" Command="{Binding GroceryItemsVm.DeleteGroceryItemCmd, Source={StaticResource Locator}}"
CommandParameter="{Binding}"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</DataTemplate>

上面是我的代码的简写

这是我的列表选择器
<phone:LongListSelector IsGroupingEnabled="True" ItemsSource="{Binding GroceryItems}"    HideEmptyGroups="True" LayoutMode="List" Grid.Row="1">     
<phone:LongListSelector.ItemTemplate>
<StaticResource ResourceKey="GroceryListItemTemplate"/>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

这是我的MVVM代码
public class GroceryItemsVm : ViewModelBase
{

public GroceryItemsVm()
{


if (IsInDesignMode)
{


}
else
{

EditGroceryItemCmd = new RelayCommand<GroceryItem>(this.Edit);
DeleteGroceryItemCmd = new RelayCommand<GroceryItem>(this.Delete);

GroceryItems = // method that gets all items back as grouped.



}
}

private List<Group<GroceryItem>> groceryItems = null;

/// <summary>
/// Sets and gets the GroceryItems property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public List<Group<GroceryItem>> GroceryItems
{
get
{
return groceryItems;
}

set
{
if (groceryItems == value)
{
return;
}

RaisePropertyChanging(() => GroceryItems);
groceryItems = value;
RaisePropertyChanged(() => GroceryItems);
}
}




private async void Delete(GroceryItem obj)
{
// trigged on context delete
}

private void Edit(GroceryItem obj)
{
// triggered on context edit
}



public RelayCommand<GroceryItem> EditGroceryItemCmd
{
get;
private set;
}

public RelayCommand<GroceryItem> DeleteGroceryItemCmd
{
get;
private set;
}

}


public class GroceryItem : ObservableObject
{
/// <summary>
/// The <see cref="Name" /> property's name.
/// </summary>
public const string NamePropertyName = "Name";

private string name = "";

/// <summary>
/// Sets and gets the Name property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string Name
{
get
{
return name;
}

set
{
if (name == value)
{
return;
}

RaisePropertyChanging(() => Name);
name = value;
RaisePropertyChanged(() => Name);
}
}
}

现在,当我运行它时,它第一次起作用,无论我选择对其进行编辑的任何项目,都将得到正确的对象。但是,下一个对象将始终是相同的。选择完成后,它永远不会更改其选择。

编辑

这是一个例子。

https://onedrive.live.com/redir?resid=FAE864D71B4770C6!19080&authkey=!ACUC2xXmZLVD7fE&ithint=file%2c.zip
  • 运行它
  • 触发上下文菜单以显示“1”以上
  • 点击编辑-注释对话框消息(会说1)
  • 点击“返回按钮”
  • 触发上下文菜单以显示“3”以上
  • 点击编辑-注释对话框消息(将显示3)

  • 我唯一能想到的就是覆盖我要访问的页面的后退按钮,而只需导航到该页面即可。这有点愚蠢,但这就是我能想到的。
      public partial class MvvmView1 : PhoneApplicationPage
    {
    // Constructor
    public MvvmView1()
    {
    InitializeComponent();

    // Sample code to localize the ApplicationBar
    //BuildLocalizedApplicationBar();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
    NavigationService.GoBack();
    }


    protected override void OnBackKeyPress(CancelEventArgs e)
    {
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }


    }

    最佳答案

    这是ContextMenu的常见问题。我已经尝试了一段时间,想出一个解决方案,四处寻找。您说过,单击一次永远不会正确。

    请尝试以下操作:

    将卸载的处理程序添加到您的contextmenu中,如下所示:

    <DataTemplate x:Key="GroceryListItemTemplate">
    <StackPanel Grid.Column="1" Grid.Row="1">
    <TextBlock x:Name="tbName" TextWrapping="Wrap" Text="{Binding Name}" FontSize="32"/>
    <TextBlock x:Name="tbProductInfo" TextWrapping="Wrap" Text="{Binding ProductInfoLabel}" HorizontalAlignment="Left"/>
    </StackPanel>
    <toolkit:ContextMenuService.ContextMenu>
    <toolkit:ContextMenu ***Unloaded="ContextMenu_Unloaded"***>
    <toolkit:MenuItem Header="Edit"
    Command="{Binding GroceryItemsVm.EditGroceryItemCmd, Source={StaticResource Locator}}"
    CommandParameter="{Binding}"/>
    <toolkit:MenuItem Header="Delete" Command="{Binding GroceryItemsVm.DeleteGroceryItemCmd, Source={StaticResource Locator}}"
    CommandParameter="{Binding}"/>
    </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
    </DataTemplate>

    删除*我添加它们以强调更改。
    然后,该处理程序的代码如下:
    private void ContextMenu_Unloaded(object sender, RoutedEventArgs e)
    {
    var conmen = (sender as ContextMenu);
    if (conmen != null)
    conmen.ClearValue(DataContextProperty);
    }

    让我知道这个是否奏效。

    关于c# - 上下文菜单继续使用Mvvm获取错误的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22509401/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com