gpt4 book ai didi

c# - 如何在来自 View 模型的异步 Web 客户端调用中使用进度条

转载 作者:太空宇宙 更新时间:2023-11-03 13:59:26 25 4
gpt4 key购买 nike

我正在尝试在 WP7 项目中使用性能进度条,但我在异步 webclient 调用方面遇到了问题。我的代码如下:

更新

    public MainPage()
{
InitializeComponent();
...................

this.Loaded += new RoutedEventHandler(MainPage_Loaded);}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}

还有我实现 LoadData 函数的 ViewModel

    private bool _showProgressBar = false;
public bool ShowProgressBar
{
get { return _showProgressBar; }
set
{
if (_showProgressBar != value)
{
_showProgressBar = value;
NotifyPropertyChanged("ShowProgressBar");
}
}
public void LoadData()
{
try
{
string defaulturl = "http://....";
WebClient client = new WebClient();
Uri uri = new Uri(defaulturl);
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
ShowProgressBar = true;
client.DownloadStringAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

this.IsDataLoaded = true;

}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
//fetch the data
ShowProgressBar = false;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{.....
}

主页 Xaml

    <toolkit:PerformanceProgressBar Margin="0,-12,0,0" x:Name="performanceProgressBar" IsIndeterminate="true" Visibility="{Binding ShowProgressBar, Converter={StaticResource BooleanToVisibilityConverter}}"/>

我的问题是,因为WebClient在执行时是一个异步方法,LoadData已经执行了,我不知道把performanceProgressBar.Visibility放在哪里

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

你在评论中解释得更多了,我明白了更多。听起来您想将 bool 属性绑定(bind)到进度条的可见性。您需要一个 bool 值到可见性的转换器(用谷歌搜索,很容易找到)。

然后你可以这样做:

private bool _showProgressBar = false;
public bool ShowProgressBar
{
get { _return _showProgressBar; }
set
{
_return _showProgressBar;
OnPropertyChanged("ShowProgressBar");
}
}

public void LoadData()
{
try
{
string defaulturl = "http://....";
WebClient client = new WebClient();
Uri uri = new Uri(defaulturl);
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

ShowProgressBar = true;

client.DownloadStringAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

this.IsDataLoaded = true;

}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
//fetch the data
ShowProgressBar = false;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

在 View 上:

<MyProgressBar Visibility={Binding Path=ShowProgressBar, Converter={StaticResource MyBoolToVisibleConverter}>

显然,MSFT 已经为您提供了一个转换器...这对我来说是个新闻:http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx

关于c# - 如何在来自 View 模型的异步 Web 客户端调用中使用进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11057969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com