问题描述
我不想为我的 uwp 应用程序创建照片上传方法,但没有办法做到这一点,因为我无法使用 FileOpenPicker 或任何其他选择器!它找不到 FileOpenPicker,即使声明为 Windows.Storage.Pickers.FileOpenPicker();
它抱怨找不到类型或命名空间 Windows。
即使我把 using Windows.Storage.Pickers;
放在文件的顶部
我已经安装了 NuGet UwpDesktop-Updated 无济于事。这很奇怪,因为我已经用谷歌搜索了好几天没有找到其他人解决我的问题!
还有其他方法可以用c#.net core在uwp中上传照片吗?
using System;
using System.Windows.Controls;
using Image.ViewModels;
namespace Image.Views
{
public partial class MainPage : Page
{
public MainPage(MainViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
}
private async void Clicked(object sender,EventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
this.textBlock.Text = "Picked photo: " + file.Name;
}
else
{
this.textBlock.Text = "Operation cancelled.";
}
}
}
}
解决方法
对于桌面应用程序,您需要使用 IInitializeWithWindow
interface。或者您可以使用 WPF 内置的选择器。