Linked Questions

Popular Questions

UWP: Passing data between MainPage.cs and Views\Page.cs

Asked by At

I have a MainPage that has the code for my NavigationView. The MainPage navigates to pages with a a frame.navigate(). One of the pages contains text boxes for user entered data. I would like to take that entered data and use it to change what is displayed on another page.

I've looked at a lot of similar posts on here about passing data from one view to another, and they all use frame.navigate(typeof(page),parameter). But my parameters are not in the MainPage, where I have my navigation controls.

MainPage.xaml

<Page>
<Grid>
    <NavigationView x:Name="nvTopLevelNav">
        <NavigationView.MenuItems>
            <NavigationViewItem Icon="Setting" Content="Page1" Tag="Page1" />
            <NavigationViewItem Icon="Rotate" Content="Page2" Tag="Page2" />
            </NavigationView.MenuItems>
        <Frame x:Name="contentFrame"></Frame>
    </NavigationView>
</Grid>
</Page>

MainPage.xaml.cs:

namespace App
{
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }
    #region NavigationView event handlers
    private void nvTopLevelNav_Loaded(object sender, RoutedEventArgs e)
    {        
        // set the initial SelectedItem
        foreach (NavigationViewItemBase item in nvTopLevelNav.MenuItems)
        {
            if (item is NavigationViewItem && item.Tag.ToString()=="Page1")
            {
                nvTopLevelNav.SelectedItem = item;
                break;
            }
        }
    }
    private void nvTopLevelNav_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
    {
        Windows.UI.Xaml.Controls.NavigationViewItem Item = args.SelectedItem as Windows.UI.Xaml.Controls.NavigationViewItem;

        if (Item.Tag is string ItemTag)
        {
            switch (ItemTag)
            {
                case "Page1":
                    contentFrame.Navigate(typeof(SettingsPage));
                    sender.Header = "Page1";
                    break;

                case "Page2":
                    contentFrame.Navigate(typeof(Page2));
                    sender.Header = "Page2";
                    break;
            }
        }
    }
}
}

Page1.xaml

<Page>
<Grid>
    <Button Name="OutputFolderButton" Click="OutputFolderButtonClick">
        <Image  x:Name="FileButton" Source="Assets/FileButton.png"/>
    </Button>
    <ToggleSwitch Name="ToggleSwitch" Toggled="SwitchToggled"/>
    <TextBox Name="TextBox1" TextChanged="TextBox1Changed"/>
    <TextBox Name="TextBox2" TextChanged="TextBox2Changed"/>
    <TextBox Name="TextBox3" TextChanged="TextBox3Changed"/>
    <TextBox Name="TextBox4" TextChanged="TextBox4Changed"/>
</Grid>
</Page>

Page1.xaml.cs:

namespace App
{
public sealed partial class SettingsPage : Page
{
    public SettingsPage()
    {
        InitializeComponent();
    }

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

    }
    public async void OutputFolderButtonClick(object sender, RoutedEventArgs e)
    {
        var picker = new Windows.Storage.Pickers.FolderPicker
        {
            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder
        };
        picker.FileTypeFilter.Add("*");

        Windows.Storage.StorageFolder outputFolder = await picker.PickSingleFolderAsync();
        if (outputFolder != null)
        {
            OutputFolderTextBox.Text = outputFolder.Path;
            OutputFolderTextBox.FontStyle = Windows.UI.Text.FontStyle.Normal;
        }
    }
    private void SwitchToggled(object sender, RoutedEventArgs e)
    {
    }
    private void TextBox1Changed(object sender, TextChangedEventArgs e)
    {
    }
    private void TextBox2Changed(object sender, TextChangedEventArgs e)
    {
    }
    private void TextBox3Changed(object sender, TextChangedEventArgs e)
    {
    }
    private void TextBox4Changed(object sender, TextChangedEventArgs e)
    {
    }
}
}

I would like the TextChanged events pass the data to the MainPage so that I can have all of the date available in one place. With all the data in the MainPage, then I can pass it to the other pages as they are called.

Related Questions