如何访问不同页面中的 XAML 对象

如何访问不同页面中的 XAML 对象

问题描述:

我有一个包含 MainPage.xaml 文件的 Windows 10 应用程序.在那个文件中,我保存了基本菜单(hambuger 菜单),并且有这个 Frame 对象,它允许我在菜单上的选择更改时加载新页面:<Frame Name="MyFrame"></Frame>

I have a Windows 10 app that has a MainPage.xaml file. In that file I hold the basic menu (hambuger menu) and there is this Frame object that allows me to load new pages when a selection on the menu changes: <Frame Name="MyFrame"></Frame>

所以当 ListBox 中的选择发生变化时,有一个非常简单的事件处理程序:MyFrame.Navigate(typeof(WeekPage));

So when the selection in the ListBox changes, there is this event handler that is pretty straightforward: MyFrame.Navigate(typeof(WeekPage));

这行代码加载带有另一个 XAML 页面的 Frame.现在我的问题是,在 WeekPage.xaml.cs 内部,我可以控制 MyFrame 对象的导航位置吗?

This line of code loads the Frame with another XAML page. Now my question is, inside of the WeekPage.xaml.cs, can I control where the MyFrame object navigates?

换句话说,我想从另一个 XAML 文件(MainPage.xaml)的代码隐藏中控制 XAML 对象及其属性(按钮、文本块、列表框)代码>WeekPage.xaml.cs).

In other words, I want to control XAML objects and their properties (buttons, textblocks, listbox) on one XAML file (MainPage.xaml) from the code-behind of another XAML file (WeekPage.xaml.cs).

我已经尝试了以下方法:<Frame Name="MyFrame" x:FieldModifier="Public"></Frame> 似乎根本没有帮助和设置使用 MainPage.tried.MyFrame.Navigate(typeof(StoryPage));MainPage.xaml.cs内部静态 NAMESPACE.MainPage 尝试过;WeekPage.xaml.cs(我从 here) 但是当我运行程序时我得到一个 System.NullReferenceException.

I have already tried the following: <Frame Name="MyFrame" x:FieldModifier="Public"></Frame> which didn't seem to help at all and setting up internal static NAMESPACE.MainPage tried; on MainPage.xaml.cs with MainPage.tried.MyFrame.Navigate(typeof(StoryPage)); on WeekPage.xaml.cs(I got that idea from here) but I get a System.NullReferenceException when I run the program.

另一个缩小我要查找内容的示例有一个 MainPage.xaml 包含应用程序的基本布局(汉堡菜单),这取决于用户在汉堡菜单的ListBox"中单击的项目加载新框架,例如:WeekPage.xaml.WeekPage.xaml 有一个 GridView,当点击它时,应该用新的框架替换当前框架,并清除 ListBox 中的任何选择MainPage.xaml.见下面的代码:

Another example to narrow down on what I am looking for There is a MainPage.xaml that contains the basic layout for the app (hamburger menu), depending on the item that the user clicks on in the 'ListBox' in for the hamburger menu a new frame is loaded, for example: WeekPage.xaml. WeekPage.xaml has a GridView that, when clicked, should replace the current frame with a new one and clear out any selections on the ListBox in MainPage.xaml. See code below:

private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
    // Replacing current frame with new frame
    Frame.Navigate(typeof(StoryPage));
    // TODO: I need to clear out any selection in the ListBox item in MainPage.xaml
}

如果您不了解 MVVM 方法,请阅读更多相关内容.在 MVVM 方法中,每个视图都将绑定到一个视图模型.即视图中的数据将绑定到视图模型属性.例如:假设您有一个文本框,您将它的 Text 属性绑定到其相应视图模型中的 string 属性.

If you’re not aware of the MVVM approach, please read more on it. In MVVM approach, each view will be bound to a view model. Ie The data in view will be bound to view model properties. For eg : Say you have a text box, you bind the Text property of it to a string property in its corresponding view model.

在您的场景中,我们可以有一个包含 2 个子视图的 MainView.

In your scenario we can have a MainView consisting of 2 subviews.

MainWindow.xaml - MainWindowViewModel
SubView1.xaml - SubView1VM
SubView2.xaml - SubView2VM

MainView 将由 xaml 中的 2 个子视图和主视图模型中的子视图的 2 个视图模型组成.您还可以在每个子视图模型中使用与按钮点击或任何此类事件相对应的命令.

MainView will consist of 2 sub views in xaml and also 2 view models of subviews in main view model. You can also have commands within each sub viewmodel corresponding to button clicks or any such events.

您可以在 MainWindowViewModel 中处理这些命令.假设在您的视图 2 中,您有一个按钮可以转换到视图 1 以及更改视图 1 中的所有文本框值.处理 MainWindowViewModel 中的按钮命令并从 MainVM 中更改 SubView1VM 属性.

You could handle these commands in the MainWindowViewModel. Suppose in your view 2 you have a button that transits to view 1 as well as change all the textbox values in view 1. Handle the button command in MainWindowViewModel and change the SubView1VM properties from within the MainVM.

希望您了解 MVVM.使用 MVVM 可以轻松实现您的要求.MVVM 对编码人员更友好,有助于将视图数据与模型分离.

Hope you got the idea of MVVM. Your requirement is easily achievable using MVVM. MVVM is more coder friendly and helps to separate the view data from model.

适合任何想要开始使用 MVVM 的人
https://mva.microsoft.com/en-US/training-courses/building-blocks-universal-windows-platform-16064?l=5V1wM0kDC_2606218949