当前位置: 首页 > 知识库问答 >
问题:

如何从上下文菜单中的选项调用longlistselector事件

萧宁
2023-03-14
private void Deleting(object sender, RoutedEventArgs e)
{
    MessageBoxResult message = MessageBox.Show(
        "The file will be permanently deleted. Continue?",
        "Delete File", 
        MessageBoxButton.OKCancel
    );

    if (message == MessageBoxResult.OK)
    {   
        LongListSelector selector = sender as LongListSelector;

        SoundData data1 = selector.SelectedItem as SoundData;

       //control goes inside this block
        if (selector == null)
        {
            return;
        }

        if (data1 == null)
            return;
    }
}

我必须能够从长列表选择器中访问该数据。删除事件处理程序来自上下文菜单按钮

此代码能够引用长列表选择器中的选项。感谢文卡塔帕蒂·拉朱的帮助

      public void Deleting(object sender, RoutedEventArgs e)
        {
        SoundData data1 = (sender as MenuItem).DataContext as SoundData;

        MessageBoxResult message = MessageBox.Show(
        "The file will be permanently deleted. Continue?",
        "Delete File",
        MessageBoxButton.OKCancel
        );

        if (message == MessageBoxResult.OK)
        { 

私有void LongListSelector_SelectionChanged(对象发送者,SelectionChangedEventArgs e){LongListSelector选择器=发送者作为longlist选择器;

        if (selector == null)
            return;

        SoundData data = selector.SelectedItem as SoundData;

        if (data == null)
            return;

        if (File.Exists(data.FilePath))
        {
            AudioPlayer.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute);
        }
        else
        {
            using (var storageFolder = IsolatedStorageFile.GetUserStoreForApplication())
            {
                //Breakpoint 
         using (var stream = new IsolatedStorageFileStream(data.FilePath, FileMode.Open, storageFolder))
                 {
                    AudioPlayer.SetSource(stream);
                }
            }
        }

我得到了这个错误消息,是一个“System”类型的异常。“IO . isolatedstorage . isolatedstorageexception”在mscorlib.ni.dll中发生,但未在用户代码中处理

共有2个答案

茅才
2023-03-14

我认为你可以做到:

private void Deleting(object sender, RoutedEventArgs e)
{
    MessageBoxResult message = MessageBox.Show(
        "The file will be permanently deleted. Continue?",
        "Delete File", 
        MessageBoxButton.OKCancel
    );

    if (message == MessageBoxResult.OK)
    {                  
        SoundData data1 = myLongListSelector.SelectedItem as SoundData;

        if (data1 == null)
            return;

       //control goes inside this block  
    }
}

还记得将选择的项目设置为null(在方法末尾),因为它被删除了:

myLongListSelector.SelectedItem = null;
微生运浩
2023-03-14

这对你有用。contextmenu的< code>DataContext属性提供了< code>longlistselector的< code>menuItem。

您可以执行删除操作,如下所示:

private void Deleting(object sender, RoutedEventArgs e)
{
    SoundData data1 = (sender as MenuItem).DataContext as SoundData;

    MessageBoxResult message = MessageBox.Show(
    "The file will be permanently deleted. Continue?",
    "Delete File", 
    MessageBoxButton.OKCancel
    );

    if (message == MessageBoxResult.OK)
    {   
        //Call the method which deletes the data and pass data1 to it.
    }
}

我相信你正在从鲍勃·塔博尔的视频中学习。继续前进。

private void Delete_Click(object sender, RoutedEventArgs e)
    {

        SoundData data = (sender as MenuItem).DataContext as SoundData;
        MessageBoxResult result = MessageBox.Show("Do you want to delete this item ?", "Are you sure ?", MessageBoxButton.OKCancel);

        if (result == MessageBoxResult.OK)
        {
            if (data == null)
            {
                MessageBox.Show("The file doesn't exist");
                return;
            }

            using (var storageFolder = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storageFolder.FileExists(data.FilePath))
                {
                    storageFolder.DeleteFile(data.FilePath);

                    App.ViewModel.CustomSounds.Items.Remove(data);

                    // Save the list of CustomSounds to IsolatedStorage.ApplicationSettings
                    var JsonData = JsonConvert.SerializeObject(App.ViewModel.CustomSounds);

                    IsolatedStorageSettings.ApplicationSettings[SoundModel.CustomSoundKey] = JsonData;
                    IsolatedStorageSettings.ApplicationSettings.Save();

                    App.ViewModel.IsDataLoaded = false;
                    App.ViewModel.LoadData();

                }
                else
                {
                    MessageBox.Show("File doesn't exist");
                    return;
                }
            }
        }
        else
        {
            return;
        }
    } 
 类似资料:
  • 我想从下面的列表中选择一个使用selenium的选项: 这里 但问题是没有列表可供选择。 在此输入图像说明 我到目前为止的代码: 需要帮助!!

  • 我在Visual Studio C#IDE中定义了一个ContextMenuStri上下文菜单,并对程序进行编码,以便当用户右键单击屏幕上的一个对象时,上下文菜单成功弹出。用户可以从上下文菜单中选择一个项目,代码成功确定用户选择了哪个上下文菜单项。到目前为止还不错。问题是上下文菜单接受右键单击作为选择上下文菜单项的机制。当我不小心双击右键单击屏幕上的一个对象时,我注意到了这一点...菜单在第一次单

  • 问题内容: 我想知道是否有可能让jQuery 在下拉框中选择,例如第4个项目? 我希望用户单击一个链接,然后让该框更改其值,就像用户通过单击来选择它一样。 问题答案: 怎么样 对于现代版本的jquery,应使用代替

  • 问题内容: 我有随ArcGIS 10.2.2一起安装的Python 2.7.5。当我第一次右键单击.py脚本时,我先前已经编写了该脚本,并在上下文菜单中列出了“使用IDLE编辑”选项。但是,当我右键单击.py文件时,此选项不再出现。 我已经阅读了许多有关此问题的线程,并尝试了其中的一些线程,例如修改/删除注册表项,然后重新安装/修复软件。目前,我对使用IDE并不感兴趣,尽管许多人很高兴知道以后打算

  • 编辑:Calrification-标签内容是窗格(VBox,GridPane,等等),所以直接在内容上设置ContextMenu是不可能的。

  • 因此,我仍然有点了解JavaFX,我能够禁止在文本框中输入文本,但我不知道如何防止右键单击时出现上下文菜单。有人知道如何防止右键单击时弹出默认上下文菜单吗?`