How to navigate to a page in NavigationView in WinUI3 using C++ - c++

I am working on very simple demo of NavigationView in WinUI3 using C++.
It is very unfortunate that there is no material or tutorial available for this.
<Window
x:Class="App1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<NavigationView x:Name="NavView"
IsTitleBarAutoPaddingEnabled="False"
IsTabStop="False" IsBackButtonVisible="Collapsed" PaneDisplayMode="Left">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Page2" Content="Tab1" Tag="Tab1" IsSelected="True"/>
<NavigationViewItem Icon="Page2" Content="Tab2" Tag="Tab2"/>
</NavigationView.MenuItems>
<ScrollViewer>
<Frame x:Name="ContentFrame" Padding="12,0,12,24" IsTabStop="True"/>
</ScrollViewer>
</NavigationView>
</Window>
How to navigate to a Page on NavigationView using C++?
ContentFrame().Navigate(**WHAT CODE GOES HERE**);
I already know how to do it using C#. and I am not using UWP with c++/winrt.
I need solution for WinUI3 using c++

Check out the C++/WinRT sample code in the docs for NavigationView on MSDN (you can search for void MainPage::NavView_Navigate inside the page to quickly find it).
Here is a summary:
Windows::UI::Xaml::Interop::TypeName pageTypeName =
winrt::xaml_typename<NavigationViewCppWinRT::SettingsPage>();
ContentFrame().Navigate(pageTypeName, nullptr, transitionInfo);
// transitionInfo is passed to your event handler
// if you do this without a transition info, simply
// omit the last argument.

Related

How can I get an element from within a ListViewItem's ItemTemplate?

