English 中文(简体)
Can t use WinUI 3个Picker
原标题:Can t use WinUI 3 File Picker

I m试图在我的Windows App SDK(WinUI 3)申请中展示一个卷宗。 I m 将其列入C#。

在此,我的职责是展示卷宗号:

        private async Task<StorageFile> PickFileAsync()
        {
            var filePicker = new FileOpenPicker();
            filePicker.ViewMode = PickerViewMode.Thumbnail;
            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.FileTypeFilter.Add(".jpg");
            filePicker.FileTypeFilter.Add(".jpeg");
            filePicker.FileTypeFilter.Add(".png");

            var hwnd = this.XamlRoot.Content.XamlRoot.WindowId;

            WinRT.Interop.InitializeWithWindow.Initialize(filePicker, hwnd);

            StorageFile file = await filePicker.PickSingleFileAsync();
            if (file != null)
            {
                // Application now has read/write access to the picked file
                return file;
            }
            else
            {
                return null;
            }
        }

这就是我如何称职:

            StorageFile pickedFile = await PickFileAsync();

就其本身而言,Im指另一个功能,即:<条码>私人、<条码>、<条码>、<条码>和<条码>。

这样,我就把<代码>PickFileAsync()方法列入“代码>cs,作为我的一个参考页码,因为该页载有引发行动的菜单。

很少有文章显示这一点,因此,我实际上没有做很多研究。

问题回答

http://learn.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd#winui-3- with-c” rel=“nofollow noreferer” https://learn.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd#-3 with-c>

var filePicker = new FileOpenPicker();
...

var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

WinRT.Interop.InitializeWithWindow.Initialize(filePicker, hwnd);

StorageFile file = await filePicker.PickSingleFileAsync();

这里的一个例子是,你想在<代码>上打开卷宗号。 页: 1

首先,请您在网页上查阅。 这样做的一个途径是将其编成<条码>。

App.xaml.cs

using Microsoft.UI.Xaml;

namespace FilePickerExample;

public partial class App : Application
{
    public App()
    {
        this.InitializeComponent();
    }

    public static Window? Window { get; private set; }

    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        Window = new MainWindow();
        Window.Activate();
    }
}

页: 1

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using WinRT.Interop;

namespace FilePickerExample;

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker fileOpenPicker = new()
        {
            ViewMode = PickerViewMode.Thumbnail,
            FileTypeFilter = { ".jpg", ".jpeg", ".png", ".gif" },
        };

        nint windowHandle = WindowNative.GetWindowHandle(App.Window);
        InitializeWithWindow.Initialize(fileOpenPicker, windowHandle);

        StorageFile file = await fileOpenPicker.PickSingleFileAsync();

        if (file != null)
        {
            // Do something with the file.
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签