0
votes

So I have tried following UWP Accessing Frame for page navigation through Usercontrol an Object? But I get a nullpointer.

I have a mainpage with a splitview, frame and a hamburger menu. From here I control the pages that are loaded into the frame.
I also have a profile view and i want to invoke a new view when the user clicks a button. Let me add the code:

MainPage:

public sealed partial class MainPage : Page

{

    Profile profile = new Profile();

    public MainPage()
    {
        this.InitializeComponent();
        MyFrame.Navigate(typeof(Financial));
        BackButton.Visibility = Visibility.Collapsed;
        Title.Margin = new Thickness(68,0,0,0);

        profile.OnNavigateParentReady += OnCreateUser;

    }

....



    public void OnCreateUser(object sender, RoutedEventArgs e)
    {
        if (MySplitView.Content != null)
            ((Frame)MySplitView.Content).Navigate(typeof(CreateUser));
        Title.Text = "Create User";
        BackButton.Visibility = Visibility.Visible;
        Title.Margin = new Thickness(0, 0, 0, 0);

    }

}

And profile:

public sealed partial class Profile : Page

{

    public delegate void MyEventHandler(object source, RoutedEventArgs e);

    public event MyEventHandler OnNavigateParentReady;


    private string _profileName;
    private string _password;

    private Dictionary<string, string> usersDictionary = new Dictionary<string, string>();

    public Profile()
    {
        this.InitializeComponent();
        usersDictionary.Add("Casper", "12345");
    }

    private void Login_Click(object sender, RoutedEventArgs e)
    {
        _profileName = ProfileName.Text;
        _password = PasswordBox.Password;

        if (usersDictionary.ContainsKey(_profileName))
        {
            if (usersDictionary[_profileName] == _password)
            {
                ProfileName.Text = "LOGIN SUCCES!";
            }
        }
        else
        {

        }
    }

    private void CreateUser_Click(object sender, RoutedEventArgs e)
    {
        OnNavigateParentReady(sender, e);
    }
}

I can change the frame with no problem using Frame.navigate but I want to edit the title as well as margin and all other OnCreateUser does. How do I go about that?

EDIT: I should say that i get a nullpointer on this line: OnNavigateParentReady(sender, e);

1

1 Answers

3
votes

That null reference exception is thrown because the OnNavigateParentReady event has no listeners yet, when the Click event is invoked on your CreateUser control. You should try the following:

if (OnNavigateParentReady != null)
{
    OnNavigateParentReady(sender, e);
}

Also, the profile object that you create inside the MainPage class - where is it used? Is it ever shown anywhere? It seems as if that object is not the one showing up in your app. Instead, some other instance of Profile is being used!

The profile member field of your MainPage class is probably not the Page actually showing up when you navigate. Try setting an event listener on the Profile page object actually being displayed.