【问题标题】:passing data between pages correctly在页面之间正确传递数据
【发布时间】:2013-05-11 02:12:17
【问题描述】:

我一直在尝试将单个 WP7 应用程序页面分成两个单独的页面,以便我可以将功能保留在一个页面中,而将视图保留在另一个页面中。基本上我正在创建一个选项卡式网络浏览器,其中我有不同的选项卡,可以根据用户的请求查看,并且只有当前单击的选项卡是视图中显示的选项卡。到目前为止,我有一个解决方案,它可以在一个页面中工作,但我想将它分成一个 TabsPage(它将控制选项卡实例)和一个 MainPage(它将向用户展示当前选择的选项卡以供显示)。我不确定如何正确分隔这两个页面,以便我可以完成此功能。我所拥有的如下(到目前为止有效)

MainPage.xaml

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <TextBox x:Name="UrlTextBox" KeyDown="UrlTextBox_KeyDown" InputScope="url"/>
    <Grid x:Name="BrowserHost" Grid.Row="1" />
    <!--<my:FullWebBrowser Name="TheBrowser" Grid.Row="1"/>-->

</Grid>

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar>
        <shell:ApplicationBarIconButton x:Name="Tabs" IconUri="/Images/appbar.tab.rest.png" Text="tabs" Click="TabsPage_Click"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar.next.rest.png" IsEnabled="True" Text="forward" x:Name="ForwardBtn" Click="ForwardBtn_Click"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="1" Click="TabMenuItem_Click" />
            <shell:ApplicationBarMenuItem Text="2" Click="TabMenuItem_Click" />
            <shell:ApplicationBarMenuItem Text="3" Click="TabMenuItem_Click" />
            <shell:ApplicationBarMenuItem Text="4" Click="TabMenuItem_Click" />
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

MainPage.xaml.cs

private const int NumTabs = 4;
    private int currentIndex;

    private string[] urls = new string[NumTabs]; 
    ////private WebBrowser[] browsers = new WebBrowser[NumTabs]; 
    private FullWebBrowser[] browsers = new FullWebBrowser[NumTabs];

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        ShowTab(0); 
    }

    //protected override void OnNavigatedTo(NavigationEventArgs e)
    //{
    //    base.OnNavigatedTo(e);

    //}

    private void ShowTab(int index)
    {
        this.currentIndex = index;

        UrlTextBox.Text = this.urls[this.currentIndex] ?? "";

        if (this.browsers[this.currentIndex] == null)
        {
            //WebBrowser browser = new WebBrowser(); 
            FullWebBrowser browser = new FullWebBrowser();

            this.browsers[this.currentIndex] = browser;

            BrowserHost.Children.Add(browser);
        }

        for (int i = 0; i < NumTabs; i++)
        {
            if (this.browsers[i] != null)
            {
                this.browsers[i].Visibility = i == this.currentIndex ? Visibility.Visible : Visibility.Collapsed;
            }
        }
    }

    private void UrlTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            Uri url;

            //string _url;

            if (Uri.TryCreate(UrlTextBox.Text, UriKind.Absolute, out url))
            {
                this.urls[this.currentIndex] = UrlTextBox.Text;
                //_url = url.ToString();

                string _url = url.ToString();

                //this.browsers[this.currentIndex].Navigate(url); 
                this.browsers[this.currentIndex].Navigate(_url);
            }
            else
                MessageBox.Show("Invalid url");
        }
    }

    private void TabMenuItem_Click(object sender, EventArgs e)
    {
        int index = Int32.Parse(((ApplicationBarMenuItem)sender).Text) - 1;
        ShowTab(index);
    }

    //**********************************************************************

    private void TabsPage_Click(object sender, EventArgs e)
    {
        this.NavigationService.Navigate(new Uri("/TabsPage.xaml", UriKind.Relative));
    }

注意:我正在使用一个名为 TheWebBrowser 的网络浏览器控件。 任何有关如何正确将 MainPage 分离为 TabsPage 的帮助将不胜感激!我已经为此工作了一段时间,似乎无法在页面之间正确传递数据(即搜索栏和要在 MainPage 上查看的当前 webbrowser 实例)。

【问题讨论】:

    标签: c# windows-phone-7 xaml


    【解决方案1】:

    可以通过在 App.xaml.cs 文件中添加一个保存点来完成在页面之间传递值并仍然遵守正确的 WP7 设计,然后您可以使用访问信息

    (App.Current as App).SomeField
    

    现在我对您使用的 webbrowser 控件没有经验,但我假设您可以使用 MVVM 模式将数据与视图分开。 This 就是一个很好的例子。使用 MVVM 还允许您使用您的应用程序 better support tombstoning。希望这能回答您的问题。

    【讨论】:

    • 感谢@singelabs,你能告诉我如何使用它吗,我以前从未将 App.Current 用作 App 吗?另外,是否有一种更简单的方法,只需在两个页面之间通过查询字符串传递数据来完成此操作?
    【解决方案2】:

    我只会使用 Caliburn Micro 在页面/屏幕之间传递数据。 http://caliburnmicro.codeplex.com/ 它还有一个很好的文档供您开始学习如何正确传递数据而不是做 (App.Current as App).SomeField。

    【讨论】:

      【解决方案3】:

      以防万一,this can be helpful

      编辑:我的意思是,你可以使用this component,它可以通过 Nuget 获得

      Install-Package SmartNavigation. 
      

      可能有点原始,但完全值得一看。

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
      • @LittleBobbyTables 罗杰罗杰 :)
      【解决方案4】:

      例如,当我想跨页面共享数据时,我就是这样做的。在 App.xaml.cs 中定义你的属性(别忘了在你需要的地方初始化):

      public List<Contact> Contacts
      {
          get
          {
              return _contacts;
          }
      }
      

      现在在您要使用此数据的页面中,像这样定义它:

      private List<Contact> Contacts
      {
          get
          {
              return (App.Current as App).Contacts;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-06
        • 2016-11-10
        相关资源
        最近更新 更多