【问题标题】:how to loading image in uwp while background worker is running如何在后台工作人员运行时在 uwp 中加载图像
【发布时间】:2017-04-27 01:12:23
【问题描述】:

我在我的 uwp 项目中使用后台工作者。在这里,我想在后台工作人员运行时显示加载图像。后台工作任务完成后,加载图像应该是不可见的。 我该怎么做。

BackgroundWorker productPrices = new BackgroundWorker();

productPrices.DoWork += new DoWorkEventHandler(productPrices_Dowork);
productPrices.RunWorkerCompleted += productPrices_RunWorkerCompleted;
productPrices.WorkerSupportsCancellation = true;
productPrices.ProgressChanged += new ProgressChangedEventHandler(productPrices_ProgressChanged);
productPrices.WorkerReportsProgress = true;

private async void productPrices_Dowork(object sender, DoWorkEventArgs e)
{
    progressring.Visibility = Visibility.Visible;
    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
    {
        string customerId = Application.Current.Resources[Constants.CUSTOMER_ID].ToString();
        string username = Application.Current.Resources[Constants.EMAIL_ID].ToString();
        string strIpAddress = Application.Current.Resources[Constants.IP_ADDRESS].ToString();
        string strPortNumber = Application.Current.Resources[Constants.PORT_Number].ToString();
        RequestHeader requestHeader = DBHelper.GetRequestHeader(customerId, customerId, username);
        string strStoreLocation = Application.Current.Resources[Constants.STORE_LOCATION].ToString();
        int startIndex = 0;
        await ProductAgent.GetProductPrices(requestHeader, strIpAddress, strPortNumber, strStoreLocation);

    });
}

【问题讨论】:

  • 我们绝对可以做到,你有没有尝试过什么?
  • 我已经编辑了我的问题
  • 实际上在 UWP 平台中内置了一些您可以使用的东西。它的名字叫ProgressRing 这会满足您的需求还是需要自定义图像?如果是后者,请查看this page 获取一些提示
  • 进度环足够了,但我无法在后台工作人员运行时显示它

标签: c# .net windows uwp win-universal-app


【解决方案1】:

您的问题是您在 DoWork 事件处理程序中设置进度环的可见性。这个是在后台线程上运行的,所以它应该给你一个例外:

抛出异常:UWP.exe 中的“System.Exception”

附加信息:应用程序调用了为不同线程编组的接口。 (来自 HRESULT 的异常:0x8001010E (RPC_E_WRONG_THREAD))

移动这一行:

progressring.Visibility = Visibility.Visible;

...到你打电话给RunWorkerAsync的任何地方。如果这已经在后台线程上,您需要使用调度程序在 UI 线程上调用那段代码,例如,

await Dispatcher.RunAsync(
    CoreDispatcherPriority.Normal,
    () => progressring.Visibility = Visibility.Visible);

(如果您愿意,也可以在 DoWork 事件处理程序中执行此操作。)


为了完整起见,提供一个工作示例。在 XAML 中:

<Page
    x:Class="UWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ProgressRing
            x:Name="progressRing"
            Height="30"
            IsActive="False"
            Visibility="Collapsed"
            VerticalAlignment="Top"
            HorizontalAlignment="Left" />
        <Button
            Content="Go"
            Margin="0,30"
            VerticalAlignment="Top"
            Click="OnGoClick" />
    </Grid>
</Page>

在代码隐藏中:

public partial class MainPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void OnGoClick(object sender, RoutedEventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += OnDoWork;
        worker.RunWorkerCompleted += OnRunWorkerCompleted;
        progressRing.IsActive = true;
        progressRing.Visibility = Visibility.Visible;
        worker.RunWorkerAsync();
    }

    private void OnDoWork(object o, DoWorkEventArgs args)
    {
        Task.Delay(2000).Wait(); // Pretend to work
    }

    private void OnRunWorkerCompleted(object o, RunWorkerCompletedEventArgs args)
    {
        progressRing.Visibility = Visibility.Collapsed;
        progressRing.IsActive = false;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多