UWP development issues - c++

I'm developing UWP app and want to set 2 columns for GridView or ListView by code.
Code:
GridView ^gView = ref new GridView();
ListView ^lView = ref new ListView();
I can't find any code or documentation how to do it programmatically. Thanks.

Yes I want 2 columns for the Grid. For example first column is property, second - data. How to achieve it by code.
I could not understand why you want to do it programmatically. In general, customizing its DataTemplate on XAML page directly is easy. For example:
<GridView ItemsSource="{Binding xx}">
<GridView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding property}"></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding data}"></TextBlock>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
If you have to specify the DataTemplate programmatically, you could do like the following:
<Page.Resources>
<DataTemplate x:Key="datatemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding property}"></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding data}"></TextBlock>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<GridView x:Name="gd" ItemsSource="{Binding}">
</GridView>
</Grid>
public MainPage()
{
this.InitializeComponent();
gd.ItemTemplate = this.Resources["datatemplate"] as DataTemplate;
}
It's C# code, you would need to convert it to C++.

Related

WinUI 3 - C++/WinRT DataTemplateSelector : Unable to cast object

I want to create a Template Selector in WinUI 3 using c++/WinRT.
For example, if the type of a person is "Employee" i want to show his name in Red and if is "Unemployee", in Blue.
I recreate the example from Microsoft but i got
this error
The code is here . All VisualStudio components are up to date.
XAML code:
<StackPanel>
<Page>
<Page.Resources>
<DataTemplate x:Key="template1" x:DataType="x:Int32">
<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{ThemeResource SystemChromeLowColor}">
<TextBlock Text="{x:Bind}" />
</Button>
</DataTemplate>
<DataTemplate x:Key="template2" x:DataType="x:Int32">
<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{ThemeResource SystemAccentColor}">
<TextBlock Text="{x:Bind}" />
</Button>
</DataTemplate>
<local:Selector x:Key="mySelector"
firstTemplate="{StaticResource template1}"
secondTemplate="{StaticResource template2}"/>
</Page.Resources>
</Page>
<ListView
ItemTemplateSelector = "{StaticResource mySelector}">
</ListView>
</StackPanel>
Thanks!

Show progressbar on the top in the existing MainPage.xaml in UWP app(c++) by masking all the other controls in UI

I have an UWP C++ app for getting some information which takes a long time to load .While startup,its taking time to get the information and update the UI.So I thought of putting a progress ring while the UI is getting data. I need the progress ring to show on the UI during the loading time in MainPage.xaml thereby making the existing controls invisible.But it is not coming on top of UI.After getting data to UI,progress ring should disappear and all the controls should be visible.
<Page
x:Class="Ft_Information.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Ft_Information"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="MainWindow_Loaded" BorderThickness="10" IsTabStop="True">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid x:Name="ftnfoGrid" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="25*" />
<RowDefinition Height="500*" />
<RowDefinition Height="100*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="500*" />
<ColumnDefinition Width="800*" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="1">
<TextBox x:Name="ProductNameText" x:Uid="ProductName" TextWrapping="Wrap" BorderThickness="0" IsTabStop="False" FontSize="13" IsReadOnly="True" />
<StackPanel Grid.Row="2" RenderTransformOrigin="0.5,0.5" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Margin="55,5.333,0.333,-5" VerticalAlignment="Stretch">
<ProgressRing Name="ProgressRingLbl" Height="120" Width="109" HorizontalAlignment="Left"
Foreground="Blue" IsActive="True" Visibility="Visible" VerticalAlignment="Center" Margin="0,0,0,0"/>
</StackPanel>
</Grid>
</Grid>
</Page>
MainPage::MainPage()
{
ProductNameText->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
ProgressRingLbl->IsActive = true;
ProgressRingLbl->Visibility = Windows::UI::Xaml::Visibility::Visible;
}
MainPage::FuntouploadUI()
{
.............
//after getting data
ProgressRingLbl->IsActive = false;
ProgressRingLbl->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
ProductNameText->Visibility = Windows::UI::Xaml::Visibility::Visible;
}
Still the progress bar is not showing anywhere in the UI.
I write a simple demo below.You can use this code in your application to test if the progress ring can show in the UI.
<Page ...>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Background="AliceBlue">
<TextBlock x:Name="MyTextBlock" Height="50" FontSize="16" Margin="0,20,0,20">Hello,world</TextBlock>​
<Button x:Name="MyButton" Click="Button_Click">click me</Button>​
<ProgressRing x:Name="MyProgressRing" Height="120" Width="109" Foreground="Blue" IsActive="True" Visibility="Visible" HorizontalAlignment="Center" VerticalAlignment="Center"></ProgressRing>​
</StackPanel>
</Grid>
</Page>
MainPage::MainPage()
{​
InitializeComponent();​
MyTextBlock->Visibility = Windows::UI::Xaml::Visibility::Collapsed;​
MyButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed;​
MyProgressRing->IsActive = true;​
MyProgressRing->Visibility = Windows::UI::Xaml::Visibility::Visible;​
​
}
The reason why progress ring not working was we are using another view to show the mainpage.xaml as a child to its root grid.When I moved the progress ring part to that xaml ,it is working fine. Since progress ring is working as a seperate UI thread it is not called by the mainpage.

Multiline Raw string confuses Visual Studio

