我的代码从我的 IIS 下载一个 XML 文件。 XML 被处理为绑定(bind)到我的 View XAML 代码的列表。
当我使用刷新命令刷新列表时,我想要 IsRefreshing
在我的列表更新时命令为真。IsRefreshing
以某种方式停留在 true
而且我的列表也没有更新。
你能帮我吗?
这是我的代码:
// ViewModel
public class ProcessesPageViewModel : BaseViewModel
{
DownloadProcesses downloadProcesses;
public Command LoadItemsCommand { get; set; }
public ProcessesPageViewModel()
{
LoadItemsCommand = new Command(ExecuteLoadItemsCommand);
CreateList();
}
void CreateList()
{
downloadProcesses = new DownloadProcesses();
var allProcesses = downloadProcesses.DownloadXML();
ProcessList = allProcesses.Processes;
}
void ExecuteLoadItemsCommand()
{
if (IsBusy)
return;
IsBusy = true;
try
{
CreateList();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
private Process[] _processList;
public Process[] ProcessList
{
get => _processList;
set => SetValue(ref _processList, value);
}
}
}
// Download Process
public class DownloadProcesses : BaseViewModel
{
public ProcessQuery DownloadXML()
{
IsBusy = true;
XmlSerializer serializer = new XmlSerializer(typeof(ProcessQuery));
string xml;
using (WebClient client = new WebClient())
{
xml = client.DownloadString("http://x.x.x.x/test.xml");
}
using (StringReader reader = new StringReader(xml))
{
IsBusy = false;
return (ProcessQuery)serializer.Deserialize(reader);
}
}
}
// Xaml
<ListView x:Name="ProcessesListView"
ItemsSource="{Binding ProcessList}"
VerticalOptions="Center"
HasUnevenRows="True"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="True"
ItemSelected="OnItemSelected">
IsRefreshing="{Binding IsBusy, Mode=TwoWay}"
最佳答案
这可能是您看到的事件顺序:
尝试将 IsRefreshing 绑定(bind)模式设置为 OneWay 并查看是否可行(当然,请确保 IsBusy 正在引发 OnPropertyChanged 事件)。您还可以假设 IsBusy 在您的命令中为 true,并且仅在完成后将其设置为 false。
如果您仍然遇到问题,请考虑为列表的 IsRefreshing 使用单独的属性,而不是使用现有的 IsBusy 属性。
关于c# - 使用下载服务将 IsRefreshing 绑定(bind)到 IsBusy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53223071/