如何访问片段 class 中 MainActivity class 的 public 成员(变量或方法)
how to access public Members(variable or method) of MainActivity class in fragment class
我有一个 Xamarin-Android 应用程序,我在其中使用了片段。我想在我的片段 class 中访问 public MainActivity 方法。
webview_download+=mWebViewDownload
但是此方法 mWebViewDownload 是在 MainActivity.i 中定义的,无法从片段访问此方法。
我试图使这个方法成为静态的,但是这个方法使用了没有实例就无法访问的服务。
我尝试通过 this.mWebViewDownload
访问,但错误是 mWebViewDownload 未在此范围内定义。
我在 Whosebug 上搜索了大部分问题建议 getActivity() 但这是 java 相关的解决方案,但我需要 c# 相关的解决方案。
我试图通过 MainActivity.mWebViewDownload
访问它,但它也给出了无法在没有对象引用的情况下访问非静态的错误,例如 that.please help.fragment class 如下:
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;
}
}
这里是MainActivity中的mWebView_Download方法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();
}
}
```
解决方案:
1.在你的MainActivity中定义一个静态的属性并在Fragment中使用它,例如:
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()
{
}
}
然后在您的片段中,您可以通过以下方式访问该方法:
MainActivity.Instance.test();
2.getActivity()方法在C#中是((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);
}
我有一个 Xamarin-Android 应用程序,我在其中使用了片段。我想在我的片段 class 中访问 public MainActivity 方法。
webview_download+=mWebViewDownload
但是此方法 mWebViewDownload 是在 MainActivity.i 中定义的,无法从片段访问此方法。
我试图使这个方法成为静态的,但是这个方法使用了没有实例就无法访问的服务。
我尝试通过 this.mWebViewDownload
访问,但错误是 mWebViewDownload 未在此范围内定义。
我在 Whosebug 上搜索了大部分问题建议 getActivity() 但这是 java 相关的解决方案,但我需要 c# 相关的解决方案。
我试图通过 MainActivity.mWebViewDownload
访问它,但它也给出了无法在没有对象引用的情况下访问非静态的错误,例如 that.please help.fragment class 如下:
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;
}
}
这里是MainActivity中的mWebView_Download方法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();
}
}
```
解决方案:
1.在你的MainActivity中定义一个静态的属性并在Fragment中使用它,例如:
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()
{
}
}
然后在您的片段中,您可以通过以下方式访问该方法:
MainActivity.Instance.test();
2.getActivity()方法在C#中是((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);
}