从另一个 class 更改框架内的页面
Change page inside a frame from another class
所以我尝试关注
但是我得到一个空指针。
我有一个带有拆分视图、框架和汉堡菜单的主页。从这里我控制加载到框架中的页面。
我还有一个配置文件视图,我想在用户单击按钮时调用一个新视图。让我添加代码:
主页:
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);
}
}
个人资料:
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);
}
}
我可以使用 Frame.navigate
毫无问题地更改框架,但我想编辑标题和边距以及所有其他 OnCreateUser
。我该怎么做?
编辑:我应该说我在这一行得到了一个空指针:OnNavigateParentReady(sender, e);
在 CreateUser
控件上调用 Click
事件时,由于 OnNavigateParentReady
事件还没有侦听器,因此抛出了空引用异常。您应该尝试以下操作:
if (OnNavigateParentReady != null)
{
OnNavigateParentReady(sender, e);
}
此外,您在 MainPage
class 中创建的 profile
对象 - 它在哪里使用?它曾经在任何地方显示过吗?似乎该对象不是出现在您的应用程序中的对象。相反,正在使用 Profile
的其他一些实例!
您的 MainPage
class 的 profile
成员字段可能不是您导航时实际显示的 Page
。尝试在实际显示的 Profile
页面对象上设置事件侦听器。
所以我尝试关注
我有一个带有拆分视图、框架和汉堡菜单的主页。从这里我控制加载到框架中的页面。
我还有一个配置文件视图,我想在用户单击按钮时调用一个新视图。让我添加代码:
主页:
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);
}
}
个人资料:
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);
}
}
我可以使用 Frame.navigate
毫无问题地更改框架,但我想编辑标题和边距以及所有其他 OnCreateUser
。我该怎么做?
编辑:我应该说我在这一行得到了一个空指针:OnNavigateParentReady(sender, e);
在 CreateUser
控件上调用 Click
事件时,由于 OnNavigateParentReady
事件还没有侦听器,因此抛出了空引用异常。您应该尝试以下操作:
if (OnNavigateParentReady != null)
{
OnNavigateParentReady(sender, e);
}
此外,您在 MainPage
class 中创建的 profile
对象 - 它在哪里使用?它曾经在任何地方显示过吗?似乎该对象不是出现在您的应用程序中的对象。相反,正在使用 Profile
的其他一些实例!
您的 MainPage
class 的 profile
成员字段可能不是您导航时实际显示的 Page
。尝试在实际显示的 Profile
页面对象上设置事件侦听器。