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)
    {   
        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 as 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”类型的异常,但未在用户代码中处理

4

2 回答 2

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;
于 2013-11-05T06:59:52.593 回答
0

这对你有用。contextmenu的DataContext属性为您提供longlistselector's 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.
    }
}

我相信您正在从 Bob Tabor 的视频中学习。保持下去。

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;
        }
    } 
于 2013-11-05T07:35:33.413 回答