I am trying to create a file picker using WInUI3. My app crashes when I click on the open file button. Below is the photo of the error....
It says :
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.Windows.SDK.NET
StackTrace:
at WinRT.Interop.IWindowNativeMethods.get_WindowHandle(Object _obj)
at WinRT.Interop.WindowNative.GetWindowHandle(Object target)
at WINUITMP.Views.MainPage.<PickAFileButton_Click>d__4.MoveNext() in C:\Users\User\source\repos\WINUITMP\Views\MainPage.xaml.cs:line 95
I just added the WindowsHelper class from This Link which is in github Please help to fix this below is my C# code
using Microsoft.UI.Xaml.Controls;
using CommunityToolkit.WinUI.Controls;
using WINUITMP.ViewModels;
using WINUITMP.Views;
using Windows.Storage.Pickers;
using WinRT.Interop;
using CommunityToolkit.WinUI.Helpers;
using Windows.Storage;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using System;
using System.Collections.Generic;
using AppUIBasics.Helper;
using Microsoft.UI.Xaml.Media;
namespace AppUIBasics.Helper
{
// Helper class to allow the app to find the Window that contains an
// arbitrary UIElement (GetWindowForElement). To do this, we keep track
// of all active Windows. The app code must call WindowHelper.CreateWindow
// rather than "new Window" so we can keep track of all the relevant
// windows. In the future, we would like to support this in platform APIs.
public class WindowHelper
{
public static Window CreateWindow()
{
Window newWindow = new Window
{
SystemBackdrop = new MicaBackdrop()
};
TrackWindow(newWindow);
return newWindow;
}
public static void TrackWindow(Window window)
{
window.Closed += (sender, args) => {
ActiveWindows.Remove(window);
};
ActiveWindows.Add(window);
}
public static AppWindow GetAppWindow(Window window)
{
var hWnd = WindowNative.GetWindowHandle(window);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(wndId);
}
public static Window? GetWindowForElement(UIElement element)
{
if (element.XamlRoot != null)
{
foreach (Window window in ActiveWindows)
{
if (element.XamlRoot == window.Content.XamlRoot)
{
return window;
}
}
}
return null;
}
public static List<Window> ActiveWindows { get; } = new List<Window>();
}
}
namespace WINUITMP.Views
{
public sealed partial class MainPage : Page
{
public MainViewModel ViewModel
{
get;
}
public MainPage()
{
ViewModel = App.GetService<MainViewModel>();
InitializeComponent();
}
private async void PickAFileButton_Click(object sender, RoutedEventArgs e)
{
// Clear previous returned file name, if it exists, between iterations of this scenario
PickAFileOutputTextBlock.Text = "";
// Create a file picker
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
// Retrieve the window handle (HWND) of the current WinUI 3 window.
var window = WindowHelper.GetWindowForElement(this);
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
// Initialize the file picker with the window handle (HWND).
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hWnd);
// Set options for your file picker
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.FileTypeFilter.Add("*");
// Open the picker for the user to pick a file
var file = await openPicker.PickSingleFileAsync();
if (file != null)
{
PickAFileOutputTextBlock.Text = "Picked file: " + file.Name;
}
else
{
PickAFileOutputTextBlock.Text = "Operation cancelled.";
}
}
}
}
I need a way to fix this , I dont know why it crashes.