繁体   English   中英

wp7显示和链接正确的项目

[英]wp7 Displaying and Linking the Correct Item

我试图弄清楚如何仅在新页面上显示特定内容,并且想知道如何检索该数据。 例如,我有一些按钮是根据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));
  }

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

设置新的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;
        }
    }

在此处搜索“隔离存储”,以找到一些使用方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM