2011-12-05 36 views
0

我正在開發WP7的應用程序,但我碰到了一個我以前沒見過的問題。當我在仿真器中構建並運行它時,它在調試和無調試(ctrl-F5)中都應如此。但在Windows設備上,它只能用於調試(F5)。當我在設備上運行它時沒有進行調試(ctrl-F5),應用程序啓動,但是當我點擊按鈕時,它只是退出應用程序。在按鈕單擊中,我加載一個xml文件。 xml文件設置爲構建操作「內容」,複製到輸出「不要複製」。難道我做錯了什麼?我想知道如果在XAML文件下面的代碼加載beeing有什麼做的錯誤:在模擬器中工作在調試和不調試,但只在設備上調試

<phone:PhoneApplicationPage 
x:Class="appname.NamesList" 
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" 
xmlns:local="clr-namespace:appname" 
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="Portrait" Orientation="Portrait" 
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" 
shell:SystemTray.IsVisible="True"> 

<phone:PhoneApplicationPage.DataContext> 
    <local:MainViewModel /> 
</phone:PhoneApplicationPage.DataContext> 

<Grid x:Name="LayoutRoot" 
     Background="Transparent"> 
    <toolkit:LongListSelector ItemsSource="{Binding Persons}"> 
     <toolkit:LongListSelector.GroupItemsPanel> 
      <ItemsPanelTemplate> 
       <toolkit:WrapPanel Orientation="Horizontal" /> 
      </ItemsPanelTemplate> 
     </toolkit:LongListSelector.GroupItemsPanel> 
     <toolkit:LongListSelector.GroupHeaderTemplate> 
      <DataTemplate> 
       <Border Background="Transparent"> 
        <Border Width="75" 
          Height="75" 
          HorizontalAlignment="Left" 
          Background="{StaticResource PhoneAccentBrush}"> 
         <TextBlock VerticalAlignment="Bottom" 
            Foreground="{StaticResource PhoneForegroundBrush}" 
            Style="{StaticResource PhoneTextExtraLargeStyle}" 
            Text="{Binding Key}" /> 
        </Border> 
       </Border> 
      </DataTemplate> 
     </toolkit:LongListSelector.GroupHeaderTemplate> 
     <toolkit:LongListSelector.GroupItemTemplate> 
      <DataTemplate> 
       <Border Width="75" 
         Height="75" 
         Background="{StaticResource PhoneAccentBrush}" 
         IsHitTestVisible="{Binding HasItems}"> 
        <TextBlock VerticalAlignment="Bottom" 
           Margin="{StaticResource PhoneTouchTargetOverhang}" 
           FontFamily="{StaticResource PhoneFontFamilySemiBold}" 
           FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
           Foreground="{StaticResource PhoneForegroundBrush}" 
           Text="{Binding Key}" /> 
       </Border> 
      </DataTemplate> 
     </toolkit:LongListSelector.GroupItemTemplate> 
     <toolkit:LongListSelector.ItemTemplate> 
      <DataTemplate> 
       <Grid Margin="{StaticResource PhoneTouchTargetOverhang}"> 
        <HyperlinkButton Name="cmdName" 
            Content="{Binding Name}" 
            Click="cmdName_Click" 
            Margin="75,0,0,0" 
            HorizontalAlignment="Left" 
            FontSize="40" /> 
       </Grid> 
      </DataTemplate> 
     </toolkit:LongListSelector.ItemTemplate> 
    </toolkit:LongListSelector> 

</Grid> 

而且MainViewModel:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using appname.Helpers; 

namespace appname 
{ 
    public class MainViewModel 
    { 
     public MainViewModel() 
     { 
      App app = Application.Current as App; 

      XmlHelper xmlHelper = new XmlHelper(); 
      app.persons = xmlHelper.GetPersons(app.gender); 
      List<Person> persons = app.persons; 


      this.Persons = new LongListCollection<Person,char>(persons, person => person.Name[0]); 
     } 

