我开发了一个关于Xamarin的应用程序。我在webview页面加载时尝试隐藏图像。我尝试不同的方法,如调用公共函数或从另一个类访问imageview。
我看过This,但它并不适用于Xamarin。
所以我尝试了一些
[Activity(Label = "XamarinWebView", Theme = "@android:style/Theme.Black.NoTitleBar", MainLauncher = true)]
public class MainActivity : Activity
{
WebView app_view = null;
WebSettings app_web_settings = null;
WebChromeClient web_client;
MyWebViewClient my_web_client;
ImageView my_splash = null;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
app_view = FindViewById(Resource.Id.webViewapp) as WebView;
my_web_client = new MyWebViewClient(this.ApplicationContext);
app_view.SetWebViewClient(my_web_client);
string app_url = "file:///android_asset/app_pages/test.html";
app_view.LoadUrl(app_url);
my_splash = FindViewById(Resource.Id.imageSplash) as ImageView;
my_splash.SetImageDrawable(GetDrawable(Resource.Drawable.splash));
}
public void HideSplash()
{
my_splash.Visibility = ViewStates.Gone;
}
}
在这个课程中,我可以获得WebView页面加载状态。
public class MyWebViewClient : WebViewClient
{
Context context;
public MyWebViewClient(Context _context) {
this.context = _context;
}
public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
**I need to change visible my_splash**
}
}
答案 0 :(得分:0)
根据杰森的建议,我尝试了一些可行的方法。
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
.
.
.
my_web_client = new MyWebViewClient(this);
.
.
.
}
public void HideSplash()
{
my_splash.Visibility = ViewStates.Gone;
}
}
public class MyWebViewClient : WebViewClient
{
MainActivity act;
public MyWebViewClient(MainActivity activity)
{
this.act = activity;
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
act.Hide_Splash();
}
}
答案 1 :(得分:0)
从另一个类获取活动的一种更简单的方法,也就是更不容易出错并且不太可能导致内存泄漏,那就是从视图上下文中获取活动。
在MyWebViewClient
课程中:
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
(view.Context as MainActivity).HideSplash();
}
或者,如果您不确定上下文是否始终为MainActivity
,您可以使用涵盖空检查的模式匹配:
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
if(view.Context is MainActivity mainActivity)
{
mainActivity.HideSplash();
}
}
使用这样的方法更容易维持下去。