【问题标题】:How to call a longlistselector event from an option in context menu如何从上下文菜单中的选项调用 longlistselector 事件
【发布时间】:2013-11-15 21:16:56
【问题描述】:
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 选择器 = 作为 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”类型的异常,但未在用户代码中处理

【问题讨论】:

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


    【解决方案1】:

    我认为你可以做到:

    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;
    

    【讨论】:

    • 谢谢罗马兹。很有帮助
    【解决方案2】:

    这对你有用。上下文菜单的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.
        }
    }
    

    我相信您正在从 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;
            }
        } 
    

    【讨论】:

    • 非常感谢。是的,我正在关注 Bob Tabor 的视频。隔离存储文件 isofile = 隔离存储文件.GetUserStoreForApplication(); isofile.DeleteFile(data1.FilePath); isofile.DeleteFile(data1.Title); App.ViewModel.CustomSounds.Items.Remove(data1);这将删除我的隔离存储文件,但该按钮仍保留在页面中。为什么它没有被删除?
    • 尝试在App.ViewModel.CustomSounds.Items.Remove(data1); 行之后添加App.ViewModel.LoadData(); 如果它适合您,请接受答案,这样其他人就不必将此问题视为未回答。
    • 谢谢。是的,我前面提到过。无论如何将问题设置为“已解决”。我是 stackoverflow 和 windowsphoneapp 开发的新手。同时,添加 App.ViewModel.LoadData();对我不起作用。它仍在列表中。当我单击它时,控件会退出应用程序。
    • 如果它还在列表中,删除后点击它会发生什么?您可以通过单击正确答案下方的勾号将其设置为已解决。
    • longlistselector selectionchanged eventhandler: if (File.Exists(data.FilePath)) { AudioPlayer.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute); } else { using (var storageFolder = IsolatedStorageFile.GetUserStoreForApplication())//断点 { using (var stream = new IsolatedStorageFileStream(data.FilePath, FileMode.Open, storageFolder)) { AudioPlayer.SetSource(stream); } } }
    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多