I am struggling to find a solution to pass a global variable into a class.
I've tried to create a public function that get the global variable's value it seem like I can't call that function from the class.
Below is my code:
string latitude = null;
string longitude = null;
public string geturl() {
return latitude + "," + longitude;
}
public Form1() {
Watcher = new GeoCoordinateWatcher();
Watcher.StatusChanged += Watcher_StatusChanged;
InitializeComponent();
Watcher.Start();
ChromiumWebBrowser a = new ChromiumWebBrowser();
panel1.Controls.Add(a);
a.Dock = System.Windows.Forms.DockStyle.Fill;
a.Load("https://drive.google.com/open?id=12j590lCugajX1T64DHKcSiX9RwwBkAeF&usp=sharing");
CefSettings settings = new CefSettings();
BrowserLifeSpanHandler blsh = new BrowserLifeSpanHandler();
a.LifeSpanHandler = blsh;
}
public GeoCoordinateWatcher Watcher = null;
private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) {
if (e.Status == GeoPositionStatus.Ready){
// Display the latitude and longitude.
if (Watcher.Position.Location.IsUnknown){
latitude = "Cannot find location data";
} else {
latitude = Watcher.Position.Location.Latitude.ToString();
longitude = Watcher.Position.Location.Longitude.ToString();
}
}
}
public class BrowserLifeSpanHandler : ILifeSpanHandler {
public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName,
WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo,
IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) {
newBrowser = null;
MessageBox.Show(targetUrl);
ChromiumWebBrowser b = new ChromiumWebBrowser(targetUrl);
return false;
}
public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser) {
//
}
public bool DoClose(IWebBrowser browserControl, IBrowser browser) {
return false;
}
public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser) {
//nothing
}
}
How can I pass the values of the string geturl()
function into the BrowserLifeSpanHandler
class?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…