Graham
Graham

Reputation: 113

How to transfer information between the main window and an AppWindow in a UWP app?

So I'm developing an app and some items need a properties window to open. I have been following this guide on how to use AppWindow.

What I'm not able to figure out is how to push information between the main window and the properties window. So like when it's first opened, I need to give it all the stored properties to be displayed, but the properties window needs to push any changes back to the main window to be stored and used.

I have very rudimentary code, but I think it demonstrates what I'm doing.

MainPage.xaml.cs:

public sealed partial class MainPage : Page
{

    ...

    public async Task OpenPropertiesWindow()
    {
        //https://learn.microsoft.com/en-us/windows/uwp/design/layout/app-window

        AppWindow properties_appwindow = await AppWindow.TryCreateAsync();
        Frame appWindowContentFrame = new Frame();
        appWindowContentFrame.Navigate(typeof(PropertiesWindow));
        ElementCompositionPreview.SetAppWindowContent(properties_appwindow, appWindowContentFrame);

        properties_appwindow.RequestSize(new Size(300, 400));
        properties_appwindow.Title = "Properties";

        //send data to the textbox in PropertiesWindow

        properties_appwindow.Closed += delegate
        {
            appWindowContentFrame.Content = null;
            properties_appwindow = null;
        };

        await properties_appwindow.TryShowAsync();
    }

PropertiesWindow.xaml:

<Page
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBox x:Name="txtbox_property1" PlaceholderText="property1"/>
            <Button x:Name="btn_apply" Content="Apply" Tapped="ApplyPropertiesButton_Tapped"/>
        </StackPanel>
    </Grid>
</Page>

PropertiesWindow.xaml.cs

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

    private void ApplyPropertiesButton_Tapped(object sender, TappedRoutedEventArgs e)
    {
        string data_from_txtbox = txtbox_property1.Text;

        //push this data_from_txtbox to MainPage
    }
}

Can anyone help me out? I will also need to run another updating method on MainPage whenever a property is changed, so I need some kind of trigger for when data is sent back.

Upvotes: 0

Views: 375

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39072

You can use the Frame you have created after you navigate to get access to the Page instance:

var page = (PropertiesWindow)appWindowContentFrame.Content;
//do something with the page, for example
page.SomePublicMethod(myData);

To go the other way around, you can use the Window API to access the main app window from an AppWindow page:

private void ApplyPropertiesButton_Tapped(object sender, TappedRoutedEventArgs e)
{
    string data_from_txtbox = txtbox_property1.Text;

    var rootFrame = (Frame)Window.Current.Content;
    var page = (MainPage)rootFrame.Content;
    page.SomePublicMethod(data_from_txtbox);
}

Upvotes: 2

Related Questions