i have a Xamarin-Android app in which i used fragments .i want to access public Method of MainActivity in my fragment class.
webview_download+=mWebViewDownload
but this method mWebViewDownload is defined in MainActivity.i cannot access this method from fragment.
i tried to make this method static but this method uses services which can't be accessed without instance. i tried to access through this.mWebViewDownload
but the error is mWebViewDownload is not defined in this scope like that. i searched stackoverflow for it most of question suggest getActivity() but this is java related solution but i need c# related solution. i tried to access it through MainActivity.mWebViewDownload
but it also gives error that cann't access non-static without object reference like that.please help.fragment class is as follows:
internal class WebviewFragment : Fragment
{
public const string ARG_NUMBER = "number";
public WebviewFragment()
{
// Empty constructor required for fragment subclasses
}
public static Fragment NewInstance(int position)
{
Fragment fragment = new WebviewFragment();
Bundle args = new Bundle();
args.PutInt(WebviewFragment.ARG_NUMBER, position);
fragment.Arguments = args;
return fragment;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.fragment_content2, container, false);
var i = this.Arguments.GetInt(ARG_NUMBER);
var url = this.Resources.GetStringArray(Resource.Array.weburls_array)[i];
var title = this.Resources.GetStringArray(Resource.Array.contents_array)[i];
// show progress bar
progressBar = (ProgressBar)rootView.FindViewById<ProgressBar>(Resource.Id.progressBar1);
var web_view = rootView.FindViewById<WebView>(Resource.Id.webview);
web_view.SetWebViewClient(new HelloWebViewClient());
web_view.Settings.JavaScriptCanOpenWindowsAutomatically = true;
web_view.Settings.JavaScriptEnabled = true;
web_view.Download += Mwebview_Download;// here is error
//set the custom web client
web_view.SetWebViewClient(new JavaScriptWebViewClient());
web_view.LoadUrl(url);
this.Activity.Title = title;
return rootView;
}
}
here is the mWebView_Download Method in MainActivity class
// Download
public void Mwebview_Download(object sender, DownloadEventArgs e)
{
var listPermissions = new System.Collections.Generic.List<string>();
if (CheckSelfPermission(Android.Manifest.Permission.WriteExternalStorage) != Permission.Granted)
{
Log.Warn(LOG_TAG, "CheckSelfPermission(WriteExternalStorage) not yet granted - will prompt user for permission");
listPermissions.Add(Android.Manifest.Permission.WriteExternalStorage);
// Make the request with the permissions needed...and then check OnRequestPermissionsResult() for the results
RequestPermissions(listPermissions.ToArray(), PERMISSION_Write_External_Storage);
}
else
{
var url = e.Url;
DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(url));
request.AllowScanningByMediaScanner();
string filename = System.IO.Path.GetFileName(url);
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
// request.SetNotificationVisibility(DownloadManager.Request.VisibilityVisibleNotifyCompleted); //Notify client once download is completed!
request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);
DownloadManager dm = (DownloadManager)GetSystemService("download");
dm.Enqueue(request);
Toast.MakeText(ApplicationContext, "Downloading File", ToastLength.Long//To notify the Client that the file is being downloaded
).Show();
}
}
```
Solutions:
1 .Define a static property in your MainActivity and use it in Fragment, for example:
public class MainActivity : AppCompatActivity
{
public static MainActivity Instance;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Instance = this;
}
public void test()
{
}
}
And then in your fragment, you can access the method by:
MainActivity.Instance.test();
2 .getActivity() method in C# is ((ActivityType)Activity).yourPublicMethod()
; :
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
((MainActivity)Activity).test();
return base.OnCreateView(inflater, container, savedInstanceState);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.