2013-11-05 45 views
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事件

此代碼能夠引用longlistselector中的選項。由於Venkatapathi拉朱求助

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

私人無效LongListSelector_SelectionChanged(對象發件人,SelectionChangedEventArgs E) { LongListSelector選擇=發送者作爲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); 
       } 
      } 
     } 

我收到此錯誤信息 型「System.IO.IsolatedStorage.IsolatedStorageException」的異常出現在mscorlib.ni.dll但在用戶代碼中沒有處理

回答

0

這將爲你工作。 contextmenu的DataContext屬性爲您提供longlistselectormenuItem

可以執行刪除操作,如下圖所示:

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

非常感謝。是的,我正在關注Bob Tabor的視頻。 IsolatedStorageFile isofile = IsolatedStorageFile.GetUserStoreForApplication(); isofile.DeleteFile(data1.FilePath); isofile.DeleteFile(data1.Title); App.ViewModel.CustomSounds.Items.Remove(data1);這將刪除我隔離的存儲文件,但該按鈕仍保留在頁面中。爲什麼它沒有被刪除? – seshagopalan

+0

嘗試在App.ViewModel.CustomSounds.Items.Remove(data1);行後面添加App.ViewModel.LoadData();' 接受它,如果它適合您的答案,以便其他人不必查看此問題沒有答案。 –

+0

謝謝。是的,我在前面提到過。無論如何將問題設置爲「解決」這樣的事情。我是新的stackoverflow和windowsphoneapp開發。同時,添加App.ViewModel.LoadData();不適合我。它仍在列表中。當我點擊它時,控制將會退出應用程序。 – seshagopalan

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

還記得設置selectoed項目爲空(在方法的結束),因爲它的刪除:

myLongListSelector.SelectedItem = null; 
+0

謝謝羅馬茲。其有用的 – seshagopalan

相關問題