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

时间:2013-11-05 03:38:58

标签: c# windows-phone-8 contextmenu longlistselector

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;
    }
}

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

此代码能够引用longlistselector中的选项。感谢Venkatapathi Raju的帮助

      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)
        { 

private void LongListSelector_SelectionChanged(object sender,SelectionChangedEventArgs e)         {             LongListSelector selector = sender作为LongListSelector;

        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);
                }
            }
        }

我收到此错误消息 mscorlib.ni.dll中出现“System.IO.IsolatedStorage.IsolatedStorageException”类型的异常,但未在用户代码中处理

2 个答案:

答案 0 :(得分:0)

我认为你可以做到:

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;

答案 1 :(得分:0)

这对你有用。 contextmenu的DataContext属性为您提供longlistselector的{​​{1}}。

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

menuItem

我相信你正在学习Bob Tabor的视频。坚持下去。

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.
    }
}