对于我来说,采用Xamarin表单WebView并为第一次加载添加加载进度条的最简单方法是什么?
我的应用程序只是包装一个网站但是在带宽不佳的情况下,当启动画面消失并且WebView正在加载它的内容时,我当然会看到白屏。
我想看一个进度指标。我环顾四周,但没有找到一个简单的解决方案,大多数问题都没有针对Xamarin.Forms。
提前致谢。
亲切的问候, 标记
答案 0 :(得分:6)
只需将StackLayout
与ActivityIindicator
和WebView
作为子项使用,并根据 WebView.Navigating
中设置的IsWorking属性值设置其可见性和 WebView.Navigated
事件。您的ViewModel必须实现IOnPropertyChanged
。
var webViewView = new WebView() {
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
};
webViewView.SetBinding<YourViewModel>(View.IsVisibleProperty, v => v.IsWorking,
BindingMode.OneWay, new Converters.NegateBoolConverter());
webViewView.Navigating += (sender, e) => {
viewModel.IsWorking = true;
};
webViewView.Navigated += (sender, e) => {
viewModel.IsWorking = false;
};
var activityIndicator = new ActivityIndicator() {
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
};
activityIndicator.SetBinding<YourViewModel>(ActivityIndicator.IsRunningProperty, v => v.IsWorking);
var isWorkingView = new ContentView() {
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Content = activityIndicator
};
isWorkingView.SetBinding<YourViewModel>(ContentView.IsVisibleProperty, v => v.IsWorking);
var root = new StackLayout() {
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Children = {
webViewView,
isWorkingView,
}
};