UWP FolderPicker 窗口打开但被冻结 [英] UWP FolderPicker window opens but is frozen

查看:22
本文介绍了UWP FolderPicker 窗口打开但被冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本思想是我有一个 UWP 应用程序从本地保存的 json 文件中提取用户数据,并且在不同时间它可能会从文件中提取完整的对象列表,但它始终检查用户是否设置了位置数据,如果没有,则通过 FolderPicker 提示用户设置位置.在这种情况下,我有组合框,可以在选择条件并输入文本后帮助过滤对象.这是调用堆栈:

The basic idea is I have a UWP app pulling user data from a json file saved locally, and at various times it may pull that full list of objects from the file, but it always checks that the user has set the location for the data, and if not, prompts via a FolderPicker for the user to set the location. In this case, I have combobox that helps filter the objects after selecting a criteria and entering text. Here's the call stack:

UWPMiniatures.exe!UWPMiniatures.Data.MiniDAL.SetSaveFolder() 第 98 行 C# 符号已加载.UWPMiniatures.exe!UWPMiniatures.Data.MiniDAL.LoadAllAsync() 第 71 行 C# 符号已加载.UWPMiniatures.exe!UWPMiniatures.Data.MiniDataService.Load() 已加载第 36 行 C# 符号.UWPMiniatures.exe!UWPMiniatures.MainPage.FilterGridView(字符串提交)第 156 行 C# 符号已加载.UWPMiniatures.exe!UWPMiniatures.MainPage.SearchIcon_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) 已加载第 95 行 C# 符号.

UWPMiniatures.exe!UWPMiniatures.Data.MiniDAL.SetSaveFolder() Line 98 C# Symbols loaded. UWPMiniatures.exe!UWPMiniatures.Data.MiniDAL.LoadAllAsync() Line 71 C# Symbols loaded. UWPMiniatures.exe!UWPMiniatures.Data.MiniDataService.Load() Line 36 C# Symbols loaded. UWPMiniatures.exe!UWPMiniatures.MainPage.FilterGridView(string submission) Line 156 C# Symbols loaded. UWPMiniatures.exe!UWPMiniatures.MainPage.SearchIcon_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) Line 95 C# Symbols loaded.

因此,工作倒叙,此处调用 FolderPicker:

So, working backwords, the FolderPicker is being called here:

private async Task SetSaveFolder()
{
    if(!StorageApplicationPermissions.FutureAccessList.ContainsItem("PickedFolderToken"))
    {
        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        folderPicker.FileTypeFilter.Add("*");
        folderPicker.CommitButtonText = "Pick A Folder To Save Your Data";
        StorageFolder folder = await folderPicker.PickSingleFolderAsync();
        if (folder != null)
        {
            // Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
            StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
            var userFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("PickedFolderToken");
            var file = await userFolder.CreateFileAsync("AllMinisList.json",CreationCollisionOption.OpenIfExists);
            var imagefolder = await userFolder.CreateFolderAsync("Images");
        }

    }
}

文件夹选择器对话框打开,文件夹:旁边有一个闪烁的光标,但当我点击任何地方时没有任何反应,我也不能输入文件夹:文本框.现在,将这个相同的代码放在一个新项目中并调用它来响应单击事件工作正常:对话框打开,我创建一个新文件夹或选择一个现有文件夹,它被添加到未来的访问列表中.不知道如何解决这个问题,但问题似乎出在调用 FolderPicker 的实际代码之外.这是另一个调用函数的代码

The folder picker dialog opens, with a blinking cursor next to Folder:, but nothing happens when I click anywhere, nor can i type in Folder: textbox. Now, putting this identical code in a new project and calling it in response to a click event works fine: Dialog opens, I make a new folder or pick an existing one, it gets added to future access list. Not sure how to else to troubleshoot this but the problem seems to lie out side the actual code calling the FolderPicker. here is the code for the other calling function

