如何在 Windows 8 样式应用程序中将对象从框架传递到另一个框架框架、中将、应用程序、样式

2023-09-06 06:38:43 作者:爱你到走投无路

我有一个问题,我现在无法弄清楚.我正在尝试开发一个 Windows-8 风格的应用程序,但我坚持实现此功能.

I have problem that i just cant figure out right now. I am trying to develop a Windows-8 style app and im stuck implementing this functionality.

我有一个 MainWindow,其中包含一个 ListBox 和一个按钮(比如说 addButton).

I have a MainWindow which contains a ListBox and a Button (lets say addButton).

当我点击按钮时,我导航到一个新页面,让我们说 AddCustomerPage with this.Frame.Navigate(typeof (AddCustomerPage));

When i click the button i navigate to a new page, lets say AddCustomerPage with this.Frame.Navigate(typeof (AddCustomerPage));

AddCustomerPage 有 1 个文本框和 1 个按钮(可以说 doneButton.当我单击按钮时,我希望将文本框中的字符串添加到上一个列表框中页面.

AddCustomerPage has 1 textBox and 1 button (lets say doneButton. When i click the button i want the string in the textBox to be added to the ListBox on the previous page.

这是我目前的功能:1. 创建主窗口.

This is my current functionality: 1. MainWindow is created.

点击添加按钮

Click addButton

AddCustomer 页面已创建.MainWindow 被破坏(问题).

AddCustomer page is created. MainWindow is destroyed(problem).

点击完成按钮

一个 MainWindow 对象是用一个有 1 个项目的 ListBox 创建的.

A MainWindow object is created with a ListBox with 1 item.

重复添加过程,我总是得到一个带有 1 个项目的 ListBox 的 MainWindow.

Repeat the add process, i always get a MainWindow with a ListBox with 1 item.

感谢您的帮助.代码如下:

Thanks for the help. Here is the code:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        this.brainPageController = new PageController();

        // add items from the List<String> to the listBox
        listGoals.ItemsSource = brainPageController.GetListGoals();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var parameter = e.Parameter as String;
        // a simple controller that adds a string to a List<string>
        brainPageController.AddGoal(parameter);
    }

    private void addButton_Click(object sender, RoutedEventArgs e)
    {
        this.Frame.Navigate(typeof (GoalsInfo));
    }

    // VARIABLES DECLARATION
    private PageController brainPageController;
}

public sealed partial class GoalsInfo : WinGoalsWIP.Common.LayoutAwarePage
{
    public GoalsInfo()
    {
        this.InitializeComponent();
        this.brainPageController = new PageController();
    }

    protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
    {
    }
    protected override void SaveState(Dictionary<String, Object> pageState)
    {
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        brainPageController.AddGoal(nameTextBox.Text);

        this.Frame.Navigate(typeof(MainPage), nameTextBox.Text);
    }
    // VARIABLES DECLARATION
    PageController brainPageController;
}

推荐答案

试试这个希望对你有帮助

Try this i hope it helps

快速入门:在页面之间导航