开发者

Setting String Values Across Pages in WP7 App

开发者 https://www.devze.com 2023-01-31 10:59 出处:网络
I\'ll try my best to explain this without speaking too much about the specific content and purpose of the app.I\'ll just say on the main page there are 3 empty \"slots\" for items you can select to oc

I'll try my best to explain this without speaking too much about the specific content and purpose of the app. I'll just say on the main page there are 3 empty "slots" for items you can select to occupy them. When you click on one, it takes you to a separate page that let's you select a specific item for that slot. Here is the code to better explain:

  <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" 
                   />

Here is the code behind for this XAML page:

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

On the selection pages an XML file is looped through and saved into new instances of a class. When the user selects a certain option from that list with a button press, I want to set the corresponding slot on the main page equal to that selection. Here is an example of the first slots selection page:

<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>                        
                </D开发者_开发百科ataTemplate>
            </ListBox.ItemTemplate>
  </ListBox>

And finally the code behind for the selectin page:

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
    }

See the comment line in the click event for my main problem. How to I set these values equal to eachother across these pages? I know it may look like a mess here so I appreciate the help.


TBH I'm not sure what you want to do. What fires setSelectedToFirst?

Anyways, to save some data and read it during app's lifycycle you can use IsolatedStorageSettings.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号