0

我试图弄清楚如何在新页面上仅显示特定内容,我想知道如何检索该数据。例如,我有从 xml 表中的解析数据生成的按钮,当我单击按钮时,我希望按钮指向我创建的新 xaml 页面,并在新 xaml 页面上显示与该按钮关联的数据.

首先,我将链接一些我用来从我的 xml 页面存储数据的代码。

   `public int countElements = 0;
    public MainViewModel()
    {
        this.Items = new ObservableCollection<ItemViewModel>();
    }

    public ObservableCollection<ItemViewModel> Items { get; private set; }


    public void LoadData()
    {

        var elements = from p in unmXdoc.Descendants(dataNamspace +      "vevent").Elements(dataNamspace + "properties")
                       select new ItemViewModel
                       {
                           summary = this.GetElementValue(p, "summary"),
                           description = this.GetElementValue(p, "description"),
                           categories = this.GetElementValue(p, "dtstamp"),
                       };

        foreach (var element in elements)
        {
            this.Items.Add(new ItemViewModel()
            {
                LineOne = element.summary,
                LineTwo = element.categories,
                LineThree = element.description
            });
            countElements++;
        }

        this.IsDataLoaded = true;`

所以 LineOne 是我的按钮的名称,当我单击按钮时,我希望将 LineTwo 和 LineThree 加载到我命名为 LineThreePage.xaml 的 xaml 页面上。我将链接现在正在生成按钮的 xaml 代码。

<Grid x:Name="LayoutRoot" Background="Transparent" >
    <!--Pivot Control-->
    <controls:Pivot Title="" Margin="0,64,0,-63">
        <!--Pivot item one-->
        <controls:PivotItem>
            <!-- Header="Events"-->
            <controls:PivotItem.Header>
                <TextBlock Text="Events" FontSize="48" ></TextBlock>
            </controls:PivotItem.Header>
            <!--Double line list with text wrapping-->
            <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                            <Button Margin="8,0,10,0" 
                                    Padding="0,0" 
                                    HorizontalContentAlignment="Left"
                                    BorderThickness="0.8"
                                    BorderBrush="Gray"
                                    Background="White"
                                    Width="420" 
                                    Click="Button_Click">
                                <TextBlock Text="{Binding LineOne}" 
                                           TextWrapping="Wrap"
                                           Foreground="#8f1020"
                                           Style="{StaticResource   PhoneTextNormalStyle}"/>
                            </Button>
TextWrapping="Wrap" Margin="12,-10,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PivotItem>'

所以基本上当我单击 button1 时,我想导航到我的 lineThreePage.xaml 并查看与该页面上的 LineOne 关联的 LineTwo 和 LineThree。

最后我在下面有我的按钮点击代码!

  private void Button_Click(object sender, RoutedEventArgs e)
  {
      this.NavigationService.Navigate(new Uri("/lineThreePage.xaml",   UriKind.Relative));
  }
4

1 回答 1

0

本质上,您正在寻找的是一种在应用程序中保留“状态”的方法。有几种方法可以做到这一点,其中一些是 App.Current.ApplicationLifetimeObjects 和独立存储。

当我设置一个新的 WP 项目时,我做的第一件事就是整理一个我用来在应用程序中保存状态的服务。假设上面代码中的 FirstListBox 绑定到“ItemViewModel”类型的实体。

1)设置一个通用的隔离存储类服务....请记住,您可以根据您的要求对其进行调整,我做了一些假设,例如当在此代码中找不到值时返回空值,

public class IsolateStorageStore
{
    /// <summary>
    /// The iosolated settings store.
    /// </summary>
    private readonly IsolatedStorageSettings isolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings;

    public T ReadValue<T>(string key)
    {
        return isolatedStorageSettings.Contains(key) ? (T)isolatedStorageSettings[key] : default(T);
    }

    public void WriteValue<T>(string key, T value)
    {
        if (isolatedStorageSettings.Contains(key))
        {
            isolatedStorageSettings[key] = value;
        }
        else
        {
            isolatedStorageSettings.Add(key, value);
        }
    }
}

2)设置读取/写入存储值的机制

public static class IsolatedStorageManager
{
    private static IsolateStorageStore IsolateStorageStore = new IsolateStorageStore();

    public static ItemViewModel FeedItemViewModel
    {
        get
        {
            return IsolateStorageStore.ReadValue<ItemViewModel>("ItemFeedsKey");
        }
        set
        {
            IsolateStorageStore.WriteValue("ItemFeedsKey", value);
        }
    }

    public static object AnotherItem
    {
        get
        {
            return IsolateStorageStore.ReadValue<object>("AnotherItemKey");
        }
        set
        {
            IsolateStorageStore.WriteValue("AnotherItemKey", value);
        }
    }
}

现在您已经有效地为读取/写入对象以存储的服务,调整您的代码以使用它。

private void Button_Click(object sender, RoutedEventArgs e)
    {
        var button = (sender as Button);

        if (button != null)
        {
            var data = button.DataContext as ItemViewModel;

            if (data != null)
            {
                //Save to isolated storage
                IsolatedStorageManager.FeedItemViewModel = data;

                //redirect to next Page.
                this.NavigationService.Navigate(new Uri("/lineThreePage.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("An error occured, either the sender is not a button or the data context is not of type ItemViewModel");
            }
        }
        else
        {
            MessageBox.Show("An error occured, either the sender is not a button or the data context is not of type ItemViewModel");
        }
    }

最后在您的 lineThreePage.xaml 页面上,您需要读取已存储在隔离存储中的值

 public lineThreePage()
    {
        InitializeComponent();

        BindData();
    }


    private void BindData()
    {
        var data = IsolatedStorageManager.FeedItemViewModel;

        if (data != null)
        {
            //Bind the data to a text box in your xaml named "txtDescription"
            txtDescription.Text = data.LineTwo;
        }
    }

在此处搜索隔离存储以查找有关如何使用它的几个资源。

于 2013-11-07T13:00:30.587 回答