我正在嘗試制作 UWP 應用。
我決定添加一個NavigationView物件,并創建了一些部分。我閱讀了檔案,但我沒有找到如何繪制頁面內容。
有一個Content屬性,但它在選單中設定頁面標簽的內容,而不是像我試圖做的那樣設定單個標簽的頁面內容。
我該怎么做?感謝所有會回答的人。
uj5u.com熱心網友回復:
更新:
對于設定頁面,如果您NavigationView通過啟用IsSettingsVisible 屬性使用 的默認項,您可以檢查 的IsSettingsInvoked值NavigationViewItemInvokedEventArgs。
這是您可以參考的代碼
void App3::MainPage::nvSample_ItemInvoked(Windows::UI::Xaml::Controls::NavigationView^ sender, Windows::UI::Xaml::Controls::NavigationViewItemInvokedEventArgs^ args)
{
if (args->IsSettingsInvoked == true)
{
contentFrame->Navigate(Windows::UI::Xaml::Interop::TypeName(SettingPage::typeid));
}
else
{
auto navItemTag = args->InvokedItemContainer->Tag->ToString();
if (navItemTag != nullptr)
{
// navigation logic here. You could use switch or other condition. This if is just a example.
if (navItemTag->Equals("SamplePage1"))
{
contentFrame->Navigate(Windows::UI::Xaml::Interop::TypeName(SamplePage1::typeid));
}
}
}
}
如果我理解正確的話,你想顯示Page的NavigationView,對不對?一般我們會在內容中添加一個Frame物件NavigationView。Frame Object可以根據需要顯示頁面。
這是一個非常簡單的例子NavigationView:
<NavigationView x:Name="nvSample" ItemInvoked="nvSample_ItemInvoked" Loaded="nvSample_Loaded">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Play" Content="Menu Item1" Tag="SamplePage1" />
<NavigationViewItem Icon="Save" Content="Menu Item2" Tag="SamplePage2" />
<NavigationViewItem Icon="Refresh" Content="Menu Item3" Tag="SamplePage3" />
<NavigationViewItem Icon="Download" Content="Menu Item4" Tag="SamplePage4" />
</NavigationView.MenuItems>
<Frame x:Name="contentFrame"/>
</NavigationView>
在代碼隱藏中,您需要處理NavigationView.ItemInvoked 事件來控制導航。像下面的代碼:
void App3::MainPage::nvSample_ItemInvoked(Windows::UI::Xaml::Controls::NavigationView^ sender, Windows::UI::Xaml::Controls::NavigationViewItemInvokedEventArgs^ args)
{
auto navItemTag = args->InvokedItemContainer->Tag->ToString();
if (navItemTag != nullptr)
{
// navigation logic here. You could use switch or other condition. This if is just a example.
if (navItemTag->Equals("SamplePage1"))
{
contentFrame->Navigate(Windows::UI::Xaml::Interop::TypeName(SamplePage1::typeid));
}
}
}
void App3::MainPage::nvSample_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
//This is the HomePage
contentFrame->Navigate(Windows::UI::Xaml::Interop::TypeName(HomePage::typeid));
}
This is a very simple c /cx demo. You could get more information about the NavigationView Document:NavigationView. Or check it in the XAML Controls Gallery app.
You could also check the NavigationView Source code here: Get the source code (GitHub).
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/358457.html