I have a ListView that uses a custom ItemTemplate (doesn't everyone?):
<ListView>
<!-- ... -->
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<MyGreatControl Thing="{x:Bind}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
MyGreatControl today has extensive keyboard handling code built-in, but due to some refactoring, I need to move the actual handlers to the ListView itself. However, I don't want to move all of the code in MyGreatControl to the ListView (for many reasons).
If I have an arbitrary ListViewItem (which, for example, I can get from an event handler), how can I access the MyGreatControl instance in its DataTemplate?
MyGreatControl^ GetMyGreatControlFromListViewItem(ListViewItem^ listViewItem) {
// ???
}
Disclaimer: I work for Microsoft.
You want to use ContentTemplateRoot!
MyGreatControl^ GetMyGreatControlFromListViewItem(ListViewItem^ listViewItem) {
return safe_cast<MyGreatControl^>(listViewItem->ContentTemplateRoot);
}
This also works for any arbitrary element—if you have a StackPanel, for example, ContentTemplateRoot will return the StackPanel instance you want:
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<StackPanel><!-- This is what you get! -->
<TextBlock Text="{x:Bind}" />
<Button Content="Foo" IsTabStop="False" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
You can then use normal visual tree navigation to find Children, etc.

UWP: Set text to bold in TreeNode

I am following Microsoft's documentation to implement a TreeView in a Universal Windows Platform app in C++. I have successfully been able to create a tree view with one node using the following codes:
XAML:
<TreeView x:Name="treeSolution"></TreeView>
C++:
TreeViewNode ^treeNode = ref new TreeViewNode();
treeNode->Content = "Hello";
treeSolution->RootNodes->Append(treeNode);
Now, I want to set the text to bold. I tried the following:
TextBlock ^textBlock = ref new TextBlock();
textBlock->Text = "Hello";
textBlock->FontWeight = Windows::UI::Text::FontWeights::Bold;
treeNode->Content = textBlock;
treeSolution->RootNodes->Append(treeNode);
The code displays Windows.UI.Xaml.Controls.TextBlock instead of Hello in bold.
The documentation says that In Windows 10, version 1803, you have to retemplate the TreeView control and specify a custom ItemTemplate if your content is not a string. It then gives a complex example using the Music and Picture library.
Could somebody provide a simple example of how to display the text in bold? Thanks.
You have to provide a custom style for the whole control in XAML to be able to set the TreeViewItemDataTemplate:
<DataTemplate x:Key="TreeViewItemDataTemplate">
<Grid Height="44">
<TextBlock
Text="{Binding Content}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Style="{ThemeResource BodyTextBlockStyle}"
FontWeight="Bold" />
</Grid>
</DataTemplate>
<Style TargetType="TreeView">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<TreeViewList x:Name="ListControl"
ItemTemplate="{StaticResource TreeViewItemDataTemplate}"
ItemContainerStyle="{StaticResource TreeViewItemStyle}"
CanDragItems="True"
AllowDrop="True"
CanReorderItems="True">
<TreeViewList.ItemContainerTransitions>
<TransitionCollection>
<ContentThemeTransition />
<ReorderThemeTransition />
<EntranceThemeTransition IsStaggeringEnabled="False" />
</TransitionCollection>
</TreeViewList.ItemContainerTransitions>
</TreeViewList>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

How to update live tiles in windows 10 using c++

The NotificationsExtensions::TileContent in windows 8.1 doesn't work in windows 10.
Therefore, I am using TileUpdateManager for live tiles, but live tiles doesn't work.
I am using struct like below for TileUpdateManager :
<tile>
<visual version='3'>
<binding template='TileSquare310x310Image' branding='name'>
<image id='1' src='ms-appx:///Assets/Tiles/310x310.png' alt='' />
</binding>
</visual>
</tile>
Is it the right way to use TileUpdateManager for live tiles?
What can I use to update live tiles like NotificationsExtensions::TileContent in windows 8.1?
Yes TileUpdateManager is the way to do it in Windows 10. Below is a small snippet in C#
var updater = TileUpdateManager.CreateTileUpdaterForApplication();
updater.EnableNotificationQueue(true);
XmlDocument document = new XmlDocument();
document.LoadXml(content); 'content is your xml'
TileNotification notification = new TileNotification(document);
updater.Update(notification);

Problems when using DataTemplate in a UWP app (crashing, data not being set)

I'm getting my feet wet trying out Windows 10 UWP app development. I've installed Visual Studio 2015 and am currently playing around with trying to figure out how to work with data binding.
The following is my simple XAML:
<Grid>
<Pivot x:Name="docPivot"
ItemsSource="{Binding}">
<Pivot.ItemTemplate>
<DataTemplate>
<PivotItem Header="{Binding Filename}">
<TextBox Text="{Binding Contents}"/>
</PivotItem>
</DataTemplate>
</Pivot.ItemTemplate>
</Pivot>
</Grid>
This is my Mainpage.xaml.cpp in relevant part: (Document is a simple struct that just has two properties, a String Filename and a String Contents.)
MainPage::MainPage()
{
InitializeComponent();
auto docs = ref new Vector<Document^>();
auto doc1 = ref new Document();
doc1->Filename = "Filename1";
doc1->Contents = "Contents 1";
docs->Append(doc1);
auto doc2 = ref new Document();
doc2->Filename = "Filename2";
doc2->Contents = "Contents 2";
docs->Append(doc2);
docPivot->ItemsSource = docs;
}
However, I'm having a pair of issues I can't figure out:
The first is, instead of each PivotItem's header being Filename, they're both MyApp.Document, where MyApp is my namespace.
The second issue is, the TextBox is being properly populated with the contents from the data binding, and the two PivotItems can be switched between, but as soon as I try and select a Textbox, the app crashes with an access violation:
Exception thrown at 0x0004CE1E in MyApp.exe: 0xC0000005: Access violation reading location 0x00000000.
Any input on what I'm doing wrong here?
First you must add Bindable attribute to Document class.
[Windows::UI::Xaml::Data::Bindable]
public ref class Document sealed
And you must add
#include "Document.h"
in Mainpage.xaml.h file not the .cpp file. You Pivot's ItemTemplate should not contain PivotItem, you should do like this
<Grid>
<Pivot x:Name="docPivot">
<Pivot.HeaderTemplate>
<DataTemplate>
<ContentControl Content="{Binding Filename}"/>
</DataTemplate>
</Pivot.HeaderTemplate>
<Pivot.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Contents}"/>
</DataTemplate>
</Pivot.ItemTemplate>
</Pivot>

Intellij/Android Studio Find/Replace a multiline view in xml layout (Android)

I have to replace this
<fr.castorflex.android.smoothprogressbar.SmoothProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="gone"
app:spb_color="#FF0000"
app:spb_mirror_mode="false"
app:spb_progressiveStart_activated="true"
app:spb_progressiveStart_speed="1.5"
app:spb_progressiveStop_speed="3.4"
app:spb_reversed="false"
app:spb_sections_count="4"
app:spb_speed="2.0"
app:spb_stroke_separator_length="4dp"
app:spb_stroke_width="4dp" />
by this
<fr.castorflex.android.smoothprogressbar.SmoothProgressBar
android:id="#+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="4dp"
android:visibility="invisible"
style="#style/GNowProgressBar"
android:indeterminate="true"/>
thoroughout my entire project using Intellij/Android Studio's Find/Replace.
Is it possible?
(For MAC and similarly for Windows)
Click on Edit (Top Left of your Intellij/Android Studio window)
Hover your cursor to "Find"
Then select "Replace Structurally"
Now the find/replace structurally window will open. Towards the bottom side of the window, uncheck all the boxes(Otherwise it might not identify the snippet through out the project).