Edit 3: Complete project if anybody wants to check
https://github.com/sumnoon/ProgressRingSample_WinUI3
Edit 2: Added Complete code
Edit: Added the code I am trying to use as sample
I am trying to add progress ring in WinUI 3 sample app. The plan is simple, add a button and when clicked show a progress ring.
I have intialized the progress ring similar to this. This is the XAML code for ProgressRingControl.xaml,
<UserControl
x:Class="progress_sample.ProgressRingControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:progress_sample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Grid x:Name="IndeterminateProgressPanel"
Background="LightGray"
Opacity=".5">
</Grid>
<ProgressRing x:Name="IndeterminateProgress"
Foreground="Red"
Width="65"
Height="65" />
</Grid>
And this the code for ProgressRingControl.xaml.cs,
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Windows.UI.Core;
namespace progress_sample
{
public sealed partial class ProgressRingControl : UserControl
{
private Popup popup = null;
public ProgressRingControl()
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().BackRequested += ProgressRingControl_BackRequested;
Window.Current.CoreWindow.SizeChanged += CoreWindow_SizeChanged;
popup = new Popup
{
Child = this
};
}
public static ProgressRingControl Instance
{
get { return Singleton<ProgressRingControl>.Instance; }
}
private void CoreWindow_SizeChanged(CoreWindow sender, Windows.UI.Core.WindowSizeChangedEventArgs args)
{
UpdateUI();
}
private void UpdateUI()
{
var bounds = Window.Current.Bounds;
this.Width = bounds.Width;
this.Height = bounds.Height;
}
private void ProgressRingControl_BackRequested(object sender, BackRequestedEventArgs e)
{
//HideProgressRing();
}
public void ShowProgressRing()
{
IndeterminateProgress.IsActive = true;
popup.IsOpen = true;
UpdateUI();
}
public void HideProgressRing()
{
if (popup.IsOpen)
{
IndeterminateProgress.IsActive = false;
popup.IsOpen = false;
SystemNavigationManager.GetForCurrentView().BackRequested -= ProgressRingControl_BackRequested;
Window.Current.CoreWindow.SizeChanged -= CoreWindow_SizeChanged;
}
}
}
}
I am calling this From MainWindow.xaml using a button in such a way that when button is clicked the progress ring should show for 1000ms than hide. I have added MainWindow.xaml and MainWindows.xaml.cs code also, MainWindow.xaml code:
<Window
x:Class="progress_sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:progress_sample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
</Window>
And MainWindow.xaml.cs code is given below,
using Microsoft.UI.Xaml;
using System.Threading.Tasks;
namespace progress_sample
{
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
private async void myButton_Click(object sender, RoutedEventArgs e)
{
ProgressRingControl.Instance.ShowProgressRing();
await Task.Delay(1000);
ProgressRingControl.Instance.HideProgressRing();
}
}
}
But during initialization of ProgressRingControl, it is crashing and gives this error,
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Any help?