windowseventsuwpuwp-xamlsplitview

UWP SplitView Communication Using Events


I have a split view inside of MainPage with its pane and content defined as 's

The pane Frame contains a page which has a PageListView. When the items in the list view are clicked I wish to update the content frame.

In the declaration of the listview page I have a delegate event handler called ItemChangedEventHandler. When the listview item is clicked I call the EventHandler which would then notify all objects subscribed to the event.

My problem is I don't have a reference to the pane Page. Is there a way to instantiate a page and then pass that to the Navigate method? If I could instantiate it before navigating to it then I could reference the PageListView.ItemChanged which wouldn't be null as it is in the code below and add the event handler would be fine. I don't know how to do this. Any suggestions would be great.

PageListView

public delegate void ItemChangedEventHandler(object sender, Item item);

public sealed partial class PageListView : Page
{

private void PageListView_ItemClick(object sender, ItemClickEventArgs e)
    {
        Item item = (Item)e.ClickedItem;
        ItemChanged(this, item);
    }
}

MainPage

public MainPage()
  {
        this.InitializeComponent();

        SplitViewPaneFrame.Navigate(typeof(PageListView));

        PageListView.ItemChanged += new ItemChangedEventHandler(Item_Clicked);
  }

  private void Item_Clicked(Object sender, Item item)
  {
        SplitViewContentFrame.Navigate(typeof(DetailPage), item);
  }

Solution

  • I think a better approach would be to have a view model encapsulate the state (current selection on the split view). Both frames can bind to the same instance of the view model class and thus share the information. This would also work if you had multiple frames that need to be updated according to the selection on the ListView.

    A common case for this pattern is an e-mail reader or RSS reader. See this example for a reference implementation: https://github.com/Microsoft/Windows-appsample-rssreader/tree/master/RssReader