在WP7应用程序中设置跨页面的字符串值

时间:2010-12-18 22:47:58

标签: c# silverlight visual-studio-2010 windows-phone-7

我会尽量解释这一点,而不要过多地谈论应用程序的具体内容和目的。我只想在主页上说有3个空的“插槽”,您可以选择占用它们。当您单击其中一个时,它会将您带到一个单独的页面,让您选择该插槽的特定项目。以下是更好地解释的代码:

  <TextBlock Text="{Binding FirstSelectionName}" />
  <TextBlock Text="{Binding FirstSelectionType}" />
  <HyperlinkButton Content="Choose First Option" 
                   Name="firstHyperLink" 
                   NavigateUri="/Pages/FirstChoices.xaml" 
                   />

  <TextBlock Text="{Binding SecondSelectionName}" />
  <TextBlock Text="{Binding SecondSelectionType}" />
  <HyperlinkButton Content="Choose Second Option" 
                   Name="secondHyperLink" 
                   NavigateUri="/Pages/SecondChoices.xaml" 
                   />

  <TextBlock Text="{Binding ThirdSelectionName}" />
  <TextBlock Text="{Binding ThirdSelectionType}" />
  <HyperlinkButton Content="Choose Third Option" 
                   Name="thirdHyperLink" 
                   NavigateUri="/Pages/ThirdChoices.xaml" 
                   />

以下是此XAML页面背后的代码:

 public class FirstSelection
    {
        public string FirstSelectionName { get; set; }
        public string FirstSelectionType { get; set; }            
    }

 public class SecondSelection
    {
        public string SecondSelectionName { get; set; }
        public string SecondSelectionType { get; set; }            
    }

 public class ThirdSelection
    {
        public string ThirdSelectionName { get; set; }
        public string ThirdSelectionType { get; set; }            
    }

在选择页面上,XML文件循环并保存到类的新实例中。当用户通过按下按钮从该列表中选择某个选项时,我想将主页面上的相应插槽设置为等于该选择。以下是第一个插槽选择页面的示例:

<ListBox Name="firstOptionsList">                
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button BorderThickness="3"
                            Click="setSelectedToFirst">                            
                        <StackPanel Orientation="Vertical">
                            <TextBlock Name="nameTextBlock" 
                                       Text="{Binding Name}" 
                                       />
                            <TextBlock Name="typeTextBlock" 
                                       Text="{Binding Type}" 
                                       /> 
                        </StackPanel>
                    </Button>                        
                </DataTemplate>
            </ListBox.ItemTemplate>
  </ListBox>

最后选择页面背后的代码:

public FirstOptionsPage()
    {
        InitializeComponent();            

        Dispatcher.BeginInvoke((Action)(() => firstList.ItemsSource = firstdata));

        WebClient firstWebClient = new WebClient();

        firstWebClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(first_DownloadStringCompleted);
        firstWebClient.DownloadStringAsync(new Uri("http://www.website.com/firstoptions.xml"));
    }


    void first_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        XElement xmlitem = XElement.Parse(e.Result);

        var firstdata = new List<FirstOptionsClass>();

        foreach (XElement item in xmlitem.Elements("entry"))
        {
            var name = item.Element("name");
            var namevalue = (name == null) ? null : name.Value;
            var type = item.Element("type");
            var typevalue = (type == null) ? null : type.Value;              

            firstdata.Add
                (new FirstOptionsClass
                    {
                        Name = namevalue,
                        Type = typevalue,                           
                    }
                );
        }

        firstList.ItemsSource = firstdata;
    }

    public class FirstOptionsClass
    {
        public string Name { get; set; }
        public string Type { get; set; }            
    }

    public System.Collections.IEnumerable firstdata { get; set; }

    private void setSelectedToFirst(object sender, RoutedEventArgs e)
    {
        //THIS IS THE PART WHERE I'M NOT SURE HOW TO SET FirstOptionsClass Name = FirstSelectionName on the MainPage.xaml
    }

在主要问题的点击事件中查看评论行。如何在这些页面中将这些值设置为相等?我知道这可能看起来像一团糟,所以我很感激帮助。

1 个答案:

答案 0 :(得分:0)

TBH我不确定你想做什么。什么火灾setSelectedToFirst?

无论如何,要保存一些数据并在应用程序的lifycycle期间阅读,您可以使用IsolatedStorageSettings