c# - 正确地在页面之间传递数据

标签 c# windows-phone-7 xaml

我一直在尝试将单个 WP7 应用程序页面分成两个单独的页面,以便我可以将功能保留在一个页面中,将 View 保留在另一个页面中。基本上,我正在创建一个选项卡式网络浏览器,其中有不同的选项卡,可以根据用户的请求查看这些选项卡,并且只有当前单击的选项卡才会显示在 View 中。到目前为止,我有一个在一个页面中工作的解决方案,但我想将其分为 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 上查看的当前网络浏览器实例)。

最佳答案

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

(App.Current as App).SomeField

现在我对您正在使用的网络浏览器控件没有任何经验,但我假设您可以使用 MVVM 模式将数据与 View 分离。 This就是一个很好的例子。使用 MVVM 还可以让您better support tombstoning与您的申请。希望这能回答您的问题。

关于c# - 正确地在页面之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096500/

相关文章:

c# - 基于 C# 中搜索文本框的文本将 ListView CollectionViewSource 滚动到一组项目

c# - MVC 将 PartialViewResult 渲染为字符串

c# - 将变量值从 VB6 转换为 C# 以使用 Win32

c# - 索引超出范围且数据少于 6 行

windows-phone-7 - Windows Phone 8 中的双指缩放功能

c# - 将 ControlTemplate XAML 转换为 C#

javascript - 使用 Node js 和 electron 上传图片。 (从 C# 代码转换)

c# - System.dll 中的 System.ServiceModel.FaultException

c# - 在内存、IsoStorage 和服务器之间同步

c# - 数据绑定(bind)到实例方法