private void SearchIcon_Click(object sender, RoutedEventArgs e)
{
    FilterGridView(SearchTextBox.Text);
    SearchTextBox.Text = "";
}

    private async void FilterGridView(string submission)
{
    var selected = FilterComboBox.SelectedValue;

    miniDS = new MiniDataService();  

   if(selected.ToString()=="All")
    {
        MiniList.Clear();
        List<Miniature> fullList = await miniDS.Load();
        fullList.ForEach(m => MiniList.Add(m));
    }
    else if (selected.ToString() == "Quantity")
    {
        List<Miniature> fullList = await miniDS.Load();
        var templist = fullList.AsQueryable()
         .Where($"{selected} = @0", submission); ;
        MiniList.Clear();
        templist.ToList<Miniature>()
            .ForEach(m => MiniList.Add(m));
    }
    else
    {
        List<Miniature> fullList = await miniDS.Load();
        var templist = fullList.AsQueryable()
        .Where($"{selected}.StartsWith(@0)", submission);
        MiniList.Clear();
        templist.ToList<Miniature>()
            .ForEach(m => MiniList.Add(m));
    }
}

MiniDataService 和 MiniDal 除了传递调用之外没有做太多事情.有什么想法可以解决这个问题吗?更新:一些附加信息,我将代码从 SetSaveFolder() 直接复制到按钮的新事件处理程序中,单击它,我得到 FolderPicker,功能完美.但这根本不是所需的功能.我需要直接或间接从我的数据服务调用它.所以这里是它创建的地方:

MiniDataService and MiniDal don't do much here other than pass the call along. Any ideas where I can look to troubleshoot this? UPDATE: Some additional info, I copied the code from SetSaveFolder() directly into a new event handler for a button, clicked it, I get FolderPicker, functions perfectly. But thats not at all the functionality needed. I need it to be called, directly or indirectly from my Data Service. So here's where its created:

 public sealed partial class MainPage : Page
    {
        /// <summary>
        /// MiniList is the list of minis currently being displayed
        /// </summary>
        private ObservableCollection<Miniature> MiniList;
        private MiniDataService miniDS;       
        private List<string> FilterComboList;
        private Miniature NewMini;

        public MainPage()
        {
            this.InitializeComponent();           
            miniDS = new MiniDataService();
            MiniList = new ObservableCollection<Miniature>();         
            FilterComboList = PopulateFilterCombo();
            NewMini = new Miniature();
            MyFrame.Navigate(typeof(MiniListPage), MiniList);
        }
...

所以这个问题似乎与从这个静态"对象调用 FolderPicker 的事实有关.这是线程问题吗?我想在 UWP 中我总是在 UI threadm 上,因为在顶级事件处理程序正在调用 folderPicker 我不明白为什么 UI 似乎被锁定.

So the problem seems to have something to do with the fact that FolderPicker is being called from this "static" object. Is this a thread issue? I thought in UWP I am always on the UI threadm and since at the top level an event handler is calling folderPicker I can't understand why the UI seems locked.

推荐答案

所以我想我明白了,虽然我不知道为什么会这样.如果有人能告诉我,我很感激.所以从调用 ListfullList = await miniDS.Load();方法如下:

So i think I figured it out, though I have no idea why this happened. If anyone can clue me in, id appreciate it. So from the call List<Miniature> fullList = await miniDS.Load(); Here's that method:

 public async Task<List<Miniature>> Load()
        {
            return await minidal.LoadAllAsync();
        }

public async Task<List<Miniature>> LoadAllAsync()
        {

            List<Miniature> MiniCollection = new List<Miniature>();
            if (StorageApplicationPermissions.FutureAccessList.ContainsItem("PickedFolderToken"))
            {
                try
                {
                    var userFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("PickedFolderToken");
                    var file = await userFolder.GetFileAsync("AllMinisList.json");
                    var data = await file.OpenReadAsync();

                    using (StreamReader stream = new StreamReader(data.AsStream()))
                    {
                        string text = stream.ReadToEnd();
                        MiniCollection = JsonConvert.DeserializeObject<List<Miniature>>(text);
                    }
                }
                catch(Exception e)
                {
                    throw e;
                }
            }
            else
            {
                SetSaveFolder().Wait();
                return MiniCollection;
            }

            return MiniCollection;
        }

所以问题就在这里:

SetSaveFolder().Wait();

当我用

await SetSaveFolder();

它工作正常.我可以点击文件夹选择器,它会做它应该做的.我想我虽然 .Wait() 是在您不返回任何其他内容时使用的,但似乎还有更多!

it works fine. I can click in the folderPicker, and it does what it's supposed to. I guess I though .Wait() was used when you aren't return anything other than but it seems there is more too it than that!

这篇关于UWP FolderPicker 窗口打开但被冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