2014-03-06 59 views
0

我正在使用longlistselector,我想要获取Hold活动中所选项目中的文字。不幸的是,Hold不会创建SelectedItem,所以我必须做一个解决方法。我已经读了很多关于这个问题,但不能得到一个完全可行的解决方案。这是我得到的错误:Unable to cast object of type PhoneApp2.Favs键入'System.String'.`。只有当我在项目中的文本旁边的开放空间中放置时才会出现此错误。我怎样才能解决这个问题?从Longlistselector获取项目举办活动

相关C#:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Navigation; 
using Microsoft.Phone.Controls; 
using Microsoft.Phone.Shell; 
using System.Windows.Media; 
using System.Collections.ObjectModel; 
using System.Xml; 
using PhoneApp2.Resources; 
using System.Xml.Linq; 
using System.IO; 
using System.IO.IsolatedStorage; 
using System.Windows.Resources; 

namespace PhoneApp2 
{ 
    public class Favs 
    { 
     private string drank; 

     public string Name 
     { 
      get { return drank; } 
      set { drank = value; } 
     } 
     public Favs(string addition) 
     { 
      this.Name = addition; 
     } 
    } 

    public partial class Favorites : PhoneApplicationPage 
    { 
     ObservableCollection<Favs> Favlist = new ObservableCollection<Favs>(); 
     Array list; 
     Boolean alpha = false; 
     public Favorites() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
     { 
      //Populate LLL listBar 
      listFavs.ItemsSource = Favlist; 

      try 
      { 
       // copy the xml file to isolated storage 
       using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        if (!file.FileExists("favorites.xml")) 
        { 
         StreamResourceInfo sr_en = Application.GetResourceStream(new Uri("Resources\\favorites.xml", UriKind.Relative)); 
         using (BinaryReader br_en = new BinaryReader(sr_en.Stream)) 
         { 
          byte[] data = br_en.ReadBytes((int)sr_en.Stream.Length); 
          //Write the file. 
          using (BinaryWriter bw = new BinaryWriter(file.CreateFile("favorites.xml"))) 
          { 
           bw.Write(data); 
           bw.Close(); 
          } 
         } 
        } 
        // work with file at isolatedstorage 
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("favorites.xml", FileMode.Open, file)) 
        { 
         XDocument xDoc = XDocument.Load(stream, LoadOptions.None); 
         list = xDoc.Descendants("cocktail").Select(n => n.Value).ToArray(); 
         Array.Sort(list); 
         alpha = true; 
         foreach (string name in list) 
         { 
          Favlist.Add(new Favs(name)); 
         } 

        } 
       } 
       Dispatcher.BeginInvoke(() => 
       { 
        pbLoading.IsIndeterminate = false; 
        pbLoading.Visibility = Visibility.Collapsed; 
       }); 
      } 
      catch (IOException IOExc) 
      { 
       MessageBox.Show(IOExc.Message); 
      } 
      catch (XmlException XmlExc) 
      { 
       MessageBox.Show(XmlExc.Message); 
      } 
      catch (Exception myExc) 
      { 
       MessageBox.Show(myExc.Message); 
      } 
     } 

     private void listFavs_Hold(object sender, System.Windows.Input.GestureEventArgs e) 
     { 
      try 
      { 
       FrameworkElement element = (FrameworkElement)e.OriginalSource; 
       String item = (String)element.DataContext; 
       var booze = item.ToString(); 

       Dispatcher.BeginInvoke(() => 
       { 
        CustomMessageBox messageBox = new CustomMessageBox() 
        { 
         Caption = "Delete " + booze, 
         Message = "Are you sure you want to remove " + booze + " from your favorites? This cannot be made undone!", 
         LeftButtonContent = "Yes", 
         RightButtonContent = "No" 
        }; 
        messageBox.Dismissed += (s1, e1) => 
        { 
         switch (e1.Result) 
         { 
          case CustomMessageBoxResult.LeftButton: 
           using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) 
           { 
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("favorites.xml", FileMode.Open, file)) 
            { 
             XDocument xDoc = XDocument.Load(stream, LoadOptions.None); 
             // delete node 
             xDoc.Descendants("data").Elements("cocktail").Where(x => x.Value == booze).DescendantsAndSelf().Remove(); 
             xDoc.Save(stream); 

             Favlist.Clear(); 

             list = xDoc.Descendants("cocktail").Select(n => n.Value).ToArray(); 
             Array.Sort(list); 
             alpha = true; 
             foreach (string name in list) 
             { 
              Favlist.Add(new Favs(name)); 
             } 
            } 
           } 
           break; 
          case CustomMessageBoxResult.RightButton: 
           //do nothing 
           break; 
          case CustomMessageBoxResult.None: 
           //do nothing 
           break; 
          default: 
           break; 
         } 
        }; 

        messageBox.Show(); 
       }); 
      } 
      catch (InvalidCastException ICExc) 
      { 
       MessageBox.Show(ICExc.Message); 
      } 
      catch (IOException IOExc) 
      { 
       MessageBox.Show(IOExc.Message); 
      } 
      catch (XmlException XmlExc) 
      { 
       MessageBox.Show(XmlExc.Message); 
      } 
      catch (NullReferenceException NRExc) 
      { 
       MessageBox.Show(NRExc.Message); 
      } 
      catch (Exception myExc) 
      { 
       MessageBox.Show(myExc.Message); 
      } 
     } 
    } 
} 

XAML:

<phone:PhoneApplicationPage 
    x:Class="PhoneApp2.Favorites" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    mc:Ignorable="d" 
    shell:SystemTray.IsVisible="True"> 

    <!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot"> 
     <Grid.Background> 
      <ImageBrush Stretch="Fill" ImageSource="/Assets/AlignmentGrid.png"/> 
     </Grid.Background> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <Grid x:Name="Header" Grid.Row="0" Margin="12,17,0,616" Grid.RowSpan="2"> 
      <TextBlock Text="Cocktail" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0,242,46"/> 
      <TextBlock Text="Favorites" Style="{StaticResource PhoneTextTitle1Style}" Margin="10,50,101,0" FontWeight="Bold"/> 
      <Button x:Name="btnSettings" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="367,60,0,-50" Height="86" Width="91" Click="btnSettings_Click" BorderThickness="0"> 
       <Button.Foreground> 
        <ImageBrush Stretch="Fill"/> 
       </Button.Foreground> 
       <!--<Button.Background> 
        <ImageBrush Stretch="Fill" ImageSource="feature.settings.png"/> 
       </Button.Background>--> 
      </Button> 
     </Grid> 

     <!--ContentPanel - place additional content here--> 
     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,157,12,0"> 
      <phone:LongListSelector x:Name="listFavs" HorizontalAlignment="Left" Height="601" VerticalAlignment="Top" Width="456" Tap="listFavs_Tap" Hold="listFavs_Hold"> 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <Button Content="{Binding Name}" FontSize="36" HorizontalContentAlignment="left" HorizontalAlignment="Left" Height="82" Margin="0,-11,0,0" VerticalAlignment="Top" Width="456" Padding="0,0,0,0" BorderThickness="0"> 
         </Button> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
      </phone:LongListSelector> 
      <ProgressBar x:Name="pbLoading" HorizontalAlignment="Left" Height="20" Margin="127,271,0,0" VerticalAlignment="Top" Width="200" IsIndeterminate="True"/> 
     </Grid> 
    </Grid> 

</phone:PhoneApplicationPage> 

回答

3

也许你的情况element.DataContext是你的自定义类Favs

所以,你应该做以下的铸造:

private void listFavs_Hold(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    try 
    { 
     FrameworkElement element = (FrameworkElement)e.OriginalSource; 
     string item = null; 

     if(element.DataContext is Favs) 
     { 
      Favs itemTmp = (Favs)element.DataContext; 
      item = itemTmp.Name; 
     } 
     else 
     { 
      item = (string)element.DataContext; 
     }  

     .... 
+0

+1,但是当用户不在列表之外时也会导致问题 - 然后事件将以Grid作为FrameworkElement和DataContext = null被触发; – Romasz

0

那是因为你正赶上许多不同FrameworkElements(TextBlock的,边防等),只有TextBlock可以转换为string

private void listFavs_Hold(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    try 
    { 
     FrameworkElement element = (FrameworkElement)e.OriginalSource; 
     if (element is TextBlock) 
     { 
      String item = (String)element.DataContext; 
      var booze = item.ToString(); 
      // rest of code 
     } 
    } 

此代码仅在用户在文本上保留时才有效。如果用户在文本后保留一个空白空间,那么它是一个Border,其DataContext为Favs - 您可以执行合适的强制转换并取出您的变量。