     public LongListCollection<Person, char> Persons 
     { 
      get; 
      private set; 
     } 
    } 
} 

編輯 代碼爲xml文件讀取:

public List<Person> GetPersons(string gender) 
    { 
     string xmlPath = ""; 
     if (gender == "Boys") 
      xmlPath = @"Resources/Boys/Xml/Names.xml"; 
     else if(gender == "Girls") 
      xmlPath = @"Resources/Girls/Xml/Names.xml"; 

     //Uri uri = new Uri(xmlPath, UriKind.Relative); 
     StreamResourceInfo sm = Application.GetResourceStream(new Uri(xmlPath, UriKind.Relative)); 
     System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sm.Stream); 

     XDocument data = XDocument.Load(xr); 



     return (from c in data.Descendants("Person") 
       orderby c.Attribute("Name") 
       select new Person() 
       { 
        Name = c.Element("Name").Value, 
        Description = c.Element("Description").Value, 
        ... 
        HasGraph = c.Element("HasGraph").Value 
       }).ToList(); 
    } 

編輯

<Capabilities> 
    <Capability Name="ID_CAP_GAMERSERVICES"/> 
    <Capability Name="ID_CAP_IDENTITY_DEVICE"/> 
    <Capability Name="ID_CAP_IDENTITY_USER"/> 
    <Capability Name="ID_CAP_LOCATION"/> 
    <Capability Name="ID_CAP_MEDIALIB"/> 
    <Capability Name="ID_CAP_MICROPHONE"/> 
    <Capability Name="ID_CAP_NETWORKING"/> 
    <Capability Name="ID_CAP_PHONEDIALER"/> 
    <Capability Name="ID_CAP_PUSH_NOTIFICATION"/> 
    <Capability Name="ID_CAP_SENSORS"/> 
    <Capability Name="ID_CAP_WEBBROWSERCOMPONENT"/> 
    <Capability Name="ID_CAP_ISV_CAMERA"/> 
    <Capability Name="ID_CAP_CONTACTS"/> 
    <Capability Name="ID_CAP_APPOINTMENTS"/> 
</Capabilities> 

回答

0

天哪

我想我的手機上啓動另一個應用程序,它也退出。所以,我重新啓動了手機(以爲我昨天就這麼做過了),而且它工作正常!我很抱歉浪費你的時間!

問候一個尷尬的傻瓜!

+0

如果你有足夠的聲望,你可以刪除自己的問題。如果可能的話,我建議你這樣做。 –

+0

是的克勞斯,但我必須等待2天才能刪除它。感謝幫助的人! –

0

沒有錯,你每說發佈的代碼。我們需要查看您的XmlHelper課程的代碼,以提供詳細信息。

但是,我懷疑你是以錯誤的方式處理資源管理。另外,我建議你嘗試將代碼包裝在try/catch中,然後使用帶有異常消息的MessageBox.Show。

至少應該確保碰撞是否與異常相關。

public MainViewModel() 
{ 
    try 
    { 
     App app = Application.Current as App; 

     XmlHelper xmlHelper = new XmlHelper(); 
     app.persons = xmlHelper.GetPersons(app.gender); 
     List<Person> persons = app.persons; 


     this.Persons = new LongListCollection<Person,char>(persons, person => person.Name[0]); 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show(e.Message); 
    } 
} 
+0

添加了try catch,但它在應用程序退出時不顯示。還在帖子中添加了xml代碼。 –

+0

然後退出與'MainViewModel'中的任何代碼都沒有關係。在重新部署應用程序之前,是否已將應用程序完全卸載到設備上? –

+0

是的。嘗試它現在奇怪,因爲現在列表加載,當我點擊「A」,然後在「N」(隨機),它退出。下次啓動它時,會在加載列表之前退出。昨天晚上也發生了同樣的事情。奇怪...! –