0

First off, I have looked at all the suggested questions and none of them actually answer my question. I see solutions for WinRT or WPF but none that work with UWP.

With that being said, I am looking for a way to do something like this:

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var pageName = ((ListViewItem)((ListView)sender).SelectedItem).Name;
            var pageObj = ???? //Some way to ref MyNamespace.MyPage.xaml
            MyFrame.Navigate(typeof(pageObj));
        }

I have tried to use all combinations of the following:

var pageObj = FindName(pageName);
---
var pageObj = System.Type.GetType(this.GetType().Namespace + "." + pageName);
---
MyFrame.Navigate(typeof(pageObj));
---
MyFrame.Navigate(pageObj);
---
MyFrame.Navigate(typeof(pageName));
---
MyFrame.Navigate(pageName);

And some otherthings I cant quite remember. At best I get a NullReferenceException.

I am fairly new to c# and UWP, so if this is something that is not possible I am sorry for wasting your time. It's just that if I decide to add a new page at some point I don't want to have to edit some switch statement; I just want to add a new ListViewItem and be done with it.

Also, its a learning experience for me. I know how to do switch statements already. Now I would like to do something a little more elegant.

Thank you in advance for any help provided!

master_ruko
  • 629
  • 1
  • 4
  • 22
  • also check if `var pageObj = FindName(pageName);` is not null – Nehorai Elbaz Jun 29 '19 at 09:05
  • Sorry, forgot to mention that ```var pageObj = FindName(pageName);``` was always coming up null. I think it because FindName only finds UI elements in the current page's XAML file. I could be wrong, but thats my guess. – master_ruko Jun 29 '19 at 09:20
  • @master_ruko: What's the exact value of `pageName` at runtime? – mm8 Jul 01 '19 at 11:53

2 Answers2

1

If you just want to get the page type by your listviewitem string value, it's impossible. You would have to manually specify the page type for navigation.

For example, like you said, you could use Switch statements.

Or if you're familiar with WindowsTemplateStudio, you could make an attached property(NavigateTo) and a NavigationService helper class. You could use the attached property on your XAML and get the page type by the attached property value like this:

var pageType = menuItem.GetValue(NavHelper.NavigateToProperty) as Type;

If you do not understand what this code is doing, I suggested that you use the WindowsTemplateStudio to create a simple code project and add some break points to debug its code and understand the whole navigation process.

Xie Steven
  • 8,544
  • 1
  • 9
  • 23
1

I think you were pretty close to solving this issue. Most likely the string containing the type name was not right. You'll want to have the entire namespace including the page name.

To test this out, I created a page called TestPage and this worked for me:

Type type = Type.GetType("Sample.Pages.TestPage");
Frame.Navigate(type);

I recommend trying this without the ListView first to see if you can get it working.

Moshe Gutman
  • 284
  • 4
  • 10
  • I was able to get it to work with using a string literal with the name of the page I want, but that's not what I'm trying to accomplish, and I sorry if this wasn't clear in my op. I want to use a variable, not a string literal. – master_ruko Jul 21 '19 at 05:20
  • You can replace the string literal with a variable. Just make sure the value of the variable matches an existing page type. – Moshe Gutman Jul 22 '19 at 11:38
  • @ Moshe I misunderstood your answer at first. I was able to get it working this working with `NavigationView` and its `Tag` property, but I chose to do it Xavier's way because it made the code more obvious as to the intent. – master_ruko Aug 16 '19 at 01:38
  • Funny story... not really, but I did actually switch to this approach. Xavier's way worked at first, but when I moved my navigation functionality to a service class I started getting null exceptions. From my searching, it looked like I needed to die delegates but I couldn't figure them out because all the examples talked about userControlls, which I'm not using for this. So, in the end, I switched to this approach. – master_ruko Aug 17 '19 at 23:41