This is part of an UWP project for a multiple save/open dialog with GoogleDrive/OneDrive/DropBox features I'm creating based on my GOD library.
Try putting this to a cpp file:
std::wstring OpenFileX = LR"(<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Orientation="Vertical">
<StackPanel Orientation="Vertical" x:Name="run" Visibility="Collapsed">
<ProgressRing x:Name="runProgress" IsActive="true" Margin="10" />
<ProgressBar x:Name="runBar" Value="0" Maximum="100" Margin="10"/>
<Button x:Name="runCancel" Margin="10">Cancel</Button>
</StackPanel>
<Pivot x:Name="pi">
<PivotItem Header="Local">
<StackPanel Orientation="Vertical" Margin="30">
<StackPanel Orientation="Horizontal">
<TextBox x:Name="localFile"
PlaceholderText="Filename"
Width="300" HorizontalAlignment="Left"/>
<Button x:Name="localPick" Margin="10,0,0,0" Width="50">...</Button>
</StackPanel>
<Button x:Name="localSave" Margin="0,20,0,0">Open</Button>
</StackPanel>
</PivotItem>
<PivotItem Header="Google Drive" x:Name="name_google">
<StackPanel Orientation="Vertical" Margin="30">
<ProgressRing x:Name="googleProgress" IsActive="true" />
<StackPanel Orientation="Horizontal" Visibility="Collapsed" x:Name="sp1">
<TextBox x:Name="googleFile"
PlaceholderText="Filename"
Width="300" HorizontalAlignment="Left"/>
<Button x:Name="googleSave" Margin="10,0,0,0" >Open</Button>
<Button x:Name="googleTop" Margin="10,0,0,0" >Top</Button>
</StackPanel>
<Grid Margin="0,20,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" x:Name="ColDef1" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" x:Name="RowDef1" />
</Grid.RowDefinitions>
<ListView Grid.Row="0" Grid.Column="0" CanDragItems="True" x:Name="googleList" SelectionMode="Extended" ScrollViewer.VerticalScrollBarVisibility="Visible" />
</Grid>
</StackPanel>
</PivotItem>
<PivotItem Header="OneDrive" x:Name="name_one">
<StackPanel Orientation="Vertical" Margin="30">
<ProgressRing x:Name="oneProgress" IsActive="true" />
<StackPanel Orientation="Horizontal" Visibility="Collapsed" x:Name="sp2">
<TextBox x:Name="oneFile"
PlaceholderText="Filename"
Width="300" HorizontalAlignment="Left"/>
<Button x:Name="oneSave" Margin="10,0,0,0" >Open</Button>
<Button x:Name="oneTop" Margin="10,0,0,0" >Top</Button>
</StackPanel>
<Grid Margin="0,20,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" x:Name="ColDef2" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" x:Name="RowDef2" />
</Grid.RowDefinitions>
<ListView Grid.Row="0" Grid.Column="0" CanDragItems="True" x:Name="oneList" SelectionMode="Extended" ScrollViewer.VerticalScrollBarVisibility="Visible" />
</Grid>
</StackPanel>
</PivotItem>
<PivotItem Header="DropBox" x:Name="name_db">
</PivotItem>
</Pivot>
</StackPanel>
)";
Then Vs is totally confused on the lines. Pressing F10 to start with WinMain shows code about 10 lines before WinMain.
Is this a known Visual Studio bug? Tried with VS 2019.

UWP ListView takes more than available height

In my FluentTorrent app I want to have a file list inside a listview item that contains a torrent. So the child of a listview is a Pivot which, among others, has this PivotItem:
<PivotItem Header="Files" x:Name="PivotFiles">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" x:Name="RowDef2" />
</Grid.RowDefinitions>
<ListView Grid.Row="0" Grid.Column="0" x:Name="FilesView" SelectionMode="Extended" ScrollViewer.VerticalScrollBarVisibility="Visible" />
</Grid>
</PivotItem>
....
The problem is that this file-list containing listview height goes beyond the available height when the torrent has too many files. There is no scroll bar so many of the files are not visible (only these that would fit the space taken by the height of the entire window are visible).
Is there a way to tell the listview item (a StackPanel) that its available height should be automatically adjusted? The main ListView takes the whole HWND height anyway.
You could try to enforce the height of your Grid row:
<Grid.RowDefinitions>
<!-- full size row -->
<RowDefinition Height="*" x:Name="RowDef2" />
</Grid.RowDefinitions>
--- disclaimer:
I am not particular sure since it might depend on your full page's layout.

Why TreeView becomes recursive when it displays Regex matches?

I tried to make app for testing regular expression
by using TreeView to display MatchCollection to window
But it does not work correctly
App.xaml
<Application x:Class="RegularExpressionTester.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RegularExpressionTester"
xmlns:Regex="clr-namespace:System.Text.RegularExpressions;assembly=System"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:MainViewModel x:Key="MainViewModel"/>
<HierarchicalDataTemplate DataType="{x:Type Regex:Match}" ItemsSource="{Binding Path=Groups}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Match)}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Group}" ItemsSource="{Binding Path=Captures}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Group)}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Capture}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Capture)}"/>
</HierarchicalDataTemplate>
</Application.Resources>
Result:
I want to display like this (expected result)
▶ Match
..▶ Group
....▶ Capture
▶ Match
..▶ Group
....▶ Capture
How I can do?
why is it possible to get infinite hierarhy?
it is due to structure of Match.Groups and Group.Captures
The Match instance itself is equivalent to the first object in the collection, at Match.Groups[0]
from Match class - Remarks
the Group instance is equivalent to the last item of the collection returned by the Captures property, which reflects the last capture made by the capturing group
from Group class - Remarks
how to fix?
modify Templates like this:
<HierarchicalDataTemplate DataType="{x:Type Regex:Match}"
ItemsSource="{Binding Path=Groups}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Match)}"/>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Group}"
ItemsSource="{Binding Path=Captures}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Group)}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type Regex:Capture}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Capture)}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>